打造万能Adapter(适配器)——适用于ListView、GridListView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvyoujt/article/details/51599220

整理总结自鸿洋的博客:http://blog.csdn.net/lmj623565791/article/details/38902805/

一、利用普通的Adapter实现ListView列表——这是最基础的适配器

以下代码是最普通的实现方法:

1、MainActiviy.java

public class MainActivity extends Activity {

    private List<String> mData = new ArrayList<String>(Arrays.asList("HongYang", "GuoLin", "RenYuGang", "Jiatao"));
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);

        MyAdapter mAdapter = new MyAdapter(context, mData);
        listView.setAdapter(mAdapter);
    }
}

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".act.MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

3、MyAdapter.java

/**
 * Created by jiatao on 2016/6/10.
 * 这是最普通的适配器:
 *      MyAdapter继承BaseAdapter,然后getView里面使用ViewHolder模式;
 *      一般情况下,我们的写法是这样的:对于不同布局的ListView,我们会有一个对应的Adapter,在Adapter中又会有一个ViewHolder类来提高效率。
 *      这样出现ListView就会出现与之对应的Adapter类、ViewHolder类;
 *  那么有没有办法减少我们的编码呢?第二部分中将初步实现
 */
public class MyAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<String> mData;

    public MyAdapter(Context mContext, List<String> mData) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

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

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_string_listview, parent, false);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_title.setText(mData.get(position));
        return convertView;
    }

    class ViewHolder {
        private TextView tv_title;
    }
}

4、item_string_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#aa111111"
    android:gravity="center_vertical"
    android:paddingLeft="15dp"
    android:text="hello"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:textStyle="bold">

</TextView>


二、打造通用的ViewHolder——使能在外部获得ViewHolder、相关View控件、convertView

1、MainActiviy.java

同上

2、activity_main.xml

同上

3、ViewHolder.java

/**
 * Created by jiatao on 2016/6/11.
 * 通用的ViewHolder
 * 1、ViewHolder的作用是通过convertView.setTag与convertView进行绑定,然后当convertView复用时,直接从与之对于的ViewHolder(getTag)中拿到convertView布局中的控件,省去了findViewById的时间;
 * 2、也就是说,实际上们每个convertView会绑定一个ViewHolder对象,这个viewHolder主要用于帮convertView存储布局中的控件;
 * 3、那么我们只要写出一个通用的ViewHolder,然后对于任意的convertView,提供一个对象让其setTag即可;
 * 4、既然是通用,那么我们这个ViewHolder就不可能含有各种控件的成员变量了,因为每个Item的布局是不同的,最好的方式是什么呢?
 * 5、提供一个容器,专门存每个Item布局中的所有控件,而且还要能够查找出来;既然需要查找,那么ListView肯定是不行了,需要一个键值对进行保存,键为控件的Id,值为控件的引用;
 * 6、相信大家立刻就能想到Map;但是我们不用Map,因为有更好的替代类,就是我们android提供的SparseArray这个类,和Map类似,但是比Map效率,不过键只能为Integer.
 */
public class ViewHolder {

    private SparseArray<View> mViews;
    private View mConvertView;

    //构造函数
    private ViewHolder(Context context, int resLayoutId, ViewGroup parent) {
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(resLayoutId, parent, false);
        this.mConvertView.setTag(this);
    }

    //获取一个ViewHolder
    public static ViewHolder getHolder(Context context, int resLayoutId, View convertView, ViewGroup parent) {
        if (convertView == null) {
            return new ViewHolder(context, resLayoutId, parent);
        }
        return (ViewHolder) convertView.getTag();
    }

    //通过控件的id获取对应的控件,如果没有则加入mViews;记住 <T extends View> T 这种用法
    public <T extends View> T getItemView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    //获得一个convertView
    public View getmConvertView() {
        return mConvertView;
    }

}

4、MyAdapter.java

/**
 * Created by jiatao on 2016/6/10.
 * 这是使用了通用的ViewHolder的适配器
 *  除了getView,其他方法都一样:
 *      1、调用ViewHolder的get方法:
 *          如果convertView为null,new一个ViewHolder实例,通过使用mInflater.inflate加载布局,然后new一个SparseArray用于存储View,最后setTag(this);
 *          如果存在那么直接getTag;
 *      2、通过getView(id)获取控件,如果存在则直接返回,否则调用findViewById,返回存储,返回。
 *      3、最后通过holder.getmConvertView()返回视图;
 */
public class MyAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<String> mData;

    public MyAdapter(Context mContext, List<String> mData) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

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

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //实例化一个ViewHolder
        ViewHolder holder = ViewHolder.getHolder(mContext, R.layout.item_string_listview, convertView, parent);
        //获取控件
        TextView view = holder.getItemView(R.id.tv_title);
        //给控件赋值
        view.setText(mData.get(position));
        return holder.getmConvertView();
    }
}

5、item_string_listview.xml

同上


三、初步打造通用的Adapter——通过泛型支持所有数据集合,抽取公共方法封装成抽象基类,对外开发getView方法

1、MainActiviy.java

同上

2、activity_main.xml

同上

3、ViewHolder.java

同上

4、ComAdapter.java

/**
 * Created by jiatao on 2016/6/11.
 * 通用的适配器
 * 1、<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">ComAdapter</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">需要保持一个List对象,存储一个Bean的集合,不同的ListView,Bean肯定是不同的,这个ComAdapter肯定需要支持泛型,内部维持一个List<T></span>
 * 2、ComAdapter是一个抽象类,提取getView以外的方法,getView方法留给子类去实现
 */
public abstract class ComAdapter<T> extends BaseAdapter {

    protected LayoutInflater mLayoutInflater;
    protected Context mContext;
    protected List<T> mData;

    public ComAdapter(Context mContext, List<T> mData) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

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

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

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

5、MyAdapter.java

/**
 * Created by jiatao on 2016/6/11.
 * 继承ComAdapter抽象类,而且使用了通用的ViewHolder的适配器
 */
public class MyAdapter<T> extends ComAdapter {

    public MyAdapter(Context mContext, List<T> mData){
        super(mContext, mData);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //实例化一个ViewHolder
        ViewHolder holder = ViewHolder.getHolder(mContext, R.layout.item_string_listview, convertView, parent);
        //获取控件
        TextView view = holder.getItemView(R.id.tv_title);
        //给控件赋值
        view.setText((String) mData.get(position));
        return holder.getmConvertView();
    }
}

6、item_string_listview.xml

同上


四、进一步打造通用的Adapter——使用匿名内部类获得适配器

1、MainActiviy.java

public class MainActivity extends Activity {

    private List<String> mData = new ArrayList<String>(Arrays.asList("HongYang", "GuoLin", "RenYuGang", "Jiatao"));
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);

        //直接使用匿名内部类即可取得适配器
        ComAdapter mAdapter = new ComAdapter<String>(context, mData, R.layout.item_string_listview) {
            @Override
            public void convert(ViewHolder holder, String item) {//通过ViewHolder找到iew,通过Item设置值
                TextView view =holder.getItemView(R.id.tv_title);
                view.setText(item);
            }
        };
        listView.setAdapter(mAdapter);
    }
}

2、activity_main.xml

同上

3、ViewHolder.java

同上

4、ComAdapter.java

/**
 * Created by jiatao on 2016/6/11.
 * 通用的适配器
 * 1、所有的Adapter的第一行(ViewHolder viewHolder = getViewHolder(position, convertView,parent);)和 最后一行:return viewHolder.getConvertView();一定是一样的。
 * 2、那么我们可以这样做:我们把第一行和最后一行写死,把中间变化的部分抽取出来,这不就是OO的设计原则嘛。
 */
public abstract class ComAdapter<T> extends BaseAdapter {

    protected LayoutInflater mLayoutInflater;
    protected Context mContext;
    protected List<T> mData;
    protected int mItemLayoutId;

    public ComAdapter(Context mContext, List<T> mData, int mItemLayoutId) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
        this.mItemLayoutId = mItemLayoutId;
    }

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

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //实例化一个ViewHolder
        ViewHolder holder = getViewHolder(position, convertView, parent);
        //使用对外公开的convert方法,通过ViewHolder把View找到,通过Item设置值
        convert(holder, getItem(position));
        return holder.getmConvertView();
    }

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent){
        return ViewHolder.getHolder(mContext, mItemLayoutId, convertView, parent);
    }

    /**
     * 对外公布了一个convert方法,并且还把ViewHolder和本item对应的Bean对象给传出去
     * 现在convert方法里面需要干嘛呢?通过ViewHolder把View找到,通过Item设置值
     */
    public abstract void convert(ViewHolder holder, T item);

}

5、MyAdapter.java

不再使用,删除即可

6、item_string_listview.xml

同上


五、打造终极版通用Adapter——进一步完善ViewHolder使匿名内部类中convert方法只需一行代码即能给对应的view赋值

1、MainActiviy.java

/**
 * 通过匿名内部类获得适配器,适配内的convert方法只需一行代码即可为对应的view赋值
 */
public class MainActivity extends Activity {

    private List<String> mData = new ArrayList<String>(Arrays.asList("HongYang", "GuoLin", "RenYuGang", "Jiatao"));
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);

        //直接使用匿名内部类即可取得适配器
        ComAdapter mAdapter = new ComAdapter<String>(context, mData, R.layout.item_string_listview) {
            @Override
            public void convert(ViewHolder holder, String item) {//通过ViewHolder找到view,通过Item设置值
                holder.setTextView(R.id.tv_title, item);//只需一行代码即可实现:给对应的view赋值
            }
        };
        listView.setAdapter(mAdapter);
    }
}

2、activity_main.xml

同上

3、ViewHolder.java

/**
 * Created by jiatao on 2016/6/11.
 * 通用的ViewHolder
 * 1、ViewHolder的作用是通过convertView.setTag与convertView进行绑定,然后当convertView复用时,直接从与之对于的ViewHolder(getTag)中拿到convertView布局中的控件,省去了findViewById的时间;
 * 2、也就是说,实际上们每个convertView会绑定一个ViewHolder对象,这个viewHolder主要用于帮convertView存储布局中的控件;
 * 3、那么我们只要写出一个通用的ViewHolder,然后对于任意的convertView,提供一个对象让其setTag即可;
 * 4、既然是通用,那么我们这个ViewHolder就不可能含有各种控件的成员变量了,因为每个Item的布局是不同的,最好的方式是什么呢?
 * 5、提供一个容器,专门存每个Item布局中的所有控件,而且还要能够查找出来;既然需要查找,那么ListView肯定是不行了,需要一个键值对进行保存,键为控件的Id,值为控件的引用;
 * 6、相信大家立刻就能想到Map;但是我们不用Map,因为有更好的替代类,就是我们android提供的SparseArray这个类,和Map类似,但是比Map效率,不过键只能为Integer.
 * <p/>
 * 最后的封装:
 * 我们现在在convertView里面需要这样:
 *
 * @Override public void convert(ViewHolder viewHolder, String item) {
 * TextView view = viewHolder.getView(R.id.id_tv_title);
 * view.setText(item);
 * }
 * 我们细想一下,其实布局里面的View常用也就那么几种:ImageView,TextView,Button,CheckBox等等;
 * 那么我觉得ViewHolder还可以封装一些常用的方法,比如setText(id,String);setImageResource(viewId, resId);setImageBitmap(viewId, bitmap);
 */
public class ViewHolder {

    private SparseArray<View> mViews;
    private View mConvertView;

    //构造函数
    private ViewHolder(Context context, int resLayoutId, ViewGroup parent) {
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(resLayoutId, parent, false);
        this.mConvertView.setTag(this);
    }

    //获取一个ViewHolder
    public static ViewHolder getHolder(Context context, int resLayoutId, View convertView, ViewGroup parent) {
        if (convertView == null) {
            return new ViewHolder(context, resLayoutId, parent);
        }
        return (ViewHolder) convertView.getTag();
    }

    //通过控件的id获取对应的控件,如果没有则加入mViews;记住 <T extends View> T 这种用法
    public <T extends View> T getItemView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    //获得一个convertView
    public View getmConvertView() {
        return mConvertView;
    }

    /**
     * 为TextView赋值
     */
    public void setTextView(int viewId, String text) {
        TextView view = getItemView(viewId);
        view.setText(text);
    }

    /**
     * 为ImageView赋值——drawableId
     */
    public void setImageResource(int viewId, int drawableId) {
        ImageView view = getItemView(viewId);
        view.setImageResource(drawableId);
    }

    /**
     * 为ImageView赋值——bitmap
     */
    public void setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getItemView(viewId);
        view.setImageBitmap(bitmap);
    }

}

4、ComAdapter.java

同上

5、MyAdapter.java

不再使用,删除即可

6、item_string_listview.xml

同上


六、实例运用

1、MainActiviy.java

/**
 * 运用万能适配器实现复杂视图的实例
 */
public class MainActivity extends Activity {

    private List<ArticleBean> beanlist;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listview);

        beanlist = new ArrayList<ArticleBean>();
        for(int i=1;i<10;i++){
            ArticleBean bean = new ArticleBean("文章"+i,"内容"+i+":周三早上丢失了红色钱包,在食堂二楼","2016060"+i,"1718013691"+i);
            beanlist.add(bean);
        }

        //直接使用匿名内部类即可取得适配器
        ComAdapter mAdapter = new ComAdapter<ArticleBean>(context, beanlist, R.layout.item_example_listview) {
            @Override
            public void convert(ViewHolder holder, ArticleBean item) {//通过ViewHolder找到view,通过Item设置值
                holder.setTextView(R.id.tv_title, item.getTitle());
                holder.setTextView(R.id.tv_describe, item.getContent());
                holder.setTextView(R.id.tv_time, item.getTime());
                holder.setTextView(R.id.tv_phone, item.getPhone());
            }
        };
        listView.setAdapter(mAdapter);
    }
}

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".act.MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

3、ViewHolder.java

public class ViewHolder {

    private SparseArray<View> mViews;
    private View mConvertView;

    //构造函数
    private ViewHolder(Context context, int resLayoutId, ViewGroup parent) {
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(resLayoutId, parent, false);
        this.mConvertView.setTag(this);
    }

    //获取一个ViewHolder
    public static ViewHolder getHolder(Context context, int resLayoutId, View convertView, ViewGroup parent) {
        if (convertView == null) {
            return new ViewHolder(context, resLayoutId, parent);
        }
        return (ViewHolder) convertView.getTag();
    }

    //通过控件的id获取对应的控件,如果没有则加入mViews;记住 <T extends View> T 这种用法
    public <T extends View> T getItemView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    //获得一个convertView
    public View getmConvertView() {
        return mConvertView;
    }

    /**
     * 为TextView赋值
     */
    public ViewHolder setTextView(int viewId, String text) {
        TextView view = getItemView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView赋值——drawableId
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getItemView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    /**
     * 为ImageView赋值——bitmap
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getItemView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

}

4、ComAdapter.java

public abstract class ComAdapter<T> extends BaseAdapter {

    protected LayoutInflater mLayoutInflater;
    protected Context mContext;
    protected List<T> mData;
    protected int mItemLayoutId;

    public ComAdapter(Context mContext, List<T> mData, int mItemLayoutId) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
        this.mItemLayoutId = mItemLayoutId;
    }

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

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //实例化一个ViewHolder
        ViewHolder holder = getViewHolder(position, convertView, parent);
        //使用对外公开的convert方法,通过ViewHolder把View找到,通过Item设置值
        convert(holder, getItem(position));
        return holder.getmConvertView();
    }

    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent){
        return ViewHolder.getHolder(mContext, mItemLayoutId, convertView, parent);
    }

    /**
     * 对外公布了一个convert方法,并且还把ViewHolder和本item对应的Bean对象给传出去
     * 现在convert方法里面需要干嘛呢?通过ViewHolder把View找到,通过Item设置值
     */
    public abstract void convert(ViewHolder holder, T item);

}

5、item_string_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="红色钱包"
        android:textColor="#444444"
        android:textSize="16sp"></TextView>

    <TextView
        android:id="@+id/tv_describe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginTop="10dp"
        android:maxLines="2"
        android:minLines="1"
        android:text="周三早上丢失了红色钱包,在食堂二楼"
        android:textColor="#898989"
        android:textSize="16sp"></TextView>

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_describe"
        android:layout_marginTop="10dp"
        android:text="20130240122"
        android:textColor="#898989"
        android:textSize="12sp"></TextView>

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/tv_describe"
        android:layout_marginTop="10dp"
        android:background="#5cbe6c"
        android:drawableLeft="@mipmap/icon_photo"
        android:drawablePadding="5dp"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="3dp"
        android:text="138024249542"
        android:textColor="#ffffff"
        android:textSize="12sp"></TextView>

</RelativeLayout>

运行结果如图:


阅读更多

没有更多推荐了,返回首页