SwipeRefreshLayout和RecyclerView一起使用的时候,有时出现RecyclerView没有滑动到顶部,手指向下滑动时,触发了SwipeRefreshLayout的刷新事件,造成了冲突。
首先来看一个http://blog.csdn.net/zhangweiwtmdbf/article/details/41206369博客分享的一个解决方法,如下:
mLayoutManager = new LinearLayoutManager(getActivity());
//mPicListView是RecyclerView对象
mPicListView.setLayoutManager(mLayoutManager);
mPicListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);
//加载更多
if (!isLoading
&& hasMore
&& mAdapter.getItemCount() == (mLayoutManager
.findLastVisibleItemPosition() + 1)
&& newState == RecyclerView.SCROLL_STATE_IDLE) {
isLoading = true;
requestServerArticleMore();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//解决RecyclerView和SwipeRefreshLayout共用存在的bug
mRefreshLayout.setEnabled(mLayoutManager
.findFirstCompletelyVisibleItemPosition() == 0);
}
});
这个我试了下,也能解决,但与单个item不会超过屏幕高度的情况下,上面的解决方法是可行的,反之就不行了。
找了好久,又找到了更好的解决办法:
http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int topRowVerticalPosition =
(recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
圆满解决问题