Delayed loading and insert View, delayed loading insert View
What do you do when we need to add a header or footer for each View in the App?
Duplicate copy and paste can solve this problem, but it is complicated. You can try to use the <include/> label:
The first method is to specify the width and height in the <include/> label:
Main. xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <Button android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:layout_gravity ="center_vertical" android:onClick ="onShowMap" android:text ="@string/show_map" /> <include layout ="@layout/footer" /></RelativeLayout>
Footer. xml
<TextView xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_alignParentBottom= "true" android:layout_marginBottom= "30dp" android:gravity= "center" android:text= "@string/footer_text" />
Ii. Use ViewStub class to delay loading views
When designing a view, it is sometimes considered that the visibility of some views depends on the user's operations or the specific environment of the running device.
How do you design it now? Is it just to change the visible attribute of the View?
Let's take a look at the introduction of ViewStub:
ViewStub is an invisible zero-sized control that can be used to load view resources at runtime. Only when we set the ViewStub visibility to true or call the inflate () method will its view resources be loaded.
Assume that a page we designed contains a map View. Imagine the following situation:
Some users do not need to see the map.
Since users do not need to see a map, why are we still persistently loading it? This affects the Performance of our apps.
Now we can use the ViewStub class:
Main. xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <Button android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:layout_gravity ="center_vertical" android:onClick ="onShowMap" android:text ="@string/show_map" /> <ViewStub android:id ="@+id/map_stub" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:inflatedId ="@+id/map_view" android:layout ="@layout/map" /></RelativeLayout>
Map. xml
<com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:apiKey= "my_api_key" android:clickable= "true" />
Next let's take a look at MainActivity
public class MainActivity extends MapActivity { private View mViewStub; @Override public void onCreate (Bundle savedInstanceState ) { super. onCreate( savedInstanceState ); setContentView( R. layout. main); mViewStub = findViewById( R. id. map_stub); } public void onShowMap (View v) { mViewStub. setVisibility (View .VISIBLE ); }....}
As you can see, we call onShowMap to change the visibility of map_stub only when the image needs to be displayed. Before the changes, map_stub will not render and load view resources.
Summary:
1. When our pages become complex and there are too many XML files, the <include/> label can effectively help us sort the file content and improve the readability of XML files. At the same time, its usage is similar to Fragment.
2. ViewStub is an excellent way to load view resources with latency. You can use ViewStub class as long as the designed view is dependent on context to change its visibility. Maybe when you only apply it to a simple page, you don't feel any improvement in performance, but in complex pages, it works very well.