search

Home  >  Q&A  >  body text

android - MotionEvent.ACTION_MOVE 在onTouchEvent返回false情况下事件仍然上传

viewpage里面放了一个imageview,MotionEvent.ACTION_MOVE里面设置imageview随着手指滑动,onTouchEvent是返回了true。纵向滑动没有问题,横向滑动会出现滑动一点点距离就划不动了,viewpage开始滑动。这是什么情况的。viewpage是横向的。

PHP中文网PHP中文网2773 days ago722

reply all(1)I'll reply

  • PHP中文网

    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个

    1. 用一个可以滑动的控件作为ImageView的父控件

    2. 重写ImageView.canScrollHorizontally(), 令到它返回true
      The solution is roughly as follows:

      Under ViewPager.onInterceptTouchEvent(), there is the following code under MotionEvent.ACTION_MOVE to prevent horizontal sliding
    3. rrreee
    As long as you can execute this code, ViewPager will not intercept this horizontal sliding. The key method should be 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#🎜🎜#
    1. #🎜🎜#Use a sliding control as the parent control of ImageView#🎜🎜##🎜🎜#
    2. #🎜🎜#Rewrite 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#🎜🎜#

      reply
      0
  • Cancelreply