<ViewStub />标签是一个轻量级的View,它不会占据任何空间,也不参与布局的计算与绘制,只是在控件树中做一个最简单的占位符,只有在确实需要使用<ViewStub />标签引用的布局时,才会去加载布局。把这种行为称之为延迟加载最贴切不过了。在第四章中,讲解ListView绑定EmptyView的时候我们曾经使用过ViewStub,也做过简单介绍。
在使用ViewStub时,通常需要给它设置以下三个属性:
1、android:id 与其他控件一样,ViewStub也有id属性,根据它可以查找到ViewStub
2、android:layout 指向ViewStub引用的布局,与include标签的laytout属性类似
3、android:inflateId通过它能覆盖引用的布局文件根节点的id,与include标签的id属性类似。
ViewStub_ExampleActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SearchView;
public class ViewStub_ExampleActivity extends Activity {
private Button searchButton;
private Button cancelButton;
private View progressView;
private ProgressBar mProgressBar;
private static int count = 0;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (count > 10) {
searchCancel();
} else {
mProgressBar.setProgress(10 * count++);
mHandler.postDelayed(mRunnable, 500);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_viewstub);
searchButton = (Button) findViewById(R.id.searchView1);
searchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchStart();
}
});
}
private void searchStart() {
if (progressView == null) {
// 这句话要注意!!
// 加载布局!!
progressView = ((ViewStub) findViewById(R.id.viewstub_progress))
.inflate();
mProgressBar = (ProgressBar) progressView
.findViewById(R.id.chatting_load_progress);
cancelButton = (Button) progressView
.findViewById(R.id.button1);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchCancel();
}
});
}
showViewStub();
mHandler.post(mRunnable);
}
private void searchCancel() {
if (progressView == null) {
progressView = findViewById(R.id.viewstub_progress);
}
mHandler.removeCallbacks(mRunnable);
hideViewStub();
}
private void showViewStub() {
count = 0;
mProgressBar.setProgress(0);
mProgressBar.setIndeterminate(false);
progressView.startAnimation(AnimationUtils.loadAnimation(this,
R.anim.abc_fade_in));
progressView.setVisibility(View.VISIBLE);
}
private void hideViewStub() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.abc_fade_out);
progressView.startAnimation(anim);
progressView.setVisibility(View.GONE);
mProgressBar.setProgress(0);
mProgressBar.setIndeterminate(true);
}
}
layout_viewstub.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff"
tools:context="${relativePackage}.${activityClass}" >
<ViewStub
android:id="@+id/viewstub_progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:inflatedId="@+id/layout_progress_inflated"
android:layout="@layout/layout_progressbar" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="18dp"
android:ems="10" />
<Button
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:layout_marginRight="25dp" />
</RelativeLayout>
layout_progressbar.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_progress_inflated"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/chatting_load_progress"
style="?android:progressBarStyleSmallInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="15.0dip" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="32dp"
android:text="Button" />
</RelativeLayout>