2.Android Skill 精致的Adapter

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缓存起来方便多次重用.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值