adapter万能适配器

adapter万能适配器

看了慕课网adapter万能适配器的视频,记录一下。

首先定义一个ViewHolder,item复用和避免重复findViewById:

package com.hou;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/** * Created by hp on 2016/3/11. */
public class ViewHolder {
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        mConvertView.setTag(this);
    }

    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        } else {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            return holder;
        }
    }

    /** * 通过viewId获取控件 */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /** * 给TextView设值 */
    public ViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /** * 给ImageView设值resource */
    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    /** * 给ImageView设Bitmap */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }

}

重写BaseAdapter,CommonAdapter.java:

package com.hou;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/** * 万能适配器 */
public abstract class CommonAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected List<T> mDatas;
    private LayoutInflater mInflater;
    private int mLayoutId;

    public CommonAdapter(Context context, int layoutId, List<T> datas) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mLayoutId = layoutId;
        this.mDatas = datas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T 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 = ViewHolder.get(mContext, convertView, parent, mLayoutId, position);
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    public abstract void convert(ViewHolder holder, T t);
}

这样只需要把这两个文件复制到项目中就可以开始使用了。

定义一个adapter,MyAdapter.java,继承CommonAdapter:

package com.hou.aidlclient;

import android.content.Context;

import com.hou.CommonAdapter;
import com.hou.ViewHolder;
import com.hou.aidl.Person;

import java.util.List;

public class MyAdapter extends CommonAdapter<Person> {
    public MyAdapter(Context context, List<Person> datas) {
        //这里需要传递给CommonAdapter三个参数:Context ,布局文件,数据集合
        super(context, R.layout.item, datas);
    }

//现在不需要重写BaseAdapter中的其他方法,只需要重写父类的convert方法即可,
//而代码也只有简单的一句。
    @Override
    public void convert(ViewHolder holder, Person person) {
        holder.setImageResource(R.id.icon, R.mipmap.ic_launcher).setText(R.id.name, "\t姓名:" + person.name).setText(R.id.age, "\t年龄:" + person.age + "");
    }
}

运行效果如图;

你可能感兴趣的:(Adapter,viewholder)