0
我的屏幕布局的主要部分显示了一组项目。用途可以选择是否显示网格或列表。因此,我实现了两个fragements(用于列表和网格),然后显示在占位符(主布局的主要部分)内。网格和列表片段之间的交换
但现在我想知道如何重用fragements以获得最后的滚动位置等。如果我记得私有字段中的片段而不是重新创建它,那么视图仍为空。
有什么想法?
public void onShowGrid(View view) {
// how to re-use instead of re-create
fragmentGrid = new PreviewGridFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.preview_fragment, fragmentGrid)
.addToBackStack(null).commit();
}
public void onShowList(View view) {
// how to re-use instead of re-create
fragmentList = new PreviewListFragment();
this.getFragmentManager().beginTransaction()
.replace(R.id.preview_fragment, fragmentList)
.addToBackStack(null).commit();
}
取而代之的是使用片段使用view.setvisibility它很安静易于管理 – 2014-09-11 10:03:39
是的,但这意味着使用大量的内存。但保留这些片段将会是相同的内存消耗。 – Matthias 2014-09-11 11:40:43
我想在我的应用程序的其他部分使用相同的片段。因此,片段是比视图更好的选择。 – Matthias 2014-09-11 12:03:55