解决问题:recyclerview未滑动到顶部就进行下拉刷新
解决方法:对recyclerview进行滑动监听,然后禁用和启用刷新控件。
第一种方式:
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//解决滑动冲突
boolean enable=recyclerView.getChildCount() == 0 || recyclerView.getChildAt(0).getTop() >= 0;
mRefresh.setEnabled(enable);
}
});
第二种方式:
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//解决滑动冲突
mRefresh.setEnabled(manager.findFirstVisibleItemPosition() == 0);
}
});
第三种方式:
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//解决滑动冲突
int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
mRefresh.setEnabled(topRowVerticalPosition >= 0);
}
});
任君挑选,目测第二种最好用。