在RecyclerView中,我们可以通过LayoutManager控制其布局的显示方式,实现横向ListView、GridView、瀑布流等常见效果。
RecyclerView.LayoutManager是一个抽象类,其直接的实现类有LinearLayoutManager、StaggeredGridLayoutManager,而LinearLayoutManager也有一个子类GridLayoutManager.
1)通过LinearLayoutManager:我们可以实现横向、纵向的列表效果
2)通过GridLayoutManager:我们可以实现像GridView一样的网格布局
3)通过StaggeredGridLayoutManager:我们可以实现瀑布流的效果
有3个构造方法:
LinearLayoutManager(Context context)
LinearLayoutManager(Context context,int orientation,boolean reverseLayout)
LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes)
第一个构造方法内部调用了第二个构造方法
第二个构造方法参数的含义:
- Context context :上下文,初始化时,构造方法内部加载资源用
- int orientation :方向,垂直和水平,默认为垂直
- boolean reverseLayout:是否倒序,设置为
True
,从最后一个item
开始,倒序加载。此时,RecyclerView
第一个item
是添加进Adapter
中的最后一个,最后一个item
是第一个加进Adapter
的数据,RecyclerView
会自动滑到末尾