Detailed explanation of Android recyclerview grid layout (supporting multiple split lines) (2)
Part I detailed explanation of Android recyclerview (1) - linear layout
The usage of recyclerview is recorded, and the usage of linear layout list is described. On this basis, a universal split line is added, which supports color split lines and picture split lines, as well as setting lineweight for split lines. This article is a summary of the use of grid layout. It also supports two kinds of split line and lineweight settings.
Main related categories:
1. RecyclerView.Adapter
2. Gridlayoutmanager grid layout manager
3. Recycleview.itemdecoration split line
The following is a direct example:
Effect drawing first:
(1) Color split line
It looks good. Display the grid according to the number of items. Of course, if the style you need is not three columns, this is very simple. You only need to set the corresponding number of columns when setting the gridlayoutmanager, that is:
mManagerLayout = new GridLayoutManager(getActivity(),3);
(2) Picture split line
Some people may say that there are split lines around your list. In fact, there are no split lines on the left and above by default without special treatment. Later, I will add that there are no dividing lines around. In fact, these two forms are common in actual development. First, there are side lines around.
Because recycleview is a highly decoupled control, drawing the split line is only related to recycleview.itemdecoration, so we only need to care about how to inherit recycleview.itemdecoration to realize the split line we need, as follows:
What needs to be explained here is that the principle of color segmentation line is exactly the same as that of picture segmentation line. The picture segmentation line only transmits a very thin picture.
Here is the complete code:
1. MainActivity
activity_ main
2. GridFragment
3. Dividing line griddivider
Directly inherit recyclerview.itemdecoration
4. Adapter
Item layout of adapter
Unfinished to be continued
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.