自定义NestedScrollView解决包裹ViewPager
2022-07-05 本文已影响0人
苏坡坡要吃婆婆酥
public class MNestedScrollView extends NestedScrollView {
private float xDistance, yDistance, xLast, yLast;
public MNestedScrollView(@NonNull Context context) {
super(context);
}
public MNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
xLast = ev.getX();
yLast = ev.getY();
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);
xLast = curX;
yLast = curY;
if (xDistance > yDistance) {
// 返回true,表示当前控件要拦截事件,紧接着事件交给当前控件的onTouchEvent方法来处理。
// 返回默认/false,表示不拦截事件,事件最终继续往下分发。
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}