英文:
Snapping RecyclerView for scrolling one item at a time, using FocusLayoutManager
问题
我正在使用 FocusLayoutManager
来处理 RecyclerView
。一切都正常,但我需要一次只能滑动一个项目。我还尝试过 LinearSnapHelper
和其他类似的,比如 SnapHelperOneByOne
,但都没有帮助。我还试图修改焦点布局管理器类本身,但都徒劳无功。任何帮助将不胜感激。
注:我已经尝试在分配布局管理器之后,将 SnapHelper
附加到 RecyclerView
。
val focusLayoutManager = FocusLayoutManager.Builder()
.focusOrientation(FocusLayoutManager.FOCUS_TOP)
.isAutoSelect(true)
.maxLayerCount(1)
.setOnFocusChangeListener { focusdPosition, lastFocusdPosition -> }
.build()
myRecycler.layoutManager = focusLayoutManager
使用 Snap Helper 也没有帮助,而且会导致滚动出现卡顿。
val mySnapHelper = SnapHelperOneByOne()
mySnapHelper.attachToRecyclerView(myRecycler)
英文:
I am using FocusLayoutManager
for RecyclerView
. Everything is fine but I need to swipe only one item at a time. I have also tried LinearSnapHelper
and others too, i.e. SnapHelperOneByOne
but didn't helped. Also tried to modify the focus layout manager class itself but all in vain. Any help would be appreciated.
-
Link for FocusLayoutManager
-
Link for SnapHelperOneByOne
-
Link for FocusLayoutManagerLibrary
Note: I have tried to attach the SnapHelper
to RecyclerView
after layout manager is assigned.
val focusLayoutManager = FocusLayoutManager.Builder()
.focusOrientation(FocusLayoutManager.FOCUS_TOP)
.isAutoSelect(true)
.maxLayerCount(1)
.setOnFocusChangeListener { focusdPosition, lastFocusdPosition -> }
.build()
myRecycler.layoutManager = focusLayoutManager
Using Snap Helper doesn't help also and makes the scrolling lagging (jerks)
val mySnapHelper = SnapHelperOneByOne()
mySnapHelper.attachToRecyclerView(myRecycler)
答案1
得分: 5
遇到了相同的问题。将焦点变化监听器代码设置如下。(针对 Java 找到了一个解决方法)
.setOnFocusChangeListener((focusdPosition, lastFocusedPosition) -> {
if (focusdPosition == lastFocusedPosition - 1 || focusdPosition == lastFocusedPosition + 1) {
myRecycler.stopScroll();
}
})
这段代码将会在 RecyclerView 中停止滚动,如果当前焦点项在前一个焦点项之后或之前,那么它将停止滚动。
移除 SnapHelper,因为它会导致 FocusLayoutManager 出现滚动动画延迟。
英文:
Got stuck with the same issue. Set the focus change listener code like below. (Found one work around for Java)
.setOnFocusChangeListener((focusdPosition, lastFocusedPosition) -> {
if (focusdPosition == lastFocusedPosition - 1 || focusdPosition == lastFocusedPosition + 1) {
myRecycler.stopScroll();
}
})
This code will stop the scroll for the RecyclerView, if the current focused item is after or before the previous focused item then it will stop the scroll.
Remove the SnapHelper because it creates a scroll animation lag with FocusLayoutManager.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论