【发布时间】:2016-12-15 06:50:36
【问题描述】:
我将 FrameLayout 替换为使用 fragmentTransaction.replace() 的片段。
布局:
<FrameLayout
android:id="@+id/articlesAppender"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
在Activity的onCreate中替换:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
articlesFragment = (ArticlesFragment) fragmentManager.findFragmentByTag(ARTICLES_FRAGMENT_TAG);
if (articlesFragment == null) {
articlesFragment = new ArticlesFragment();
}
fragmentTransaction.replace(R.id.articlesAppender, articlesFragment, ARTICLES_FRAGMENT_TAG);
fragmentTransaction.commit();
ArticleFragment 的 onCreate:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.articles_fragment, container, false);
view.setVisibility(View.GONE);
return view;
}
但view.setVisibility(View.GONE); 不适用于支持库 25.1.0。
所以片段仍然会显示在屏幕上。
如果我将articlesAppender 的可见性设置为GONE。
所以它应该是这样的:
<FrameLayout
android:id="@+id/articlesAppender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</FrameLayout>
然后该片段将不会在屏幕上可见,但是当我稍后尝试调用view.setVisibility(View.VISIBLE);时,它仍然无法正常工作。
该片段仍然不可见。
这意味着inflater.inflate(R.layout.articles_fragment, container, false);返回的view不是片段的真实视图。
但它在支持库 25.0.1 上完美运行。
所以这是 Android 的错误?
【问题讨论】:
-
你想用片段替换框架布局吗?
-
您是如何得出 replace() 方法不起作用的结论的?如果您尝试在该片段布局中的主根元素上而不是在膨胀视图上 setVisibility(View.GONE),它仍然不起作用吗?
-
@RishabhMahatha 是的,我想替换它。 @Gil 因为它适用于支持库 25.0.1。我已经描述了我的问题中的情况。我必须直接设置articleAppender 的可见性。这意味着它没有被替换。膨胀的视图是根元素,这就是我在
onCreateView()中返回此视图的原因。
标签: android fragment android-support-library