1.ViewStubLazy Loading
Viewstub is an invisible, size 0 view, the best use is to implement the view delay loading, when needed to load the view, can be common in Java Performance optimization method lazy loading.
When the setvisibility function that calls Viewstub is set to visible or the call inflate initializes the view, the resource referenced by the viewstub begins to initialize, and then references the resource substitution viewstub its own location to populate the Viewstub location. Therefore, before calling the setvisibility (int) or inflate () function, a viewstub exists in the component tree hierarchy, but because Viewstub is very lightweight, this has a very small impact on performance. You can redefine the referenced layout ID by viewstub the Inflatedid property. For example:
<ViewStub android:id="@+id/stub" android:layout_width="match_parent" android:layout_height="match_parent" android:inflatedId="@+id/subTree" android:layout="@layout/activity_viewstub_item"/>
//获取到viewstubfinal ViewStub stub = (ViewStub) findViewById(R.id.stub);//测试用inflate()填充布局new Handler() { @Override publicvoidhandleMessage(Message msg) { View view = stub.inflate(); }};handler.sendEmptyMessageDelayed(17000);
I've been here a while to call the stub.inflate(); method, load the layout, and we don't have to go to Findviewbyid to load the view anymore.
Android optimized--ui optimization (iii)