横向RecyclerView加载Banner使Item居中显示(使用LinearSnapHelper)

项目中做了个横向滑动的banner,想要实现主图居中显示,查找了下实现的方式,写在此处作为记录,如下:

一句话介绍SnapHelper: SnapHelper是RecyclerView功能的一种拓展,使RecyclerView滑动行为类似ViewPager,无论怎么滑动最终停留在某页正中间。ViewPager一次只能滑动一页,RecyclerView+SnapHelper方式可以一次滑动好几页,且最终都停留在某页正中间。非常实用和酷炫。

SnapHelper的实现原理是监听RecyclerView.OnFlingListener中的onFling接口。LinearSnapHelper是抽象类SnapHelper的具体实现。



上面的效果只需下面几行代码即可。重点在于new LinearSnapHelper().attachToRecyclerView(recyclerView);

LinearLayoutManager linearLayoutManager =newLinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);recyclerView.setLayoutManager(linearLayoutManager);

newLinearSnapHelper().attachToRecyclerView(recyclerView);

具体详解请转至:http://www.jianshu.com/p/d8f0842f5f06?utm_campaign=maleskine&utm_content=note&utm_medium=mobile_all_hots&utm_source=recommendation

点击此处跳转:

LinearSnapHelper源码解析(作者: 依然fantac) 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容