Some pitfalls to pay attention to when using layoutinflator in Android

preface

In the normal development process, we often use the layoutinflator class, for example, in fragment $oncreateview and recyclerview. Adapter $oncreateviewholder. Its usage is nothing more than layoutinflator. Inflate (resourceid, root, attachtoroot). The first parameter has nothing to say, but the combination of the second and third parameters will bring some confusion. Sometimes I found some problems in the interface layout before. After checking for a long time, I accidentally changed these two parameters. I found that the problem was solved, and then it passed. I didn't think about why, and then I may repeat this dilemma next time.

So I would like to summarize here to avoid further pit falling in the future.

Let's take a look at the comments of the inflate method:

The first thing to understand is that the measurement result of a view is not just determined by its own layout_ Width and layout_ The height (i.e. layoutparams) is determined by the constraint (measurespec) given to it by the parent container and its own layoutparams.

After reaching this consensus, let's look at its parameters.

It will be easier to understand with a few examples. The layout of activity is a LinearLayout. The layout to be added is as follows:

Normal situation

The visual results are the same

But the log is a little different, which is caused by different values of attachtoroot.

Log of the first method

Log of the second method

Another thing to note is that adding mlinearlayout.addview (inflatedview) to the first method will result in an error IllegalStateException: the specified child already has a parent.

If the second method does not have this sentence, nothing can be seen on the interface.

When root is null

Now look at its layout file:

Not hard to find, all layouts_ The properties of XXX are all invalid.

Inflator in recyclerview

As mentioned above, there are two methods to add a layout to the root when creating a layout, but when we add a layout in oncreateviewholder, it is written as follows:

If the third parameter passes true, an error will be reported. Why?

The intuitive explanation is that the addition and deletion of child views are managed by recyclerview, and we do not need to add them. But it would be better for us to understand it from the code of recyclerview.

Taking linearlayoutmanager as an example, recyclerview will call the linearlayoutmanager $layoutchunk method when creating a child view:

During initialization, the oncreateviewholder method we are familiar with will be called in view view = layoutstate.next (recycler). Then, in the process of inflate, the third parameter passed true and the child view was added to the recyclerview. However, after getting the View, it is called to addView (because it is initialized, it is impossible to call addDisappearingView), and it will be added again, so the IllegalStateException exception above is reported.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. 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
分享
二维码
< <上一篇
下一篇>>