Detailed explanation of layoutinflator example for Android layout loading

preface

When creating the interface, the activity needs to load the contents in the XML layout file. Just as we need to load the layout of item in listview or recyclerview, we use layoutinflator for operation.

There are many ways to obtain the layoutinflator instance, but it is finally obtained through (layoutinflator) context. Getsystemservice (context. Layout_inflator_service), that is, the layoutinflator loading the layout comes from the system service.

Because the content part of the Android system source code adopts the decoration mode, the specific functions of context are realized by contextimpl. By finding the code of getsystemservice in contextimpl and following up, we know that the last returned instance is phonelayouteinflator.

Layoutinflator is just an abstract class, while phonelayoutinflator is a concrete implementation class.

Load view with the inflate method

When using layoutinflator, the common method is the inflate method, which passes in a layout file ID and finally resolves it into a view.

Layoutinflator there are also many overloaded forms of the inflate method for loading a layout:

The difference between the two is whether to load the resource layout file into the root layout.

However, it should be noted that if the root is null, the attribute set for resource in the XML layout will be invalid, and the layout will be simply loaded.

Follow up the createviewfromtag method to see how the view is created.

If the factory interface is set, the view will be generated by the oncreateview method in the factory.

The function of layoutinflator.factory is to create a view when loading a layout, before the system creates a view.

As for the usage scenario of layoutinflator.factory, now more is the skin change of the application.

If the factory interface is not set, it is used to judge whether it is a custom control or system control. Whether it is oncreateview method or createview method, the createview method is finally called internally to generate the view.

Inside the createview method, first find out whether there is a corresponding cache from the view constructor cache. If not, generate a constructor and put it into the cache. If there is a constructor, see whether it can pass the filtering and whether it is allowed to generate the view.

Finally, if all conditions are met, the view is generated through the constructor reflection of the view.

When generating a view, constructor.newinstance is used to call the constructor, and the variables required for the parameters are defined by the mconstructorsignature variable, which are context and attributeset respectively. You can see that the corresponding parameters are also passed in when the view is finally generated.

The view is generated by reflection in the form of constructor.newinstance for decoupling. It can be loaded only with the class name.

It can be seen that the context still needs to be passed when loading the layout. Not only is it to get the layoutinflator, it will also be used when generating the view by reflection.

Depth traversal loading layout

If the layout that needs to be loaded has only one control, the view returned by layoutinflator will be finished.

If there are multiple views to be loaded in the layout file, continue to load the views under the top-level view through the rflatchildren method, and finally load them through the rflat method.

The rinfflate method first determines whether the parsing is finished. If not, the next view will be loaded from the xmlpullparser for processing, and different types will be processed in the middle, such as tag_ REQUEST_ FOCUS、TAG_ TAG、TAG_ INCLUDE、TAG_ Merge, wait.

Finally, the view is still generated through createviewfromtag. Take the generated view as the parent node, start the depth traversal, continue to call the rflatchildren method to load the layout, and add the view to its parent view.

As for why the method name createviewfromtag for generating view is literally from the tag tag, it must be related to the content generated by xmlpullparser parsing the layout.

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