gpt4 book ai didi

java - smoothScrollTo(position) 滚动到底部而不是 RecyclerView 中的目标位置

转载 作者:行者123 更新时间:2023-12-02 01:39:49 25 4
gpt4 key购买 nike

当我按下按钮时,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)
}

最佳答案

这与我重写 LinearLayoutManager 的方式有关。

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

它有效。

关于java - smoothScrollTo(position) 滚动到底部而不是 RecyclerView 中的目标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57494934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com