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;
}
}