Android RecyclerView 的基本使用之GridView
前文實現了利用RecyclerView實現ListView,並可以任意新增分割線,在這裡主要討論一下用RecyclerView實現GridView並實現新增分割線。
這裡要提到的是RecyclerView.LayoutManager這個類。
通過查閱Android API我們可以看到如下:
A RecyclerView.LayoutManager繼承自Object,提供了三個子類: GridLayoutManger:A StaggeredGridLayoutManager:A
LayoutManager that lays out children in a staggered grid formation. It supports horizontal & vertical layout as well as an ability to layout children in reverse. 這樣,要新增GridView,只要使用 In contrast to other adapter-backed views such as or , RecyclerView allows client code to provide custom layout arrangements for child views. These arrangements are controlled by the. A LayoutManager must be provided for RecyclerView to function. Several default strategies are provided for common uses such as lists and grids. Parameters
DividerGridItemDecoration。
在MainActivity里加入以下程式碼即可實現: 效果如下: 此外,下面程式碼分別添加了橫向滑動的GridView和豎向滑動的GridView。 |