本文介绍了Android -fragmentTransaction.replace()在支持库25.1.0上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用fragmentTransaction.replace()用片段替换了FrameLayout.

I replace a FrameLayout with a fragment using fragmentTransaction.replace().

布局:

<FrameLayout
        android:id="@+id/articlesAppender"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</FrameLayout>

替换Activity的onCreate:

Replacing in Activity's 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:

ArticleFragment's 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.所以它应该像这样:

But the view.setVisibility(View.GONE); is not working on support library 25.1.0.So the fragment will still be displayed on the screen.If I set the visibility of the articlesAppender to GONE.So it should look like this:

<FrameLayout
        android:id="@+id/articlesAppender"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
</FrameLayout>

然后该片段将在屏幕上不可见,但是当我稍后尝试调用view.setVisibility(View.VISIBLE);时,它仍然不起作用.该片段仍然不可见.

Then the the fragment will not be visible on the screen, but when I try to call view.setVisibility(View.VISIBLE); later, it still not works.The fragment still not be visible.

这意味着inflater.inflate(R.layout.articles_fragment, container, false);返回的view不是该片段的真实视图.但它在支持库25.0.1上完美运行.

That means the view which is returned by inflater.inflate(R.layout.articles_fragment, container, false); is not the real view of the fragment.But it works perfectly on support library 25.0.1.

这是Android的错误吗?

So it's an Android's bug?

推荐答案

问题.似乎有更多人遇到这个问题

issue is reported. more people seems to have this issue

https://code.google.com/p/android/issues/detail?id = 230191

这篇关于Android -fragmentTransaction.replace()在支持库25.1.0上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:53
查看更多