在刷新期间使用SwipeRefreshLayout切换片段时,片段冻结但实际上仍然有效

zlm*_*012 72 android android-fragments swiperefreshlayout android-recyclerview

更新:我认为它工作正常.但经过一些测试仍然存在麻烦*嗅*

然后我做了一个更简单的版本,看看究竟发生了什么,并且我知道应该已经分离的刷新片段仍然留在那里.或者确切地说,旧片段的视图留在那里,较新的片段之上.由于RecyclerView的原始应用程序的背景不透明,所以我之前说过.

更新结束


我有这样的MainActivity布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start" tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

ContentView我用来填充的片段@id/container配置为:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/tweet_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_300"/>

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

这里是onCreateView()ContentView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View inflatedView = inflater.inflate(R.layout.fragment_content, container, false);

    mRecyclerView = (RecyclerView) inflatedView.findViewById(R.id.tweet_list);
    mRecyclerView.setHasFixedSize(false);

    mLinearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mRecyclerView.setLayoutManager(mLinearLayoutManager);

    mTweetList = new ArrayList<Tweet>;
    mAdapter = new TweetAdapter(mTweetList);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    mSwipeRefreshLayout = (SwipeRefreshLayout) inflatedView.findViewById(R.id.contentView);
    mSwipeRefreshLayout.setOnRefreshListener(
        ...
    );
    return inflatedView;
}
Run Code Online (Sandbox Code Playgroud)

然后MainActivity我通过切换不同来切换内容显示ContentView.所有看起来都不错,除了一件事:当我ContentView在导航抽屉刷新时切换片段,然后内容冻结.但实际上所有事情都像往常一样工作,除非你看不到它.

zlm*_*012 104

嗯......经过一番挣扎,我最终以一种棘手的方式解决了这个问题...

我只需要添加以下内容onPause():

@Override
public void onPause() {
    super.onPause();
    ...

    if (mSwipeRefreshLayout!=null) {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.destroyDrawingCache();
        mSwipeRefreshLayout.clearAnimation();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在FrameLayout中包装SwipeRefreshLayout解决了appcompat v7:22+ (5认同)
  • 谢谢.我在FragmentTransaction期间只有`setRefreshing(false)`.添加`clearAnimation()`后,我的应用程序无需冻结. (2认同)

use*_*603 75

这个问题似乎仍然发生在appcompat 22.1.1中.在FrameLayout中包装SwipeRefreshLayout解决了这个问题

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout

    android:id="@+id/contentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/tweet_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_300" />

</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这有效.有什么想法的原因? (7认同)
  • 在25.2.0仍然发生了bug,这解决了问题.我想在找到这个解决方案之前翻转表格.谢谢. (3认同)
  • 谢谢!它解决了我的问题!我浪费了这么多时间来寻找原因和解决方案.它必须被接受为正确的答案! (2认同)