SwipRefreshLayout 和ViewPager和CoordinatorLayout 解决滑动冲突
1 CoordinatorLayout和SwipRefreshLayout 冲突解决:
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//verticalOffset AppBarLayout 滑动的距离,当滑动到最低端时为0
// 往上滑为负,所以>=0 时为最低端,<0 时为 向上滑动了一段距离
if (verticalOffset >= 0) {
mRefreshLayout.setEnabled(true);
} else {
mRefreshLayout.setEnabled(false);
}
});
2 SwipRefreshLayout 和ViewPager的滑动冲突解决:
onPageScrollStateChanged() 参数:
- @param state The new scroll state.
- @see ViewPager#SCROLL_STATE_IDLE 空闲
- @see ViewPager#SCROLL_STATE_DRAGGING 滑动
- @see ViewPager#SCROLL_STATE_SETTLING 停止滑动
/**
- Indicates that the pager is in an idle, settled state. The current page
- is fully in view and no animation is in progress.
*/
public static final int SCROLL_STATE_IDLE = 0;
/**
- Indicates that the pager is currently being dragged by the user.
*/
public static final int SCROLL_STATE_DRAGGING = 1;
/**
- Indicates that the pager is in the process of settling to a final position.
*/
public static final int SCROLL_STATE_SETTLING = 2;
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
if(i == 1) {//滑动过程中关闭下拉刷新
mRefreshLayout.setEnabled(false);//设置不可触发
}else if(i == 2){//停止滑动时触发,可以刷新
mRefreshLayout.setEnabled(true);//设置可触发
}
}
});