【问题标题】:Touch Event Interception when using ViewPager + NestedScrollView + RecyclerView使用 ViewPager + NestedScrollView + RecyclerView 时的触摸事件拦截
【发布时间】:2021-05-02 05:58:36
【问题描述】:

我使用 ViewPager 来浏览页面,每个页面都包含一个 NestedScrollView 和一个 RecyclerView。 NestedScrollView 用于在分屏模式下滚动图表。

fragment_accounts:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/mAccountsProgressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/mAccountsScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mAccountsProgressbar">

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

            <at.guger.moneybook.core.ui.widget.StrokePieChart
                android:id="@+id/mAccountsPieChart"
                android:layout_width="match_parent"
                android:layout_height="@dimen/default_chart_height"
                app:accounts="@{viewModel.accounts}"
                app:bold="true"
                app:fontResId="@font/eczar_regular"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/mAccountsProgressbar"
                app:textSize="42sp"
                tools:text="$ 1234,56" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/mAccountsRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                tools:itemCount="4"
                tools:listitem="@layout/item_account" />

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

现在,我正在使用 OnTouchListener 来检测 RecyclerView 中的点击。当在 ViewPager 中的页面之间滑动时,RecyclerView 还会检测到长按 RecyclerView 中的某个项目。 如何确保 RecyclerView 在滑动到下一页时不使用触摸事件?

澄清:我想接收 RecyclerView 中项目的长按事件,但我不想在用户在 ViewPager 的页面之间滑动时接收它们!

OnItemTouchListener:

class OnItemTouchListener(context: Context, recyclerView: RecyclerView, private val onTouchCallback: ItemTouchListener) : RecyclerView.OnItemTouchListener {

    //region Variables

    private val gestureDetector: GestureDetectorCompat

    //endregion

    init {
        gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
            private val MIN_SWIPE_DISTANCE: Int = 50
            private lateinit var downMotionEvent: MotionEvent

            override fun onDown(e: MotionEvent?): Boolean {
                e?.let { downMotionEvent = it }

                return super.onDown(e)
            }

            override fun onSingleTapUp(e: MotionEvent?): Boolean {
                return true
            }

            override fun onLongPress(e: MotionEvent?) {
                e?.let {
                    val child: View? = recyclerView.findChildViewUnder(it.x, it.y)

                    if (child != null && !isGestureSwipe(it)) {
                        onTouchCallback.onItemLongClick(child, recyclerView.getChildLayoutPosition(child), it)
                    }
                }

                super.onLongPress(e)
            }

            fun isGestureSwipe(e: MotionEvent): Boolean {
                return downMotionEvent.x - e.x >= MIN_SWIPE_DISTANCE
            }
        })
    }

    //region TouchHandler

    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        val childView = rv.findChildViewUnder(e.x, e.y)

        if (childView != null && gestureDetector.onTouchEvent(e)) {
            onTouchCallback.onItemClick(childView, rv.getChildLayoutPosition(childView), e)
        }

        return false
    }

    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {

    }

    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {

    }

    //endregion

    interface ItemTouchListener {
        fun onItemClick(view: View, pos: Int, e: MotionEvent)
        fun onItemLongClick(view: View, pos: Int, e: MotionEvent)
    }

    companion object {

        fun isViewClicked(container: View, @IdRes viewId: Int, e: MotionEvent): Boolean {
            val view = container.findViewById<View>(viewId)

            return isViewClicked(view, e)
        }

        fun isViewClicked(view: View, e: MotionEvent): Boolean {
            val rect = Rect()
            view.getGlobalVisibleRect(rect)

            return view.isVisible && rect.contains(e.rawX.toInt(), e.rawY.toInt())
        }
    }
}

Parent Fragment,其中嵌入了 fragment_accounts:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/mHomeConstraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/mHomeViewPager"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/mHomeTabs" />

        <at.guger.moneybook.core.ui.widget.LabelVisibilityTabLayout
            android:id="@+id/mHomeTabs"
            style="@style/Widget.MaterialComponents.TabLayout"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:backgroundTint="@android:color/transparent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabIconTint="@color/tab_highlight_selector"
            app:tabIndicator="@null"
            app:tabInlineLabel="true"
            app:tabMode="scrollable"
            app:tabRippleColor="@color/colorRippleMaterialDark"
            app:tabTextColor="@color/tab_highlight_selector" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabHomeAddTransaction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:backgroundTint="?colorPrimary"
        android:clickable="true"
        android:contentDescription="@string/AddTransaction"
        android:focusable="true"
        android:src="@drawable/ic_add"
        app:layout_anchor="@+id/mHomeConstraintLayout"
        app:layout_anchorGravity="bottom|end"
        app:tint="?colorOnPrimary" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

所有其他代码都可以在 Github (https://github.com/guger/MoneyBook) 上找到。

【问题讨论】:

  • 只是为了澄清这个问题。您在问如何避免从回收视图中长按拦截?并将其进一步传播到嵌套视图。对吗?
  • 或多或少:我的问题是,viewpager 拦截了滑动(正确),但有时,RecyclerView 也将其解释为对项目的长按。
  • 好的。对于长按案例,我知道该怎么做。当您获得onLongPressEvent 时,您能否尝试在您的方法的顶部调用requestDisallowInterceptTouchEvent。
  • 你的意思是在 OnTouchListener 中吗?
  • 是的。在 onTouchListener 中。不管你怎么在不同的地方调用它,它都会从父级释放触摸事件。

标签: android kotlin android-recyclerview android-viewpager android-nestedscrollview


【解决方案1】:

可能使用焦点可能会有所帮助,我的意思是,如果您要滑动,然后从 RecyclerView 清除焦点,如果没有焦点,您现在可以确定事件 onTouch () 不会被调用,否则你会为 RecyclerView 调用它。

否则我不知道如何让它工作,因为或者你滑动或者你在LongPress上

【讨论】:

  • 但是当我开始在 Viewpager 中滑动时,如何从 RecyclerView 中移除焦点?
  • android:focusable="false" android:focudeableInTouchMode="false"
  • 这并没有改变触摸/点击检测的行为。
【解决方案2】:

由于我在OnItemTouchListener 中找不到错误,我分别使用View.OnClickListener 和OnLongClickListener 重写了点击/长点击检测。

作为一种解决方法,这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多