主要是分两步实现,一是在xml布局中使用ViewStub控件,再在java代码中实现。
1) 首先,只要是要将Android页面中要进行延迟加载,那么都要将该部分布局加载到ViewStub中。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<ImageView
android:id="@+id/shengwuImg"
android:layout_width="320dp"
android:layout_height="200dp"
android:layout_marginTop="70dp"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal|top" />
<ViewStub
android:id="@+id/inflat_class_img_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout="@layout/view_class_img_top"/>
<ViewStub
android:id="@+id/inflat_class_img_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout="@layout/view_class_img_bottom"
android:layout_gravity="right|center_vertical"/>
<ViewStub
android:id="@+id/inflat_class_mu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="left|bottom"
android:layout="@layout/view_class_mu"/>
<ViewStub
android:id="@+id/inflat_class_zhong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout="@layout/view_class_zhong"/>
</FrameLayout>
注意点:
=è首先,注意两点,一是ViewStub实现异步延迟加载,二是对于Fragment布局使用,使用Fragment布局实现的是可以将某一控件覆盖到某一控件上。
可以看出的是,每一个ViewStub控件都含有一个android:layout=””,也就是说的是,这就是表示的是所延迟加载的布局内容。
2) 在java代码中实现
inflat_class_img_top=(ViewStub)findViewById(R.id.inflat_class_img_top);
ViewStub控件的使用和普通控件的使用是一样的。
3) 使用Timer、TimerTask、Handler三个类完成的是异步加载效果,
其中Timer完成的是定时去检查文件中是否有更新的功能,注意这里指的是定时,也就是说的是Timer是一个线程设施,可以实现某一个时间或者某一段时间后去执行一个操作或者定期重复执行;
TimerTask的主要作用就是完成Timer所分配的任务,也就是一个TimerTask对象就是一个线程。
Handler实现的是消息传递。
public void initActivityData(){
try{
cursor = getDb().rawQuery("select _id,SHENGWUNAMEFISH,SHENGWUENGNAME,SHENGWUMUNAME,SHENGWUKENAME,SHENGWUSHUNAME,SHENGIMG,SHENGWUJIANJIE,SHENGWUTEZHENG,SHENGWUXIXING,SHENGWUREALIMG from "+getZhanlanDao().getTablename()+" WHERE SHENGWUNAMEFISH = '"+getIntentData().getShengwuname().toString()+"'",null);
if(cursor!=null){
if(cursor.moveToFirst()){
shengwuXingtai.setText(cursor.getString(8));
shengwuXixing.setText(cursor.getString(9));
shengwuImg.setImageURI(Uri.parse(cursor.getString(10)));
}
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
}, 400);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(2);
}
}, 800);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(3);
}
}, 1200);
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(4);
}
}, 1600);
}
}catch (Exception e){
e.printStackTrace();
}
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what){
case 2:
View pic_view = inflat_class_zhong.inflate();
shengwuZhongEngName=(TextView)pic_view.findViewById(R.id.shengwuZhongEngName);
shengwuZhongChName=(TextView)pic_view.findViewById(R.id.shengwuZhongChName);
shengwuZhongChName.setText(cursor.getString(1));
shengwuZhongEngName.setText(cursor.getString(2));
break;
case 1:
View img_view=inflat_class_img_top.inflate();
classImgTop=(ImageView)img_view.findViewById(R.id.classImgTop);
break;
case 3:
View img_view_bottom=inflat_class_img_bottom.inflate();
classImgBottom=(ImageView)img_view_bottom.findViewById(R.id.classImgBottom);
break;
case 4:
View class_txt_mu=inflat_class_mu.inflate();
shengwuMuChName=(TextView)class_txt_mu.findViewById(R.id.shengwuMuChName);
shengwuMuEngName=(TextView)class_txt_mu.findViewById(R.id.shengwuMuEngName);
shengwuKeChName=(TextView)class_txt_mu.findViewById(R.id.shengwuKeChName);
shengwuKeEngName=(TextView)class_txt_mu.findViewById(R.id.shengwuKeEngName);
shengwuShuChName=(TextView)class_txt_mu.findViewById(R.id.shengwuShuChName);
shengwuMuChName.setText(cursor.getString(3));
shengwuMuEngName.setText(cursor.getString(4));
shengwuKeChName.setText(cursor.getString(5));
shengwuKeEngName.setText(cursor.getString(6));
shengwuShuChName.setText(cursor.getString(7));
break;
}
};
};