使用ViewPager + NestedScrollView + RecyclerView时的触摸事件拦截

时间:2021-01-28 10:40:13

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

我使用 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())
        }
    }
}

父片段,其中嵌入了 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) 上找到。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

由于我在 OnItemTouchListener 中找不到错误,我分别使用 View.OnClickListenerOnLongClickListener 重写了 Click-/Long-Click-Detection。

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