Android Optimization-UI optimization (3)
UseViewStub
Delayed Loading
1.
ViewStub
Delayed Loading
ViewStub is an invisible View with a size of 0. The best use is to implement delayed loading of the View and load the View as needed, the common performance optimization methods in Java are the same for delayed loading.
When the setVisibility function of ViewStub is called to be visible or inflate is called to initialize the View, the resources referenced by ViewStub start initialization, and then the referenced resources replace ViewStub's position and fill in the ViewStub position. Therefore, ViewStub exists in the component tree hierarchy before the setVisibility (int) or the inflate () function is called. However, ViewStub is lightweight and has little impact on performance. You can use the inflatedId attribute of ViewStub to redefine the referenced layout id. For example:
Xml
Java
// Obtain viewstubfinal ViewStub stub = (ViewStub) findViewById (R. id. stub); // use inflate () to fill the layout with Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {View view = stub. inflate () ;}}; handler. sendEmptyMessageDelayed (1, 7000 );
I have called it for a while.stub.inflate();
Method to load the layout. Here we do not need to load the view with findViewById.