我对Fragment的结构感到困惑,情况是这样的:在Activity中有两个Fragment,其中一个包含列表,称其为FragmentA
,另一个包含详情,称其为FragmentB
。
在FragmentA
中的每个列表项都有一个不同于FragmentB
的视图,那么处理这种情况的首选方式是什么?
谢谢
我对Fragment的结构感到困惑,情况是这样的:在Activity中有两个Fragment,其中一个包含列表,称其为FragmentA
,另一个包含详情,称其为FragmentB
。
在FragmentA
中的每个列表项都有一个不同于FragmentB
的视图,那么处理这种情况的首选方式是什么?
谢谢
考虑到应用程序的复杂性,我建议为FragmentB
的每个不同视图创建其自己的片段。
使用片段事务方法替换占位符(我们称之为R.id.fragment_container
),其中FragmentB
是根据您在FragmentA
中选择的相应片段。大致如下:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();