最佳实践 - Android碎片化

3

我对Fragment的结构感到困惑,情况是这样的:在Activity中有两个Fragment,其中一个包含列表,称其为FragmentA,另一个包含详情,称其为FragmentB

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

谢谢

1个回答

3

考虑到应用程序的复杂性,我建议为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();

2
但要注意不同的屏幕尺寸,在小尺寸上启动新活动而不是提交片段事务。 - Michał Klimczak
绝对的,你肯定想要以不同的方式处理显著不同的屏幕尺寸... - Jonathan Schneider
谢谢您的帮助,我认为这应该完美地适用于我的情况。 - Nixit Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接