2014-04-30 10 views
15

Rozważmy przypadek, w którym mam Fragment A i Fragment B.Przechodząc interfejs Fragment

B deklaruje:

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

A implementuje ten interfejs.

Kiedy naciska Fragment B do stosu, jak mam przekazać odniesienie Fragment A go w Bundle tak A można uzyskać onTrigger oddzwanianie, gdy są potrzebne.

Według mojego scenariusza użycia A ma ListView z przedmiotami i B ma ViewPager z przedmiotami. Obie zawierają te same elementy, a gdy użytkownik wychodzi z B -> A przed popping B powinno wywołać zwrotnego dla A zaktualizować to ListView pozycję pasujące z B pozycji pagera.

Dzięki.

Odpowiedz

16
Passing interface to Fragment 

myślę, że komunikują się między dwoma Fragment

W tym celu, można zajrzeć do 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

Ale moja aktywność nie ma nic wspólnego z dwóch fragmentów, które wa się komunikować, nie istnieje inne rozwiązanie? – Niko

+0

@Niko możesz wyjaśnić nieco więcej za pomocą interfejsu użytkownika. Aby ci pomóc. –

+0

Zaktualizowałem moje pytanie w przypadku użycia. – Niko

1

Korzystanie @ odpowiedź Amit, a przystosowanie do pytania OPS , tutaj jest cały odpowiedni kod:

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

Zrobiłem to samo, ale nie dostałem wywołania zwrotnego w Fragmencie A, ale otrzymałem wywołanie zwrotne w Aktywności. –

2

Na 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

jest optymalne dla dwóch fragmentów komunikować się tylko z działania. Możesz więc zdefiniować interfejs w Fragmencie B, który jest zaimplementowany w działaniu. Następnie w działalności, określenie w metodzie interfejsu, co chcesz, aby stało się fragmentem A.

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

metoda określania, czy użytkownik przechodzi z B do A

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

W działaniu,

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

Goodluck!

Powiązane problemy