protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
// Count backwards - let topmost views consume scroll distance first.
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
// This will not work for transformed views in Honeycomb+
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
canScroll(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && ViewCompat.canScrollHorizontally(v, -dx);
}
因为
ViewPager.onInterceptTouchEvent()在一般情况下会拦截横向的滑动, 所以你的图片收不到MotionEvent.ACTION_MOVE动作.解决思路大致为:
在
ViewPager.onInterceptTouchEvent()的MotionEvent.ACTION_MOVE下有一下代码可以不拦截横向滑动只要你能执行这段代码, ViewPager就不会拦截这次的横向滑动, 关键的方法应该是
canScroll()源码如下
大致就是判断里面的子控件是否可以横向滑动
因为你里面最后是一个
ImageView, 所以最后关键就是ViewCompat.canScrollHorizontally(v, -dx);当v时ImageView的时候要返回true如果你使用的版本较高的话, 实际执行的就是
ImageView.canScrollHorizontally()方法, 这里面的代码很简单, 就不贴出来了, 这个方法直接继承了View.canScrollHorizontally(), 返回的是false, 所以解决方案想到2个用一个可以滑动的控件作为
ImageView的父控件重写
ImageView.canScrollHorizontally(), 令到它返回true推荐方法1
上面的分析没有写代码验证, 可能有错, 欢迎指正