RecylerView介绍
RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recylerview即回收view也可以看出。官方对于它的介绍则是:RecyclerView 是 ListView 的升级版本,更加先进和灵活。RecyclerView通过设置LayoutManager,ItemDecoration,ItemAnimator实现你想要的效果。
因RecyclerView功能强大,因此准备写一个简单的demo通过RecyclerView实现ViewPager的功能,下面讲讲实现的步骤。
设计思路:RecyclerView可以通过PagerSnapHelper这个api实现翻页功能,每一页的子view通过string.xml设置的个数动态设置,然后加载在对应的LinearLayout中显示在当页,这样就达到了可动态设置和翻页的ViewPager功能
这个demo的实现使用mvp模式,对应的数据和需要设置子Item的宽高,个数,以及RecyclerView显示方向都是通过string.xml获取在代码中动态设置的。
1:下面讲下数据的获取:
通过线程池json解析获取拿到的string json字符串的方式获取对应的数据,然后回调给到ui进行RecyclerView的数据刷新。
2:RecyclerView设置
通过获取string中属性设置RecyclerView的宽高,设置LayoutManager,pagerSnapHelper,和 NaviAdapter。
3:NaviAdapter的onCreateViewHolder方法中创建LinearLayout,动态添加子view
4:RecyclerView的数据刷新
通过model中获取到的数据回调给MainActivity进行数据的刷新动作
5:动态设置项举例
<string name="ry_position">0</string><!--加载的方向 0表示水平,1表示竖直 --> <string name="ry_page_sizes">4</string> <string name="ry_total_sizes">50</string> <string name="ry_item_height">74</string> <string name="ry_item_width">280</string> <string name="ry_serial_num_loop">0</string> <!--子Item序号是否循环,0否1是 -->
另外需要动态设置的内容全部在string.xml中设置,目前只是写了一个简单的demo,因此动态设置项比较少,各位大神有什么好的建议获取需要做什么样的优化,可以在评论下面提出来,本人将非常感谢,另外demo的源码已经上传github,地址为:GitHub - daxiangzaici214703306/RyDemo: A project about recyclerview, which realizes the viewpager function through recyclerview, can set two methods, vertical and horizontal through string.xml, set the number of visible areas and the width and height of sub items, and the page layout is simple, and hope to have the favorite God to optimize.,欢迎大家下载修改。