片段最佳实践 - Android

时间:2012-06-04 07:00:56

标签: android android-fragments

我正在努力解决片段的结构,事情是......在Activity中有两个片段。一个包含一个列表。请拨打此FragmentA。另一个包含细节。请拨打此FragmentB

对于FragmentA中的每个列表项,FragmentB都有不同的视图,那么处理这种情况的首选方法是什么?

谢谢

1 个答案:

答案 0 :(得分:2)

如果没有看到相关应用的复杂性,我建议将FragmentB的每个不同视图都用自己的片段表示。

根据您在R.id.fragment_container中的选择,使用Fragment Transaction方法替换占位符(让我们称之为FragmentBFragmentA所在的位置。像这样:

// 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();
相关问题