RecyclerView中类似寻呼机的行为 [英] Pager-like behavior in RecyclerView
问题描述
我正在尝试为水平RecyclerView
实现类似ViewPager的行为.来自适配器的数据应正常进行充气和绑定,但是通过Recycler
进行的导航应以不同的方式进行处理.当用户滑动(或尝试滚动)时,我沿该方向移动Recycler
一项,并将其粘贴到左侧.
I am trying to implement a ViewPager-like behavior for a horizontal RecyclerView
. The data from the adapter should inflate and bind as normal, but the navigation through the Recycler
should be handled differently. When the user swipes (or attempts to scroll), I move the Recycler
one item in that direction, sticking it to the left side.
我已经拥有所有项目转换逻辑.我使用的是自定义LayoutManager
,它会用自定义LinearSmoothScroller()
覆盖onSmoothScrollToPosition()
,该自定义LinearSmoothScroller()
会将项目粘贴到左侧.
I already have all the item transition logic. I am using a custom LayoutManager
, which overrides onSmoothScrollToPosition()
with a custom LinearSmoothScroller()
which does the item to-the-left sticking.
问题是-如何覆盖RecyclerView
的滚动行为以拦截刷卡并自己处理?我尝试禁用LayoutManager
中的滚动,然后在onTouchListener
中拦截手势,但这似乎不起作用. RecyclerView
框架是否有解决此问题的干净方法?
The question is - how can I override the scrolling behavior of the RecyclerView
to intercept the swipes and handle them myself? I tried disabling scrolling in the LayoutManager
and then intercepting the gesture in an onTouchListener
, but this does not seem to work. Does the RecyclerView
framework have a clean way to handle this?
推荐答案
这是使RecyclerView表现得像ViewPager一样简单的方法.
The native Android solution covered in this post was a super easy way to get the RecyclerView to behave like a ViewPager.
相关位:
24.2.0版的支持库引入了两个新类(SnapHelper和LinearSnapHelper),这些类应用于处理RecyclerView中的捕捉. .... 唯一需要的代码是:
The 24.2.0 version of the support library introduced two new classes (SnapHelper and LinearSnapHelper) that should be used to handle snapping in a RecyclerView. .... The only code needed is:
SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
snapHelper.attachToRecyclerView(startRecyclerView);
SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
snapHelper.attachToRecyclerView(startRecyclerView);
这篇关于RecyclerView中类似寻呼机的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!