gpt4 book ai didi

android - 如何捕捉到水平 RecyclerView 中 LinearSnapHelper 的特定位置?

转载 作者:行者123 更新时间:2023-11-29 01:00:37 25 4
gpt4 key购买 nike

如何在水平 RecyclerView 中捕捉到 LinearSnapHelper() 的特定位置? RecyclerView 有一个函数 scrolltoposition,它滚动到那个位置,但没有将它保持在这个 snaphelper 的中心。

我正在寻找类似下图的内容。所以当我设置到特定位置时,它会保持在中心位置。我没有找到任何与选择位置相关的信息 SnapHelper

我发现 this ,但这对我没有帮助。任何帮助将不胜感激。

enter image description here

最佳答案

Add this where ever you want to scroll your recycler view

 recyclerView.scrollToPosition(position)
recyclerView.post {
var view = recyclerView.layoutManager?.findViewByPosition(position);
if (view == null) {
// do nothing
}

var snapDistance = snapHelper.calculateDistanceToFinalSnap(recyclerView.layoutManager!!, view!!)
if (snapDistance?.get(0) != 0 || snapDistance[1] != 0) {
recyclerView.scrollBy(snapDistance?.get(0)!!, snapDistance?.get(1));
}
}

By using this LinearSnap Helper which is attached to your recycler view

  var snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)

关于android - 如何捕捉到水平 RecyclerView 中 LinearSnapHelper 的特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51495651/

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