http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout
SwipeRefreshLayout和RecyclerView一起使用的时候,有时出现RecyclerView没有滑动到顶部,手指向下滑动时,触发了SwipeRefreshLayout的刷新事件,造成了冲突。
1.解决办法
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); } });
2. 写一个类继承SwipeRefreshLayout,然后重写 canChildScrollUp
public class SimpleSwipeRefreshLayout extends SwipeRefreshLayout { private View view; public SimpleSwipeRefreshLayout(Context context) { super(context); } public SimpleSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); } public void setViewGroup(View view) { this.view = view; } @Override public boolean canChildScrollUp() { if (view != null && view instanceof AbsListView) { final AbsListView absListView = (AbsListView) view; return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0) .getTop() < absListView.getPaddingTop()); } return super.canChildScrollUp(); } }
3.解决方法
final LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){ @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { swipeRefresh.setEnabled(layoutManager.findFirstCompletelyVisibleItemPosition() == 0); } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } });