天天看點

Using lazy loading and avoiding replication(延遲加載和避免重複)

1.Avoid replication using the <include /> tag

<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_horizontal"
    android:text="footer_text" />
           

使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:text="hello" />

    <include layout="@layout/footer_with_layout_properties" />

</RelativeLayout>
           

2. Lazy loading views with the ViewStub class

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate

layout resources at runtime. When a ViewStub is made visible, or when

inflate() is invoked, the layout resource is inflated. The ViewStub then

replaces itself in its parent with the inflated View or Views.

下面使用這個延遲加載百度地圖: 首先:

Using lazy loading and avoiding replication(延遲加載和避免重複)
Using lazy loading and avoiding replication(延遲加載和避免重複)

代碼:

<?xml version="1.0" encoding="utf-8"?>
<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:onClick="onShowMap"
        android:text="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>
           
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:clickable="true" />
    

</LinearLayout>
           
package com.example.lview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;

public class MainActivity extends Activity {

	MapView mMapView = null;

	private View mViewStub;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SDKInitializer.initialize(getApplicationContext());

		setContentView(R.layout.activity_main);
		mViewStub = findViewById(R.id.map_stub);

		mMapView = (MapView) findViewById(R.id.bmapView);
	}

	public void onShowMap(View v) {
		mViewStub.setVisibility(View.VISIBLE);
	}

}