美文网首页自定义控件
横向RecyclerView加载Banner使Item居中显示(

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

作者: 阿茳小粥257 | 来源:发表于2017-03-21 11:33 被阅读0次

项目中做了个横向滑动的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) 

相关文章

网友评论

    本文标题:横向RecyclerView加载Banner使Item居中显示(

    本文链接:https://www.haomeiwen.com/subject/mtnhgttx.html