2014-04-30 10 views
15

पर इंटरफ़ेस पास करना एक ऐसे मामले पर विचार करें जहां मेरे पास Fragment A और Fragment B है।फ्रैगमेंट

B वाणी:

public interface MyInterface { 
    public void onTrigger(int position); 
} 

A इस इंटरफेस लागू करता है।

जब ढेर में Fragment B धक्का, मैं कैसे Bundle में इसके लिए Fragment A के संदर्भ पास करना चाहिए ताकि A जब जरूरत onTrigger कॉलबैक प्राप्त कर सकते हैं।

मेरा उपयोग केस परिदृश्य यह है कि A में ListView आइटम और B के साथ ViewPager है। दोनों में एक ही आइटम होते हैं और जब B पॉपिंग करने से पहले उपयोगकर्ता B -> A से जाता है तो B पेजर स्थिति के साथ मिलान करने के लिए ListView स्थिति अपडेट करने के लिए इसे A के लिए कॉलबैक ट्रिगर करना चाहिए।

धन्यवाद।

उत्तर

16
Passing interface to Fragment 

मुझे लगता है कि आप दो Fragment

के बीच संवाद कर रहे हैं ऐसा करने के लिए, आप Communicating with Other Fragments

public class FragmentB extends Fragment{ 
    MyInterface mCallback; 

    // Container Activity must implement this interface 
    public interface MyInterface { 
     public void onTrigger(); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (MyInterface) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement MyInterface "); 
     } 
    } 

    ... 
} 
+3

लेकिन मेरी गतिविधि के पास उन दो टुकड़ों से कोई लेना देना नहीं है जो संवाद नहीं करना चाहते हैं, क्या कोई अन्य समाधान नहीं है? – Niko

+0

@ निको आप कुछ उपयोगकर्ता इंटरफ़ेस के साथ थोड़ा और समझा सकते हैं। ताकि मैं आपकी मदद कर सकूं। –

+0

उपयोग के मामले में मेरे प्रश्न को अपडेट किया गया। – Niko

1

@ अमित के जवाब का उपयोग में देख सकते हैं, और ऑप्स सवाल के अनुरूप ढलने , यहां सभी प्रासंगिक कोड हैं:

public class FragmentA extends BaseFragment implements MyInterface { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     // THIS IS JUST AN EXAMPLE OF WHERE YOU MIGHT CREATE FragmentB 
     FragmentB myFragmentB = new FragmentB();   
    } 


    void onTrigger(int position){ 
     // My Callback Happens Here! 
    } 
} 

...

public class FragmentB extends BaseFragment { 

    private MyInterface callback; 

    public interface MyInterface { 
     void onTrigger(int position); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      callback = (MyInterface) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement MyInterface"); 
     } 
    } 
} 
+0

पूरा करने में मदद की, वही किया लेकिन फ्रैगमेंट ए में कॉलबैक नहीं मिला लेकिन मुझे गतिविधि में कॉलबैक मिला। –

2

Kotlin 1.0.0 बीटा 3595 के लिए

interface SomeCallback {} 

class SomeFragment() : Fragment(){ 

    var callback : SomeCallback? = null //some might want late init, but I think this way is safer 

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { 
     callback = activity as? SomeCallback //returns null if not type 'SomeCallback' 

     return inflater!!.inflate(R.layout.frag_some_view, container, false); 
    } 
} 
1

यह के लिए दो टुकड़े केवल एक गतिविधि के माध्यम से संवाद करने के इष्टतम है। तो आप गतिविधि में लागू किया गया है कि Fragment बी में एक इंटरफ़ेस परिभाषित कर सकते हैं। तब गतिविधि में, निर्धारित करने के लिए इंटरफ़ेस विधि में परिभाषित तुम क्या टुकड़ा ए

टुकड़ा बी में में हो करना चाहते हैं,

MyInterface mCallback; 
public interface MyInterface { 
     void onTrigger(int position); 
    } 

@Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (MyInterface) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement MyInterface"); 
     } 
} 

विधि उपयोगकर्ता एक

public void onChangeFragment(int position){ 
//other logic here 
mCallback.onTrigger(position); 
} 
को बी से हो जाता है

गतिविधि में,

public void onTrigger(int position) { 
    //Find listview in fragment A 
    listView.smoothScrollToPosition(position); 
    } 

गुडलक!