关闭

[置顶] ListView适配器之增强版BaseAdapter

标签: adapterlistviewgetview
395人阅读 评论(0) 收藏 举报
分类:
以往我们要写一个ListView的适配器的时候,我们往往要先继承BaseAdapter,然后在重写很多方法,然后再在**getView(int position, View view, ViewGroup viewGroup)** 函数里面,写我们的itemView的逻辑,其实无非就是三点:
  1. inflate 视图布局
  2. 绑定视图
  3. 绑定数据(也就是我们的视图的逻辑)

    因此,这就是我下面要做的一些事情:把构造的关键步骤全部抽取成抽象函数,封装在父类当中,子类实现只需要在重写的方法里面实现就好了。


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

import java.util.List;

/**
 * an Enhanced abstract BaseAdapter .
 *
 * Created by Tata on 15/7/7.
 */
public abstract class AbstractBaseAdapter<T,V> extends BaseAdapter {


    private LayoutInflater mInflater;
    private List<T> mDatas;
    private Context mContext;


    public AbstractBaseAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

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

    @Override
    public T getItem(int i) {
        return mDatas == null ? null : mDatas.get(i);
    }

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

    public void setData(List<T> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    public void appendData(List<T> appendedData) {
        if (mDatas != null)
            mDatas.addAll(appendedData);
        notifyDataSetChanged();
    }

    public void clearData() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

         V viewHolder = null;
        if (view == null) {


            viewHolder = getViewHolder();
            view = mInflater.inflate(getItemLayoutId(), null);
            onCreateItemView(position, viewHolder, view);

            view.setTag(viewHolder);
        } else {
            viewHolder = (V) view.getTag();
        }

        onCreateItemData(view, viewHolder, position);

        return view;
    }


    /**
     * Retrieve the current item layou id.
     *
     * @return
     */
    protected abstract int getItemLayoutId();


    /**
     * Retrieve an instance of ViewHolder which was inherited from baseViewHolder
     *
     * @return
     */
    protected abstract V getViewHolder();

    /**
     * Called when the item view was first created.
     *
     * @param position
     * @param viewHolder
     * @param view
     */
    protected abstract void onCreateItemView(int position, V viewHolder, View view);

    /**
     * Called when the item view was filled with data.
     *
     * @param view
     * @param viewHolder
     * @param position
     */
    protected abstract void onCreateItemData(View view, V viewHolder, int position);


}

下面是一个demo:

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Tata on 15/7/7.
 */
public class DemoAdapter extends AbstractBaseAdapter<DemoAdapter.Info, DemoAdapter.ViewHolder> {

    //the item layout resource id
    private int layoutid = 0x001;

    public DemoAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    protected int getItemLayoutId() {
        return layoutid;
    }

    @Override
    protected ViewHolder getViewHolder() {
        return new ViewHolder();
    }


    @Override
    protected void onCreateItemView(int position, ViewHolder viewHolder, View view) {

        viewHolder.icon = (ImageView) view.findViewById(000);
        viewHolder.name = (TextView) view.findViewById(0000);

    }

    @Override
    protected void onCreateItemData(View view, ViewHolder viewHolder, int position) {

        viewHolder.icon.setImageResource(0);
        viewHolder.name.setText("tata");
    }

    static class ViewHolder {
        private TextView name;
        private ImageView icon;
    }


    /**
     * data-bean
     */
    class Info {
        private String name;
        private int resId;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getResId() {
            return resId;
        }

        public void setResId(int resId) {
            this.resId = resId;
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41913次
    • 积分:743
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:4篇
    • 译文:1篇
    • 评论:6条