ViewHolder模式充分利用了ListView的视图缓存机制,避免了每次调用getView()方法的时候都需要通过findViewById()实例化控件,因此是提高ListView效率一个重要的方法。
实现效果图如下所示:
实际使用中也并不复杂,只需在自定义的Adapter中定义一个内部类ViewHolder,并将布局的控件作为内部成员变量,举例代码如下所示:
PicTxtLVAdapter
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PicTxtLVAdapter extends BaseAdapter {
private List<Map<String, Object>> mDatas;
private LayoutInflater mInflater;
public PicTxtLVAdapter(Context context, List<Map<String, Object>> datas) {
this.mInflater = LayoutInflater.from(context);
this.mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_pic_txt, null);
holder.mIvIc = (ImageView) convertView.findViewById(R.id.iv_picTxt_ic);
holder.mTvTitle = (TextView) convertView.findViewById(R.id.tv_picTxt_title);
holder.mTvContent = (TextView) convertView.findViewById(R.id.tv_picTxt_content);
convertView.setTag(holder);
} else { // 通过tag找到缓存的布局
holder = (ViewHolder) convertView.getTag();
}
holder.mIvIc.setImageBitmap((Bitmap) mDatas.get(position).get("icon"));
holder.mTvTitle.setText((CharSequence) mDatas.get(position).get("title"));
holder.mTvContent.setText((CharSequence) mDatas.get(position).get("content"));
return convertView;
}
public final class ViewHolder {
public ImageView mIvIc;
public TextView mTvTitle;
public TextView mTvContent;
}
}
核心部分就是ViewHolder内部类和getView()方法,这种模式便可使得我们ListView效率大大提高,因此强烈建议使用。最后贴上其他代码如下所示:
item_pic_txt.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dp" >
<ImageView
android:id="@+id/iv_picTxt_ic"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="6dp"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_picTxt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:text="TextView" />
<TextView
android:id="@+id/tv_picTxt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
activity_pic_txt.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"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/lv_picTxt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
PicTxtLVAdapter
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;
public class PicTxtActivity extends Activity {
private ListView mLv;
private List<Map<String, Object>> mDatas;
private PicTxtLVAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pic_txt);
mLv = (ListView) findViewById(R.id.lv_picTxt_content);
initData();
mAdapter = new PicTxtLVAdapter(this, mDatas);
mLv.setAdapter(mAdapter);
}
private void initData() {
mDatas = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("icon", BitmapFactory.decodeResource(getResources(), R.drawable.ic_item1));
map1.put("title", "Itme1");
map1.put("content", "This is a Item1");
mDatas.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("icon", BitmapFactory.decodeResource(getResources(), R.drawable.ic_item2));
map2.put("title", "Itme2");
map2.put("content", "This is a Item2");
mDatas.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("icon", BitmapFactory.decodeResource(getResources(), R.drawable.ic_item3));
map3.put("title", "Itme3");
map3.put("content", "This is a Item3");
mDatas.add(map3);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("icon", BitmapFactory.decodeResource(getResources(), R.drawable.ic_item4));
map4.put("title", "Itme4");
map4.put("content", "This is a Item4");
mDatas.add(map4);
Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("icon", BitmapFactory.decodeResource(getResources(), R.drawable.ic_item5));
map5.put("title", "Itme5");
map5.put("content", "This is a Item5");
mDatas.add(map5);
}
}