Detailed explanation of item width and height of recyclerview in Android

preface

This article mainly introduces the relevant content about the width and height of the item of recyclerview in Android, and shares it for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

When creating the view passed in by the viewholder, if its ViewGroup is not specified, the width and height will only wrap the display content.

The above practice will go wrong

Change it so that the set width and height can be displayed normally

Then there is still a problem.

If my view is created before the adapter is created, that is, my view is passed in from the outside, not in the oncreateviewholder method. In this way, the parent cannot be set. How to deal with this situation?

I only found many methods. For example, in the oncreateviewholder method, parent. Addview () is useless.

I checked a lot of posts on the Internet, which are nonsense. But I had to check the source code. Unexpectedly, I just found the key to the solution.

After reading the source code, we know that the parent parameter we want to pass is the recyclerview.

From here, we found that the parent we used was recyclerview.this. This means that as long as there is a recyclerview before creating the view, even creating the view before the adapter can make the view display normally in the recyclerview

Additional content: solve the width and height problem of item in an alternative way

I wrote the above content a long time ago. Now I encounter new situations, so I'll add it. As mentioned above, if you do not specify a parent when creating an item, the final effect is to wrap the content.

For example:

For such an item layout, even if I set textview to be displayed in the middle, it will be displayed on the left during actual operation. This situation is a bit like the ViewGroup of relativelayout pumping out the air inside.

The above solution is no problem. Set the parent for this layout when creating. However, sometimes you decouple and reduce the coupling between recyclerview and item very low. This is to create an item layout. It is very troublesome to get the parent of recyclerview. What should I do at this time?

In fact, you will find that sometimes without setting parent, the width of item can fill the entire parent layout. Why, because in this case, the width of item already fills the layout, that is, there is no "air" in the width, so it will not be compressed in any width.

According to this principle, we can solve this problem with a simple operation, that is, it is easy for relativelayout to find a way to fill the layout width.

In this way, when displaying, the content of the first textview can be displayed to the middle normally.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>