viewpage里面放了一个imageview,MotionEvent.ACTION_MOVE里面设置imageview随着手指滑动,onTouchEvent是返回了true。纵向滑动没有问题,横向滑动会出现滑动一点点距离就划不动了,viewpage开始滑动。这是什么情况的。viewpage是横向的。
PHP中文网2017-04-17 17:49:46
Because ViewPager.onInterceptTouchEvent()
will intercept horizontal sliding under normal circumstances, so your picture will not receive the MotionEvent.ACTION_MOVE
action.ViewPager.onInterceptTouchEvent()
在一般情况下会拦截横向的滑动, 所以你的图片收不到MotionEvent.ACTION_MOVE
动作.
解决思路大致为:
在ViewPager.onInterceptTouchEvent()
的MotionEvent.ACTION_MOVE
下有一下代码可以不拦截横向滑动
if (dx != 0 && !isGutterDrag(mLastMotionX, dx) &&
canScroll(this, false, (int) dx, (int) x, (int) y)) {
// Nested view has scrollable area under this point. Let it be handled there.
mLastMotionX = x;
mLastMotionY = y;
mIsUnableToDrag = true;
return false;
}
只要你能执行这段代码, ViewPager就不会拦截这次的横向滑动, 关键的方法应该是canScroll()
源码如下
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);
}
大致就是判断里面的子控件是否可以横向滑动
因为你里面最后是一个ImageView
, 所以最后关键就是ViewCompat.canScrollHorizontally(v, -dx);
当v时ImageView
的时候要返回true
如果你使用的版本较高的话, 实际执行的就是ImageView.canScrollHorizontally()
方法, 这里面的代码很简单, 就不贴出来了, 这个方法直接继承了View.canScrollHorizontally()
, 返回的是false
, 所以解决方案想到2个
用一个可以滑动的控件作为ImageView
的父控件
重写ImageView.canScrollHorizontally()
, 令到它返回true
The solution is roughly as follows:
ViewPager.onInterceptTouchEvent()
, there is the following code under MotionEvent.ACTION_MOVE
to prevent horizontal sliding canScroll()
The source code is as follows#🎜🎜# rrreee #🎜🎜#Roughly, it is to determine whether the sub-control inside can slide horizontally #🎜🎜#Because you have an
ImageView
at the end, so the final key is #🎜🎜#ViewCompat.canScrollHorizontally(v , -dx);
When v is ImageView
, return true
#🎜🎜#
#🎜🎜#If you are using a higher version, what is actually executed is the ImageView.canScrollHorizontally()
method. The code here is very simple, so I won’t post it. This method directly inherits < code>View.canScrollHorizontally(), returns false
, so I can think of two solutions#🎜🎜#
ImageView
#🎜🎜##🎜🎜#
ImageView.canScrollHorizontally()
so that it returns true
#🎜🎜#Recommended method 1#🎜🎜##🎜🎜#
#🎜🎜#
#🎜🎜#The above analysis did not write code verification, there may be errors, please correct me#🎜🎜#