你必须使用另一个Fragment
来做到这一点。您需要创建它Fragment
,然后用 替换旧的FragmentTransaction
,如果要使用“后退”按钮,则需要将新片段添加到Backstack
. 如果这些ListFragment
在数据方面完全不同,我将有 2 个片段类(例如:FirstListFragment、SecondListFragment)。
这是我工作过的应用程序的一些代码:
// Here I get the FragmentTransaction
FragmentTransaction ft = getFragmentManager().beginTransaction();
// I replace it with a new Fragment giving it data. Here you need to tell
// the FragmentTransaction what (or actually where) and by what you want to replace.
ft.replace(R.id.fraglist, new ModuleFragment(lpId, sqId, sqName));
R.id.fraglist 应该在您的 XML 布局中定义(除非您以编程方式创建布局)。它只是您的默认片段的 ID。
// I set the animation
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// I add it to the backstack so I can use the back button
ft.addToBackStack(null);
// Then I finally commit so it happens !
ft.commit();
您还可以使用 Bundle 来解析数据,如下所示:
Bundle moduleBundle = new Bundle();
moduleBundle.putString("id", id);
moduleBundle.putString("description", description);
moduleFragment.setArguments(moduleBundle);