之前做项目的时候,发现在 Fragment 中使用 SwipeRefreshLayout 会有一个问题,就是当 SwipeRefreshLayout 正在刷新的时候如果切换 Fragment ,会导致当前 Fragment 的界面保留在 Activity 中的相应位置上,切换到其他的 Fragment 会和之前的 Fragment 重合,并且 SwipeRefreshLayout 会一直处于刷新状态直到退出 Activity ,解决方法有两个,下面分别说明。
方法一是在退出 Fragment 的时候结束 SwipeRefreshLayout 动画,并且清除缓存,如下所示:
@Override
public void onDestroyView() {
super.onDestroyView();
refreshLayout.setRefreshing(false);
refreshLayout.destroyDrawingCache();
refreshLayout.clearAnimation();
unbinder.unbind();
}
方法二更简单,在 SwipeRefreshLayout 的外层嵌套一层 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>
参考自: