1. 程式人生 > >android之listView快取機制

android之listView快取機制

package com.example.day_05_06;


import java.util.ArrayList;
import java.util.List;


import com.litsoft.General.General;


import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
private ListView lvGenerals;//listView物件
private List<General> generals ;//資料集合
private BaseAdapter adapter;//介面卡
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initView() {
// TODO Auto-generated method stub
lvGenerals = (ListView) findViewById(R.id.lvGenerals);
adapter = new GeneralAdapter(this,generals);
lvGenerals.setAdapter(adapter);
}
private void initData() {
// TODO Auto-generated method stub
generals = new ArrayList();

int [] images = new int[] {
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
String [] names = getResources().getStringArray(R.array.generals);
for(int i=0;i<images.length;i++){
General general = new General(images[i],names[i]);
generals.add(general);
}
}
class GeneralAdapter extends BaseAdapter{
private Context context;
private List<General> innerGenerals;
public GeneralAdapter(Context context, List<General> innerGenerals) {//這裡用構造器的方式傳遞外部類的上下文物件和General集合物件
super();
this.context = context;
this.innerGenerals = innerGenerals;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return generals.size();
}





@Override
public General getItem(int position) {
// TODO Auto-generated method stub
return generals.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.i("main", position+"");
ViewHolder viewHolder = null;

if(convertView == null){//第一屏中尚未建立列表項時
viewHolder = new ViewHolder ();
convertView = View.inflate(context, R.layout.general, null);
viewHolder.ivGerneral = (ImageView) convertView.findViewById(R.id.ivThumb);
viewHolder.tvName = (TextView)convertView.findViewById(R.id.tvName);
convertView.setTag(viewHolder);//這裡相當於初始化一次用於適配的佈局,以後都用這個佈局了,就是滾動的時候就不用再初始化用於適配的佈局
}else{//以後的滾動,出現其他的列表項
viewHolder = (ViewHolder) convertView.getTag();
}
General general = this.innerGenerals.get(position);
viewHolder.ivGerneral.setImageResource(general.getImageSrc());
viewHolder.tvName.setText(general.getName());
return convertView;
}

}
class ViewHolder{//用於適配佈局的具體項
ImageView ivGerneral;
TextView tvName;
}


}