Adapter(适配器),在Android方面主要作为ListView和数据源之间的是中间人、适配器。
一、Adapter的特点
1.当每条数据进入可见区时:
<1>.Adapter的getView()会被调用
<2>.返回代表具体数据的视图
2.触摸滚动时,频繁调用
3.支持成百上千条数据
二、Adapter操作的过程:
三、精致的Adapter
</pre><p></p><p><span style="font-family:Microsoft YaHei;font-size:14px;"><strong>1</strong>.<span style="color:#009900;"><strong>显示每条数据的 XML 布局文件</strong></span></span></p><p><pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="48dip"
android:layout_height="48dip" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0" />
</LinearLayout>
2.使用ViewHolder模式和convertView回收视图
package com.zyy.android_csdn.skill;
import com.zyy.android_csdn.R;
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;
/**
*
* 精致的Adapter
*
* @author CaMnter
*
*/
public class ExquisiteAdapter extends BaseAdapter {
private Context context;
private int[] data_text;
private Bitmap oneIcon;
private Bitmap zeroIcon;
public ExquisiteAdapter(Context context, int[] data, Bitmap oneIcon,
Bitmap zeroIcon) {
this.context = context;
this.data_text = data;
this.oneIcon = oneIcon ;
this.zeroIcon = zeroIcon ;
}
// 使用ViewHolder模式,效率再提高50%
static class ViewHolder {
TextView text;
ImageView icon;
}
@Override
public int getCount() {
return this.data_text.length;
}
@Override
public Object getItem(int position) {
return this.data_text[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//利用convertView回收视图, 效率提高 200%
if (convertView == null) {
convertView = LayoutInflater.from(this.context).inflate(
R.layout.adapter_list, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
// 精致的 setTag
convertView.setTag(holder);
} else {
// 精致的 getTag
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(this.context.getResources().getString(
this.data_text[position]));
holder.icon
.setImageBitmap((position & 1) != 1 ? zeroIcon : oneIcon);
return convertView;
}
}
还提到了setTag()保存Holder到对应的convertView中。
四、关于setTag()
我们可以看一看setTag()的源码:
/**
* Sets the tag associated with this view. A tag can be used to mark
* a view in its hierarchy and does not have to be unique within the
* hierarchy. Tags can also be used to store data within a view without
* resorting to another data structure.
*
* @param tag an Object to tag the view with
*
* @see #getTag()
* @see #setTag(int, Object)
*/
public void setTag(final Object tag) {
mTag = tag;
}
/**
* The view's tag.
* {@hide}
*
* @see #setTag(Object)
* @see #getTag()
*/
protected Object mTag = null;
就是作为一个View的额外保存任何数据的标签而存在的。可以使用setTag把查找的view缓存起来方便多次重用.