RecyclerView + SnapHelper实现的炫酷Vi

作者: Android高级架构探索 | 来源:发表于2018-12-08 23:10 被阅读23次

目录

image.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做了一个案例:

image.png

注‘Android技术交流群878873098,欢迎大家加入交流,畅谈!本群有免费学习资料视频’并且免费分享源码解析视频

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

链接:https://www.jianshu.com/p/f23f6271a074

相关文章

网友评论

    本文标题:RecyclerView + SnapHelper实现的炫酷Vi

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