我使用 fragmentTransaction.replace()
将 FrameLayout 替换为 fragment 。
布局:
<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。
所以 fragment 仍然会显示在屏幕上。
如果我将 articlesAppender
的可见性设置为 GONE
。
所以它应该是这样的:
<FrameLayout
android:id="@+id/articlesAppender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</FrameLayout>
然后 fragment 将不会在屏幕上可见,但是当我稍后尝试调用 view.setVisibility(View.VISIBLE);
时,它仍然不起作用。
该 fragment 仍然不可见。
这意味着 inflater.inflate(R.layout.articles_fragment, container, false);
返回的 view
不是 fragment 的真实 View 。
但它在支持库 25.0.1 上完美运行。
所以这是 Android 的错误?
最佳答案
问题已报告。更多人似乎有这个问题
关于Android - fragmentTransaction.replace() 不适用于支持库 25.1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41158071/