滑动冲突Demo 全代码版

Posted wangweizu99

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了滑动冲突Demo 全代码版相关的知识,希望对你有一定的参考价值。

滑动冲突Demo 全代码版

学习过程讲解见:https://blog.csdn.net/weixin_50738670/article/details/119650634?spm=1001.2014.3001.5501

父View:

public class MyParentView extends LinearLayout 
    private int mMove;
    private int yDown, yMove;
    private int i = 0;
    public MyParentView(Context context, AttributeSet attrs)
        super(context, attrs);
    
    @Override
    public boolean onTouchEvent(MotionEvent event) 
        int y = (int) event.getY();
        switch (event.getAction())
            case MotionEvent.ACTION_DOWN:
                yDown = y;
                Log.i("down", "down: "+yDown);
                break;
            case MotionEvent.ACTION_MOVE:
                yMove = y;
                if((mMove=yMove-yDown)>0&&i<100)
                    mMove = mMove>100?100:mMove;
                    i += mMove;

                    layout(getLeft(),getTop()+mMove,getRight(),getBottom()+mMove);
                
                Log.i("down", "Move: "+i+"     "+y);
                break;
            case MotionEvent.ACTION_UP:
                layout(getLeft(),getTop()-i,getRight(),getBottom()-i);
                i=0;break;
            default:break;
        
        return true;
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) 
        if(event.getAction()==MotionEvent.ACTION_DOWN)
            return false;
        
        return true;
    

子View:

public class MyNestedScrollView extends NestedScrollView 
    public MyNestedScrollView(@NonNull Context context) 
        super(context);
    

    public MyNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) 
        super(context, attrs);
    

    public MyNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) 
        super(context, attrs, defStyleAttr);
    
    public static int a;
    private int mMove;
    private int yDown, yMove;
    private int i = 0;
    @Override
    public boolean onTouchEvent(MotionEvent ev) 
        int y = (int) ev.getY();
        switch (ev.getAction())
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                yDown = y;
                Log.i("down11", "down: "+yDown);
                return true;
            case MotionEvent.ACTION_MOVE:
                //判断是否在子布局顶部
                if (getScrollY()==0&&yDown<y)
                    //允许父布局进行拦截
                    getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                else
                    //不允许父布局进行拦截,自身进行处理
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return super.onTouchEvent(ev);
                

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(true);
                break;

            default:break;

        
        return false;
    

布局文件XML:

<?xml version="1.0" encoding="utf-8"?>
<com.example.android.touchevent.MyParentView 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.example.android.touchevent.MyNestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:background="#005555"
                android:text="Hello World!"
                android:gravity="center"
                android:textSize="200sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:background="#F44336"
                android:text="Hello World!"
                android:gravity="center"
                android:textSize="200sp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:background="#03A9F4"
                android:text="Hello World!"
                android:gravity="center"
                android:textSize="200sp"/>

        </LinearLayout>
    </com.example.android.touchevent.MyNestedScrollView>
</com.example.android.touchevent.MyParentView>


以上是关于滑动冲突Demo 全代码版的主要内容,如果未能解决你的问题,请参考以下文章

滑动冲突Demo 全代码版

滑动冲突Demo 全代码版

Android嵌套滑动控件的冲突解决和ViewPager适配当前子控件高度不留空白的办法

2 UIPageViewController 手势冲突

NestedScrollView嵌套ListView滑动冲突

iOS8 UICollectionView横向滑动demo