Implementation of recyclerview custom split line in Android

I've been watching recyclerview recently. Compared with listview, it is really flexible and gives developers more customization space, such as adding header and tail, item click events, customized layoutmanager, and the following customized split line.

1. How to understand split lines

I often hear people say that customizing the split line is troublesome. Why not write the split line into the item layout? Isn't it easier? In some cases, it is difficult to achieve the desired effect by writing the split line into the item layout, such as gridlayoutmanager, staggeredgridlayoutmanager and some custom layoutmanagers in recyclerview. The split lines to be drawn by items in different positions are different. At this time, the application of custom split lines can solve this problem.

2. How to draw a split line

There are also many ways to write recyclerview custom split lines on the Internet. Many of them are added by obtaining listdivider in the system properties and set in apptheme in the system. However, if I have two styles of split lines, it will be embarrassing. Therefore, I hope to pass in a drawable like listview to set the split line, So our idea is to finally set the split line as follows:

3. Specific code implementation

Because recyclerview has a variety of layout methods, its split lines are also different according to different layouts. This paper only focuses on the linear layout manager

Now the complete class is given, and there are comments in key parts of the code, so we won't explain them one by one:

4. How to use it

Recyclerview Trilogy

R.drawable.divider_ love

The corresponding effects are as follows:

We can see the obvious indentation effect. If it is set to zero, there will be no indentation.

Let's see what it looks like in normal use

Corresponding r.drawable.divider_ love

We only need to modify the color of paint in customdecoration to make the indent color consistent with the background color. The default is white.

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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>