其实是因为三星旗舰机太灵敏,在一个click事件中会触发 move 事件,而move被拦截,导致 click 不能触发。
// 最小移动像素。如果起始移动距离小于这个值,就不认为用户做了move,不对事件进行主动拦截
private static final float MIN_MOVE_PIXEL = 3;
private boolean mIsIntercept = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
mIsIntercept = false;
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - xLast);
yDistance += Math.abs(curY - yLast);
// 如果起始移动距离小于这个值,就不认为用户做了move,不对事件进行主动拦截
if (!mIsIntercept && Math.abs(curX - xLast) < MIN_MOVE_PIXEL
&& Math.abs(curY - yLast) < MIN_MOVE_PIXEL) {
break;
}
// 当x方向移动的距离比较大时,交给子view处理,保证viewpager横向滑动
if (xDistance * 0.5f >= yDistance) {
break;
}
if (yLast > curY) { //当手向上划,列表向上滚动时
boolean thisCanScrollVertically = this.canScrollVertically(1);
if (thisCanScrollVertically) {
//nothing
} else {
//nothing
}
return thisCanScrollVertically;
mIsIntercept = thisCanScrollVertically;
return mIsIntercept;
} else { //当手向下划,列表向下滚动时
if (this.canScrollVertically(-1)) { // 如果当前scrollview滚动到顶,则交给listview处理
if (mViewPager == null) {
break;
}
if (position != mViewPager.getCurrentItem() || fragment == null) {
position = mViewPager.getCurrentItem();
fragment = mViewPager.getCurrentFragment();
}
try {
if (fragment == null) {
break;
}
View view = fragment.getView().findViewById(R.id.recyclerview);
if (view == null) {
break;
}
boolean viewCanScrollVerDown = view.canScrollVertically(-1);
return !viewCanScrollVerDown;
mIsIntercept = !viewCanScrollVerDown;
return mIsIntercept;
} catch (Exception e) {
e.printStackTrace();
}
} else {
return false;
mIsIntercept = false;
return mIsIntercept;
}
}
xLast = curX;
yLast = curY;
}
return super.onInterceptTouchEvent(ev);
}