美文网首页
2020-06-07-Android-滑动冲突

2020-06-07-Android-滑动冲突

作者: 耿望 | 来源:发表于2020-06-15 19:23 被阅读0次

    滑动(平移)的实现方式

    首先看一下有几种方式可以实现滑动效果,实际也就是平移。

    通过layout实现

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastX = x;
                    lastY = y;
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_MOVE:
                    int offsetX = (int) (x - lastX);
                    int offsetY = (int) (y - lastY);
                    layout(getLeft() + offsetX, getTop() + offsetY,
                            getRight() + offsetX, getBottom() + offsetY);
                    break;
            }
            return true;
        }
    

    也可以简单封装成一个方法,实现X轴方向上的平移:

        private void translateWithLayout(int deltaX) {
            layout(getLeft() - deltaX, getTop(),
                    getRight() - deltaX, getBottom());
        }
    

    通过offset方法实现

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastX = x;
                    lastY = y;
                    Log.d(TAG, "onTouchDown " + x + " " + y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int offsetX = (int) (x - lastX);
                    int offsetY = (int) (y - lastY);
                    offsetLeftAndRight(offsetX);
                    offsetTopAndBottom(offsetY);
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d(TAG, "onTouchUp " + x + " " + y);
                    break;
            }
            return true;
        }
    

    可以封装成一个方法,实现X轴方向的平移:

        private void translateWithOffset(int deltaX) {
            offsetLeftAndRight(deltaX);
        }
    

    通过LayoutParams实现

        private void translateWithParams(int deltaX) {
            MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
            layoutParams.leftMargin = getLeft() - deltaX;
            setLayoutParams(layoutParams);
        }
    

    滑动冲突的解决方式

    实际上所有的滑动冲突,都是两种解决思路,外部拦截和内部拦截。
    下面举了一个实际例子,是在ScrollView的外部添加一个横向滑动效果。
    布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.one.view.OutsideView
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/outside"
            android:layout_marginTop="40dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ScrollView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="100dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:textSize="30sp"
                    android:text="HelloWorld1\nHelloWorld2\nHelloWorld3\nHelloWorld4\n"/>
            </ScrollView>
        </com.one.view.OutsideView>
    
        <com.one.view.InsideView
            app:layout_constraintTop_toBottomOf="@+id/outside"
            android:layout_marginTop="40dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <com.one.view.MyScrollView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="100dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textAlignment="center"
                    android:textSize="30sp"
                    android:text="HelloWorld1\nHelloWorld2\nHelloWorld3\nHelloWorld4\n"/>
            </com.one.view.MyScrollView>
        </com.one.view.InsideView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    实现了一个这样的效果:


    t1.png

    外部拦截

    外部拦截是通过重写父容器的onInterceptTouchEvent方法,根据需要返回是否拦截的决定。

        // intercept from outside
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            boolean intercepted = false;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastX = x;
                    lastY = y;
                    intercepted = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int deltaX = lastX - x;
                    int deltaY = lastY - y;
                    intercepted = (Math.abs(deltaX) - 10 > 0) && (Math.abs(deltaY) - 10 < 0);
                    break;
            }
            Log.d(TAG, "onInterceptTouchEvent " + intercepted
                    + " x=" + event.getX() + " y=" + event.getY());
            return intercepted;
        }
    

    接着在onTouchEvent方法中实现横向滑动效果:

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                int deltaX = lastX - x;
                int deltaY = lastY - y;
                Log.d(TAG, "onTouchEvent x=" + deltaX + " y=" + deltaY);
                layout(getLeft() - deltaX, getTop(),
                        getRight() - deltaX, getBottom());
            }
            return true;
        }
    

    内部拦截

    内部拦截方式稍微比较复杂,需要在父容器和子view中都添加逻辑代码。
    首先在父容器中,对除了ACTION_DOWN之外的所有事件进行拦截。

        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
    
            int action = event.getAction();
            int x = (int) event.getX();
            int y = (int) event.getY();
            if (action == MotionEvent.ACTION_DOWN) {
                lastX = x;
                lastY = y;
                Log.d(TAG, "onInterceptTouchEvent ACTION_DOWN");
            }
    
            return (action != MotionEvent.ACTION_DOWN);
        }
    

    接着在onTouchEvent方法中实现一个横向平移。

        @Override
        public boolean onTouchEvent(MotionEvent event) {
    
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
    
                int x = (int) event.getX();
                int y = (int) event.getY();
                int deltaX = lastX - x;
                int deltaY = lastY - y;
    
                Log.d(TAG, "onTouchEvent x=" + deltaX + " y=" + deltaY);
    
                translateWithParams(deltaX);
    
            }
    
            return true;
        }
    

    比较复杂的逻辑是需要在子view中实现一个是否禁止拦截的判断:

        // intercept from inside
        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
    
            int x = (int) event.getX();
            int y = (int) event.getY();
            boolean intercepted = false;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    lastX = x;
                    lastY = y;
                    intercepted = false;
                    getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int deltaX = lastX - x;
                    int deltaY = lastY - y;
                    intercepted = (Math.abs(deltaX) - 10 > 0) && (Math.abs(deltaY) - 10 < 0);
                    if (intercepted) {
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    break;
                default:
                    break;
    
            }
            Log.d(TAG, "dispatchTouchEvent " + intercepted
                    + " x=" + event.getX() + " y=" + event.getY());
            return super.dispatchTouchEvent(event);
        }
    

    参考

    ViewPager的简单使用
    Android View体系(二)实现View滑动的六种方法

    相关文章

      网友评论

          本文标题:2020-06-07-Android-滑动冲突

          本文链接:https://www.haomeiwen.com/subject/jbnqtktx.html