SnapHelper系列之入门

SnapHelper简介

SnapHelper简介

SnapHelper类是在支持包24.2.0版本(com.android.support:recyclerview)中添加的,其作用于RecyclerView在Fling状态下滑动到某一特定位置。

官方为我们提供了SnapHelper的两个子类,分别为LinearSnapHelper,PagerSnapHelper。

LinearSnapHelper

The implementation will snap the center of the target child view to the center of the attached RecyclerView.

RecyclerView滚动停止时,相应的子类停留在RecyclerView滚动停止时中间位置

PaperSnapHelper

Implementation of the SnapHelper supporting pager style snapping in either vertical or horizontal orientation.
PagerSnapHelper can help achieve a similar behavior to ViewPager. Set both RecyclerView and the items of the RecyclerView.Adapter to have MATCH_PARENT height and width and then attach PagerSnapHelper to the RecyclerView using attachToRecyclerView(RecyclerView).

RecyclerView实现类似ViewPager的滑动效果,实现单页滚动

用法

用法其实很简单,创建SnapHelper子类的实例,调用attachToRecyclerView()方法将RecyclerView和SnapHelper子类关联在一起。

LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(mLinearSnapRecyclerView);
//或者
PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(mPagerSnapRecyclerView);

Demo(Ps:demo中adapter使用的第三方的MultiTypeAdapter)

sdfsdfs
sdfsdfs
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容