【发布时间】:2011-11-17 14:30:46
【问题描述】:
在 Android 3.2 上,有 2 个新方法:FragmentTransaction 的附加和分离。 但是,这些功能不适用于 Android 3.0 和 3.1。 有没有办法绕过它?
Tkx
【问题讨论】:
标签: android android-3.0-honeycomb android-fragments fragment
在 Android 3.2 上,有 2 个新方法:FragmentTransaction 的附加和分离。 但是,这些功能不适用于 Android 3.0 和 3.1。 有没有办法绕过它?
Tkx
【问题讨论】:
标签: android android-3.0-honeycomb android-fragments fragment
如果您打算进行分段事务,只需使用文档齐全的 3.0 和 3.1 API 版本即可。您应该检查FragmentTransaction 类中的add、remove 和replace。
有关实际示例,请查看Performing Fragment Transactions,并阅读整个Fragments 框架主题。这是我所知道的最好的资源之一。
另外,请查看以下教程: The Android 3.0 Fragments API 和 How to use the Fragment class in the Android Honeycomb SDK.
不管怎样,这里是一个代码剪辑:
Fragment f = new TestFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
// Replaces an existing fragment with the newly created one
ft.replace(R.id.the_frag, f);
// Any transition you prefer
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Add it to the stack so the transition will be remembered and could be reversed
ft.addToBackStack(null);
ft.commit();
如果你只是想detach 或attach 一个片段,这可以通过上面提到的添加和删除来完成,并确保在添加时膨胀它的内容视图。
【讨论】: