Recyclerview is an example of implementing multi style layout through gridlayoutmanager

1、 Opening

It's been a while since I wrote the article last time. To tell you the truth, I really don't know what I've been busy recently. Now I'll settle down and record the problems encountered in the recent project for future use!

No more nonsense. Let's take a look at the renderings first

2、 Demand analysis

1. First, let's talk about the project requirements. Whether it's good or bad, the tag Tags below have different display types. Some with a large number of words will be processed in a single line (in fact, it can also be expanded here. For example, three tag tags can be displayed in a line for two or three words). At the first sight, we see that this requirement is ready to be implemented using the online open source library taglayout, However, after trying, we found that there is still a big gap between the effect they achieved and the effect required by the project. We can see that the text in the effect drawing is required to be centered and symmetrical.

2. It's no problem to use GridView to implement this function and define an adapter. The problem is that when to display one line and when to display two or three lines is uncertain. After all, sometimes the server returns only tag without type tag_ Name and tag_ ID, what type should you set according to, and if you still use GridView since recyclerview, it's too outdated. The following focuses on using recyclerview to achieve the effect shown in the figure.

3、 Function realization

We know that recyclerview only needs to configure the parameters to achieve the GridView effect

You can see that gridlayoutmanager needs to pass two parameters, one is a context object, and the other is a parameter constant that displays several columns in a row. Since this constant can be specified, whether this value can be controlled, of course, the answer is yes

We will notice that there is a setspansizelookup method in the gridlayoutmanager, and the focus of this article is this method (the specific significance of this method can be searched online, there will be a lot of relevant introductions, and some complex layouts can be realized through it, so we won't discuss it too much again)

In fact, the return value of getspansize is to control how many columns there are in each row. According to this idea, we might as well try. Because the recyclerview fills in data according to the adapter, let's judge the data source given to the adapter in the setspansizelookup method? According to this idea, we have the following code

There are so many core codes. We can customize the style according to our own needs

Finally, the source code address: https://github.com/lygttpod/AndroidCustomView

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
分享
二维码
< <上一篇
下一篇>>