首页 > 解决方案 > 重新启用滚动后,水平 RecyclerView 不会向左滚动

问题描述

重新启用滚动时,具有自定义视图的回收器视图停止向左滚动。

基于业务逻辑,我禁用了滚动,但是当用户滑动到可配置的距离时,我启用了滚动(x 上为 100px)。如果用户执行 LTR,则滚动很好,但如果完成 RTL,则不会发生任何事情。

recyclerview 有三个视图。V1、V2 和 V3,但为了使 recyclerview 成为轮播,我在第一个索引处添加 V3,在最后一个索引处添加 V1,如下所示V3, V1, V2, V3, V1。这样,当用户到达最后一个可见视图(V3)时,进一步滑动会显示第一个视图(V1)

 list = listOf(scrollableData.last()) + scrollableData + listOf(
            scrollableData.first()
        )

上述逻辑适用于 V1 和 V2,但不适用于 V3,我认为这会导致左滚动禁用。我可能错了。除了我上面指定的之外,我正在做任何其他事情。

家活动

gestureDetector = GestureDetector(this@HomeActivity, object : CustomGestureDetector(recycler_view) {
            override fun move(): Boolean {
                enableScroll()
                return false
            }

            override fun down(): Boolean {
                disableScroll()
                return false
            }

        })
        recycler_view.setOnTouchListener { v, event ->
            if (event != null) {
                gestureDetector.onTouchEvent(event)
            }
            false
        }

    override fun enableScroll() {
        linearLayoutManager.isScrollEnabled = true
    }

    override fun disableScroll() {
        linearLayoutManager.isScrollEnabled = false
    }

自定义手势检测器

public abstract class CustomGestureDetector(view: View) : GestureDetector.SimpleOnGestureListener() {

    var view = view

    override fun onDown(e: MotionEvent?): Boolean {
        view.onTouchEvent(e)
        down()
        return false
    }

    override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
        if (Math.abs(e2.x - e1.x) > 500) {
            move()
        }
        return false
    }

    abstract fun move(): Boolean
    abstract fun down(): Boolean
}

我希望我已经提供了足够的信息来理解我面临的问题。

标签: androidandroid-recyclerview

解决方案


而不是在recyclerview. 我在那里创建了一个自定义RelativeLayout和实现的逻辑,并为每个项目使用了这个布局。

    class CustomRelativeLayot: RelativeLayout{
        ...
        override fun onTouchEvent(motionEvent: MotionEvent): Boolean {
                when (motionEvent.action) {
                    MotionEvent.ACTION_DOWN -> {
pressedX = motionEvent.x
                disableScreenScroll()
                pressedTime = System.currentTimeMillis()
}
          }
        MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(motionEvent.x - pressedX) > scrollSensitivity && isSwipable) {
    ...
                    }
                }
                MotionEvent.ACTION_UP -> {}
    }
    return true
    }

项目.xml

<com...CustomRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/airModuleLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/temp_height"
    android:orientation="vertical"
    app:isSwipable="true">
..
</com...CustomRelativeLayout>

我希望这可以帮助其他面临同样问题的人。


推荐阅读