SwipeRefreshLayout和ViewPager一起使用的时候,事件处理上会有冲突,比如当你想要左右滑动的时候,但是因为角度稍微斜了一些,极有可能导致ViewPager没有滑动起来,却触发了SwipeRefreshLayout的刷新动画。解决这个问题的思路就是继承SwipeRefreshLayout,在OnInterceptTouchEvent函数中拦截处理:
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* 支持调整下拉灵敏度
* Created by zhangming on 15/11/19.
*/
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
private float mInitialDownY;
private int mTouchSlop;
public MySwipeRefreshLayout(Context context) {
this(context, null);
}
public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mInitialDownY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float yDiff = ev.getY() - mInitialDownY;
if (yDiff < mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
/**
* @return 返回灵敏度数值
*/
public int getTouchSlop() {
return mTouchSlop;
}
/**
* 设置下拉灵敏度
*
* @param mTouchSlop dip值
*/
public void setTouchSlop(int mTouchSlop) {
this.mTouchSlop = mTouchSlop;
}
}
网友评论