2017-07-03 9 views
0

他のフラグメントの親であるフラグメントがあります。私はこのようなフラグメントの子を追加しています:他のフラグメント内のタグによる子フラグメントの検索

@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; 
} 

後で、私は子供を見つける必要がある親の断片にメソッドを持っています。だから私はこのようにしています:

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

しかし、常にnullを返します。私は間違って何をしていますか?

答えて

2

変更下記のコードで、このコード:

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


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

うまくいけば、それは動作します!

関連する問題