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
网友评论