Android SwipeRefreshLayout 、RecyclerView冲突下拉冲突的解决方案

SwipeRefreshLayout和RecyclerView一起使用的时候,有时出现RecyclerView没有滑动到顶部,手指向下滑动时,触发了SwipeRefreshLayout的刷新事件,造成了冲突。

首先来看一个http://blog.csdn.net/zhangweiwtmdbf/article/details/41206369博客分享的一个解决方法,如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mLayoutManager = new LinearLayoutManager(getActivity());  
  2.             //mPicListView是RecyclerView对象  
  3.             mPicListView.setLayoutManager(mLayoutManager);  
  4.             mPicListView.setOnScrollListener(new OnScrollListener() {  
  5.                 @Override  
  6.                 public void onScrollStateChanged(RecyclerView recyclerView,  
  7.                         int newState) {  
  8.                     super.onScrollStateChanged(recyclerView, newState);  
  9.                     //加载更多  
  10.                     if (!isLoading  
  11.                             && hasMore  
  12.                             && mAdapter.getItemCount() == (mLayoutManager  
  13.                                     .findLastVisibleItemPosition() + 1)  
  14.                             && newState == RecyclerView.SCROLL_STATE_IDLE) {  
  15.                         isLoading = true;  
  16.                         requestServerArticleMore();  
  17.                     }  
  18.                 }  
  19.   
  20.                 @Override  
  21.                 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {  
  22.                     super.onScrolled(recyclerView, dx, dy);  
  23.                     //解决RecyclerView和SwipeRefreshLayout共用存在的bug  
  24.                     mRefreshLayout.setEnabled(mLayoutManager  
  25.                             .findFirstCompletelyVisibleItemPosition() == 0);  
  26.                 }  
  27.             });  

这个我试了下,也能解决,但与单个item不会超过屏幕高度的情况下,上面的解决方法是可行的,反之就不行了。

找了好久,又找到了更好的解决办法:

http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){  
  2.         @Override  
  3.         public void onScrolled(RecyclerView recyclerView, int dx, int dy) {  
  4.             int topRowVerticalPosition =  
  5.                     (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();  
  6.             swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);  
  7.   
  8.         }  
  9.   
  10.         @Override  
  11.         public void onScrollStateChanged(RecyclerView recyclerView, int newState) {  
  12.             super.onScrollStateChanged(recyclerView, newState);  
  13.         }  
  14.     });  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值