一种简洁的Adapter模式实现

2015年12月15日 11:46

鄙人觉得做重复的事情就是浪费生命啊(Don’t Repeat Yourself原则), 在Android中ListView的Adapter中有大量重复工作可以被取消掉.

首先看看一种已经实现的调用方式:

adapter =
new QuickAdapter<GoodsModel>(R.layout.adapter_goods) {
            @Override
            public void bindView(Context context, AutoViewHolder holder, int position, GoodsDetailModel model) {
                //商品缩略图
                Picasso.with(NativeActivity.this).load(model.getPicThumb()).into(holder.getImageView(R.id.goods_item_thumb));
                //商品的标题
                holder.getTextView(R.id.goods_item_title).setText(model.getTitle());
                //商品的价格
                holder.getTextView(R.id.goods_item_now_price).setText(model.getPrice());
            }
        };

说明: GoodsModel是商品的数据模型, R.layout.adapter_goods是相应的布局文件, 然后在bindView()方法中将相应的数据和视图绑定.

以下是鄙人的实现:

QuickAdapter

package com.qbeenslee.base;

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

import java.util.ArrayList;
import java.util.List;

public class QuickAdapter<T> extends BaseAdapter {
    private final int layoutId;
    protected List<T> mData;

    public QuickAdapter(int layoutId) {
        this.layoutId = layoutId;
    }

    public QuickAdapter(int layoutId, List<T> data) {
        this.layoutId = layoutId;
        this.mData = data;
    }


    public void addData(List<T> data) {
        if (data != null) {
            if (this.mData == null) {
                this.mData = new ArrayList<>(data.size());
            }
            this.mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public List<T> getData() {
        return mData;
    }

    public void setData(List<T> data) {
        if (data != null) {
            if (this.mData == null) {
                this.mData = data;
            } else {
                this.mData.clear();
                this.mData.addAll(data);
            }
            notifyDataSetChanged();
        }
    }

    public View getLayoutView(ViewGroup parent, int layoutRes) {
        View view = null;
        if (parent != null) {
            Context context = parent.getContext();
            if (context != null) {
                LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (li != null) {
                    view = li.inflate(layoutRes, parent, false);
                }
            }
        }
        return view;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public T getItem(int position) {
        return mData != null && mData.size() > position && position >= 0 ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public String toString() {
        return mData == null ? "CommonAdapter:Data Is Null" : "CommonAdapter{" + "mData=" + mData + '}';
    }

    public int getLayoutId() {
        return layoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutView(parent, layoutId);
        }
        bindView(parent.getContext(), AutoViewHolder.getViewHolder(convertView), position, getItem(position));
        return convertView;
    }

    public void bindView(Context context, AutoViewHolder holder, int position, T model) {
    }
}

AutoViewHolder

package com.qbeenslee.base;

import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * ViewHolder模式
 */
public class AutoViewHolder {
    private final SparseArray<View> viewHolder;
    private final View view;

    private AutoViewHolder(View view) {
        this.view = view;
        viewHolder = new SparseArray<>();
        view.setTag(viewHolder);
    }

    public static AutoViewHolder getViewHolder(View view) {
        AutoViewHolder viewHolder = (AutoViewHolder) view.getTag();
        if (viewHolder == null) {
            viewHolder = new AutoViewHolder(view);
            view.setTag(viewHolder);
        }
        return viewHolder;
    }

    public <T extends View> T get(int id) {
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }

    public View getConvertView() {
        return view;
    }

    public TextView getTextView(int id) {
        return get(id);
    }

    public Button getButton(int id) {
        return get(id);
    }

    public ImageView getImageView(int id) {
        return get(id);
    }

    public void setTextView(int id, CharSequence charSequence) {
        getTextView(id).setText(charSequence);
    }

}

在github上搜了一下发现基于RecyclerView的实现方案, 大家也可以参考一下QuickAdapter , SuperAdapter, CommonAdapter .

文章作者: qbeenslee

本文为原创,转载请遵守本站版权声明