RecycleView嵌套RecycleView竖向滑动冲突解决

重写子RecycleView

代码如下:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.recyclerview.widget.RecyclerView;

import com.xiaoniu.hulumusic.utils.Apputils;

public class NestChildRecyclerView extends RecyclerView {

    public NestChildRecyclerView(Context context) {
        this(context, null);
    }

    public NestChildRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NestChildRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                Log.d("test","---onInterceptTouchEvent---ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("test","---onInterceptTouchEvent---ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Log.d("test","---onInterceptTouchEvent---ACTION_CANCEL");
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
 
}

布局如下:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingHorizontal="@dimen/dp_15">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:lines="1"
            android:ellipsize="end"
            android:textColor="@color/dark_text"
            tools:text="test"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBaseline_toBaselineOf="@id/tv_search_history"
            android:text="demo"/>

        <com.demo.NestChildRecyclerView
            android:id="@+id/recycler_view"
            android:layout_marginTop="10dp"
            app:layout_constraintTop_toBottomOf="@id/tv_search_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.constraintlayout.widget.ConstraintLayout>

注意:

处于父RecyclerView下的子RecyclerView的父布局高一定要设置为match_parent;不然无法滑动

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值