SnapHelper Item Position

后端 未结 3 828
半阙折子戏
半阙折子戏 2020-12-24 08:37


I\'m using vertical RecyclerView to list my items and SnapHelper to snap center item. The idea is to randomize selection, so user swipe screen

3条回答
  •  北海茫月
    2020-12-24 09:31

    I try to use this code with a PagerSnapHelper to mimic the pager behaviour and it was useful but i found some corner cases to solve, if you move fast from the last page to the first one and keep swapping until see the boundarie then the IDLE state doesnt happen and you lose your index. to solve that I move out the position from the IF and add a extra condition for this corner case.

     override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                 super.onScrollStateChanged(recyclerView, newState)
                 val centerView = snapHelper.findSnapView(mLayoutManager)
                 val pos = mLayoutManager.getPosition(centerView!!)
                 if (newState == RecyclerView.SCROLL_STATE_IDLE || (pos == 0 && newState == RecyclerView.SCROLL_STATE_DRAGGING)) {
                     Log.d("BINDING", "positionView SCROLL_STATE_IDLE: $pos")
                 }
             }
    

    Code is in kotlin hope it helps

提交回复
热议问题