Fragment 中使用 SwipeRefreshLayout 导致的不能退出问题

之前做项目的时候,发现在 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>



参考自:

https://stackoverflow.com/questions/27057449/when-switch-fragment-with-swiperefreshlayout-during-refreshing-fragment-freezes


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值