Android中實現延遲載入效果
主要是分兩步實現,一是在xml佈局中使用ViewStub控制元件,再在java程式碼中實現。
1) 首先,只要是要將Android頁面中要進行延遲載入,那麼都要將該部分佈局載入到ViewStub中。
<FrameLayoutandroid:layout_width="match_parent"android:layout_height="360dp"android:layout_marginLeft="20dp"android:layout_marginRight="20dp">
<ImageViewandroid:id="@+id/shengwuImg"android:layout_width=
<ViewStubandroid:id="@+id/inflat_class_img_top"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="45dp"android
<ViewStubandroid: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
<ViewStubandroid: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() {
@Overridepublic void
run() {
handler.sendEmptyMessage(1);
}
}, 400);
timer.schedule(new
TimerTask() {
@Overridepublic void
run() {
handler.sendEmptyMessage(2);
}
}, 800);
timer.schedule(new
TimerTask() {
@Overridepublic void
run() {
handler.sendEmptyMessage(3);
}
}, 1200);
timer.schedule(new
TimerTask() {
@Overridepublic 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;
}
};
};