布局文件延迟加载

在Android中可以很容易使用<include />标签复用布局,可是当使用<include />标签引入了一些复杂控件时,有些控件在实际的使用过程中,很少会使用它,这样以来会降低页面加载的速度。针对这样的情况,可以使用Android 提供<ViewStub />标签进行优化。

        <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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值