使用FocusLayoutManager实现每次滚动一个项的RecyclerView捕捉滚动。

huangapple go评论66阅读模式
英文:

Snapping RecyclerView for scrolling one item at a time, using FocusLayoutManager

问题

我正在使用 FocusLayoutManager 来处理 RecyclerView。一切都正常,但我需要一次只能滑动一个项目。我还尝试过 LinearSnapHelper 和其他类似的,比如 SnapHelperOneByOne,但都没有帮助。我还试图修改焦点布局管理器类本身,但都徒劳无功。任何帮助将不胜感激。

  1. FocusLayoutManager 链接

  2. SnapHelperOneByOne 链接

  3. FocusLayoutManagerLibrary 链接

注:我已经尝试在分配布局管理器之后,将 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.

  1. Link for FocusLayoutManager

  2. Link for SnapHelperOneByOne

  3. 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.

huangapple
  • 本文由 发表于 2020年9月1日 19:08:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63686470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定