Solution to inconsistent layout of items displayed in Android recyclerview

Recyclerview displays inconsistent item layout

When customizing the recycleradapter, the oncreateviewholder method is overridden

After generating the layout, it is found that the generated layout does not have layoutparams. When customizing the view in the past, it was found that layoutparams was generated by ViewGroup because the ViewGroup added here is null. Therefore, layoutparams will not be generated. As a result, check whether there are layoutparams in the getviewforposition method of recyclerview. If not, call generatedefaultlayoutparams of layoutmanager to generate default layoutparameters. The code snippet is as follows:

In the linearlayoutmanager, the generatedefaultlayoutparams method is implemented as follows.

Eventually, the display effect of recycleview will be inconsistent with the layout file. Later, layoutinflator was used to fill the layout.

Check the source code of layoutinflator and find that if the last parameter of inflate is false, the generated view will not be added to the parent. However, the corresponding layoutparameters will be generated according to the parent. The source code is as follows:

Because the view generated in oncreateviewholder cannot be manually added to recycleview, the last parameter can only be false;

Thank you for reading, hope to help you, thank you for your support to this site!

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