当前位置: 首页 > 工具软件 > CommonAdapter > 使用案例 >

万能适配器 CommonAdapter

麻宜春
2023-12-01
package com.stephen.ifishing.adapter;

import java.util.List;

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

/**
 * 
 * @author Stephen Huang
 *
 * @param <T>
 */
public abstract class CommonAdapter<T> extends BaseAdapter {

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

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

    @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;
    }

    public void onDataChange(List<T> data) {
        this.mDatas = data;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position);
        convert(holder, mDatas.get(position));
        return holder.getConvertView();
    }

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

}
 2. 辅助类ViewHolder
package com.stephen.ifishing.adapter;

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;

/**
 * 
 * @author Stephen Huang
 *
 */
public class ViewHolder {

    private SparseArray<View> mViews;
    private int mPostion;
    private View mConvertView;

    public ViewHolder(Context context, ViewGroup parent, int layoutId, int postion) {
        this.mPostion = postion;
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(layoutId, null);
        mConvertView.setTag(this);
    }

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

    /**
     * 通过viewId获取控件
     * @param viewId
     * @return
     */
    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;
    }

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

    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bm);
        return this;
    }
}

3. 如何使用我们的CommonAdapter?
package com.stephen.ifishing.adapter;

import java.util.List;

import com.stephen.ifishing.R;
import com.stephen.ifishing.model.ShowFishingItem;

import android.content.Context;

/**
 * 
 * @author Stephen Huang
 *
 */
public class ShowFishingAdapter extends CommonAdapter<ShowFishingItem> {

    public ShowFishingAdapter(Context context, List<ShowFishingItem> itemList) {
        super(context, itemList, R.layout.show_fishing);
    }

    @Override
    public void convert(ViewHolder holder, ShowFishingItem bean) {
        holder.setText(R.id.tv_user_name, bean.userName)
            .setText(R.id.tv_say_something, bean.saySomething)
            .setImageResource(R.id.iv_portrait, bean.portraitImageResId);
    }

}
 类似资料: