同级视图可防止recyclerview滚动成可点击状态

我的屏幕由一个ConstraintLayout和两个孩子组成。一个孩子是一个RecyclerView,它占据了整个屏幕。另一个孩子是另一个ConstraintLayout,它包含一个TextView(屏幕实际上比这更复杂,但是我将其缩小为此处的内容以便于理解)。子ConstraintLayout放置在RecyclerView上方,并在您滚动RecyclerView时向上滚动。这是通过在RecyclerView上使用滚动侦听器并重新调整子ConstraintLayout的Y位置来完成的。子ConstraintLayout必须是可点击的。

我遇到的问题是,如果您尝试通过先按下子ConstraintLayout来滚动RecyclerView,则滚动将无法进行。但是,子ConstraintLayout的click事件处理程序将起作用。

根据我对Android如何处理触摸事件的理解,如果视图在其onTouchEvent处理函数中返回false,则该视图将不会收到任何其他运动事件的通知。因此,从理论上讲,如果我的子ConstraintLayout在其onTouchEvent处理函数中返回false,则RecyclerView仍应获取滚动运动事件。

这是我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:background="#55338855">

    <androidx.constraintlayout.motion.widget.MotionLayout
        android:id="@+id/dashboardConstraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layoutDescription="@xml/dashboard_initial_animation_start"
        app:showPaths="false">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/contentRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:clipChildren="false"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/guideWelcome"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="1.0" />

        <ReservationCard
            android:id="@+id/reservationConstraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="60dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="16dp"
            android:background="@color/dashboard_reservation_card_background_color"
            android:padding="16dp"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/txtSearch">

            <TextView
                android:id="@+id/txtReservationHeader"
                style="@style/Apptheme.Label"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/dashboard_screen_reservation_card_headline"
                android:textColor="@color/dashboard_reservation_card_text_color" />

        </com.sinnerschrader.motelone.screens.dashboard.ReservationCard>

    </androidx.constraintlayout.motion.widget.MotionLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我自定义的ConstraintLayout(ReservationCard):

class ReservationCard : androidx.constraintlayout.widget.ConstraintLayout {

    constructor(context: Context?) : super(context)
    constructor(context: Context?,attrs: AttributeSet?) : super(context,attrs)
    constructor(context: Context?,attrs: AttributeSet?,defStyleAttr: Int) : super(context,attrs,defStyleAttr)

    private val gestureDetector: GestureDetector

    init {
        gestureDetector = GestureDetector(GestureListener())
    }

    override fun onTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.actION_DOWN) {
            gestureDetector.onTouchEvent(ev)
            return true
        } else if (ev?.action == MotionEvent.actION_UP) {
            gestureDetector.onTouchEvent(ev)
            return false
        } else {
            super.onTouchEvent(ev)
            return false
        }
    }

    private inner class GestureListener : GestureDetector.OnGestureListener {
        override fun onfling(p0: MotionEvent?,p1: MotionEvent?,p2: Float,p3: Float): Boolean {
            return false
        }

        override fun onScroll(p0: MotionEvent?,p3: Float): Boolean {
            return false
        }

        override fun onLongPress(p0: MotionEvent?) {
        }

        override fun onDown(p0: MotionEvent?): Boolean {
            return false
        }

        override fun onShowPress(p0: MotionEvent?) {
        }

        override fun onSingletapUp(p0: MotionEvent?): Boolean {
            return false
        }
    }
}
xiejy1 回答:同级视图可防止recyclerview滚动成可点击状态

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160617.html

大家都在问