2014-04-30 16 views
15

Consideriamo un caso in cui ho Fragment A e Fragment B.Passando all'interfaccia su Fragment

B dichiara:

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

A implementa questa interfaccia.

Quando spingendo Fragment B in pila, come devo passare di riferimento di Fragment A per esso in modo BundleA può ottenere il onTrigger richiamata in caso di necessità.

Il mio scenario di utilizzo è che A ha ListView con articoli e B ha ViewPager con elementi. Entrambi contengono gli stessi elementi e quando l'utente passa da B -> A prima di scattare B deve attivare la richiamata per A per aggiornarlo alla posizione ListView in modo che corrisponda alla posizione del cercapersone B.

Grazie.

risposta

16
Passing interface to Fragment 

penso che si sta comunicando tra due Fragment

Per fare ciò, si può avere uno sguardo in 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

Ma la mia attività non ha nulla a che fare con i due frammenti che non vogliono comunicare, non esiste un'altra soluzione? – Niko

+0

@Niko puoi spiegare un po 'di più con qualche interfaccia utente. In modo che io possa aiutarti. –

+0

Ho aggiornato la mia domanda con il caso d'uso. – Niko

1

Utilizzando @ risposta di Amit, e adattando alla domanda PO , ecco tutto il codice rilevante:

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

Ha fatto lo stesso ma non ha ottenuto la richiamata in Frammento A, ma ho ottenuto il callback in Attività. –

2

Per Kotlin 1.0.0-beta-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

Si è ottimale per due frammenti di comunicare solamente attraverso un'attività. Quindi puoi definire un'interfaccia in Fragment B implementata nell'attività. Poi l'attività, definire nel metodo di interfaccia ciò che si vuole che accada in frammento A.

Nel frammento B,

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"); 
     } 
} 

Metodo per determinare se l'utente passa da B ad A

public void onChangeFragment(int position){ 
//other logic here 
mCallback.onTrigger(position); 
} 

In Activity,

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

Goodluck!