您现在的位置是:首页 > 文章详情

RecyclerView + SnapHelper实现炫酷ViewPager效果

日期:2018-09-16点击:853

目录

img_d6f3db79959842c089779b4c34eec5eb.png

什么是SnapHelper

SnapHelper是Google 在 Android 24.2.0 的support 包中添加的对RecyclerView的拓展,结合RecyclerView使用,能很方便的做出一些炫酷的效果。

SnapHelper的使用方法

SnapHelper是一个抽象类 Google 内置了两个默认实现类,LinearSnapHelper和PagerSnapHelper。

  • LinearSnapHelper:使当前Item居中显示,常用场景是横向的RecyclerView, 类似ViewPager效果,但是又可以快速滑动多个条目。
LinearLayoutManager manager = new LinearLayoutManager(getContext()); manager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(manager); LinearSnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(mRecyclerView); 
  • PagerSnapHelper:使RecyclerView 像ViewPager一样的效果,每次只能滑动一页。
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecycleview.setLayoutManager(linearLayoutManager); PagerSnapHelper snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(mRecycleview); 

这里我只使用了PagerSnapHelper做了一个案例:

img_0082754f4cee12fd2790e62bfb8424be.gif

项目源码: https://github.com/myml666/SnapHelperDemo

个人技术博客:https://myml666.github.io

原文链接:https://yq.aliyun.com/articles/665537
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章