2017-07-03 6 views
0

Ich habe ein Fragment, das das Elternteil eines anderen Fragments ist. Ich bin das Fragment Kind wie folgt ergänzt:Suchen von Kind-Fragment durch Tag in einem anderen Fragment

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_main, container, false); 

     FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
     transaction.add(R.id.list_container, new SwipeableFragment(), FRAGMENT_LIST_VIEW).commit(); 
     transaction.addToBackStack(FRAGMENT_LIST_VIEW); 

return view; 
} 

Später, ich habe eine Methode in meiner Eltern-Fragment, das das Kind finden muss. So mache ich so:

final Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_LIST_VIEW); 

Aber immer Null zurückgibt. Was mache ich falsch?

Antwort

2

Ändern Sie diesen Code mit dem Code unten angegeben:

// Your Code: 
final Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_LIST_VIEW); 


// Replace with this: 
final Fragment fragment = getChildFragmentManager().findFragmentByTag(FRAGMENT_LIST_VIEW); 

Hoffentlich wird es funktionieren!

Verwandte Themen