如何在Fragment实现中显示不同的活动?

时间:2011-07-14 13:08:36

标签: android android-layout android-emulator android-widget android-manifest

这是我第一次使用android片段。我正在关注this tutorial以实现片段。

本教程一切正常,我成功得到如下结果:

enter image description here

在本教程中, DetailsFragment 只显示包含当前所选项目文本的TextView。这是正确的部分只显示了一些文本。

我的问题是如何在右侧显示不同的活动而不是文本视图。

我的意思如下图所示,例如,图像中“ 1 ”的区域是一项活动。如何选择左侧的列表项时显示不同的活动?

enter image description here

2 个答案:

答案 0 :(得分:1)

您不显示活动,显示片段。实现Fragment类而不是Activity类。然后,就像在Activity中一样构建View。请记住,对于需要访问活动的实例,Fragment类具有方便的getActivity()方法。

答案 1 :(得分:0)

使用FragmentManager.beginTransaction()启动FragmentTransaction。通过该操作,您可以隐藏并显示新的碎片。它也使用android历史堆栈进行管理。

https://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction() https://developer.android.com/reference/android/app/FragmentTransaction.html

以下是一些代码:Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?