谷歌说使用RecycleView的理由:
RecyclerView is a more advanced and flexible version of ListView. This
widget is a container for large sets of views that can be recycled and
scrolled very efficiently. Use the RecyclerView widget when you have
lists with elements that change dynamically.
它是ListView的更高度定制版,当你需要高效的展示大量数据时候,动态改变元素的列表的时候,就用这个。
优点:
当然,如果只是动态展示数据,listview也可以做到,用它替代listview的原因有几个:
- 简介中提到的它封装了viewholder的回收复用。
RecyclerView使用布局管理器管理子view的位置(目前尚只提供了LinearLayoutManager),也就是说你再不用拘泥于ListView的线性展示方式,如果之后提供其他custom LayoutManager的支持,你能够使用复杂的布局来展示一个动态组件。
StaggeredGridLayoutManager mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); //表示两列,并且是竖直方向的瀑布流 mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
在布局上支持三种
StaggeredGridLayoutManager
,GridLayoutManager
LinearLayoutManager
。- 自带了ItemAnimation,可以设置加载和移除时的动画,方便做出各种动态浏览的效果。
分开的view
我们平时用listview的时候,adapter一般这么写的if (convertView == null) { holder = new ViewHol