【问题标题】:smoothScrollTo(position) scrolls to bottom instead of targeted position in RecyclerViewsmoothScrollTo(position) 滚动到底部而不是 RecyclerView 中的目标位置
【发布时间】:2019-08-17 20:48:02
【问题描述】:

当我按下按钮时,我在 Kotlin 中的垂直 RecyclerView(看起来像一个简单的列表)会整齐地滚动到下一个元素。 ?????? 但是,当下一个元素不在屏幕上时(例如,因为同时用户用手势滚动到不同的位置),问题是这不再起作用了。相反,它会自动滚动到 RecyclerView 的底部。

知道如何解决这个问题吗?我会很感激的! 提前感谢您的努力。

我正在覆盖 SmoothScrollLinearLayoutManager:

class SmoothScrollLinearLayoutManager(private val mContext: Context, orientation: Int, reverseLayout: Boolean) : LinearLayoutManager(mContext, orientation, reverseLayout) {

    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?,
                                        position: Int) {
        val smoothScroller = object : TopSnappedSmoothScroller(recyclerView.context) {
            //This controls the direction in which smoothScroll looks for your view
            override fun computeScrollVectorForPosition(targetPosition: Int): PointF {
                return PointF(0f, 1f)
            }

            //This returns the milliseconds it takes to scroll one pixel.
            protected override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                return MILLISECONDS_PER_INCH / displayMetrics.densityDpi
            }
        }
        smoothScroller.targetPosition = position
        Log.i("Target", smoothScroller.targetPosition.toString())
        startSmoothScroll(smoothScroller)
    }


    private open inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {

        override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
            return this@SmoothScrollLinearLayoutManager
                    .computeScrollVectorForPosition(targetPosition)
        }

        override fun getVerticalSnapPreference(): Int {
            return LinearSmoothScroller.SNAP_TO_START
        }
    }

    private open inner class CenterSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {

        override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
            return this@SmoothScrollLinearLayoutManager
                    .computeScrollVectorForPosition(targetPosition)
        }

        override fun getVerticalSnapPreference(): Int {
            return LinearSmoothScroller.SNAP_TO_END
        }
    }

    // Scrolling speed
    companion object {
        private val MILLISECONDS_PER_INCH = 110f
    }
}

按钮触发此功能滚动到列表中的下一个元素(RecyclerView):

private fun fastForwardTapped() {

        // Update selected position in RecyclerView
            selectedPosition += 1

        recyclerView.smoothScrollToPosition(selectedPosition)
    }

【问题讨论】:

    标签: java android kotlin android-recyclerview


    【解决方案1】:

    这与我如何覆盖 LinearLayoutManager 有关。

    我最终使用了这个解决方案: https://stackoverflow.com/a/36784136/8400139

    有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多