当我们在使用ListView或者GridView时,通常需要一个Adapter,而这个Adapter通常会继承BaseAdapter,在自定义的Adapter里面会使用ViewHolder来提高程序的效率。传统的ViewHolder模式使得程序能够提高一定的效率, 而今天我将记录一下怎么进一步的封装Adapter,使得更加的通用 -- 来自慕课网的学习
原始ViewHolder和Adapter的写法:
package com.example.android_commonadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by 前世诀别的一纸书 on 2017/2/28.
*/
public class MyAdapter extends BaseAdapter {
private LayoutInflater mLayoutInflater = null;
private List mDatas = null;
public MyAdapter(Context cotnext, List datas)
{
mDatas = datas;
mLayoutInflater = LayoutInflater.from(cotnext);
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object 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 = null;
if(convertView == null)
{
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.item_layout, parent, false);
holder.mImageView = (ImageView) convertView.findViewById(R.id.id_imageView);
holder.mTextViewTitle = (TextView) convertView.findViewById(R.id.id_textViewTitle);
holder.mTextViewContent = (TextView) convertView.findViewById(R.id.id_textViewContent);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bean bean = mDatas.get(position);
holder.mImageView.setImageResource(bean.mImageViewIcon);
holder.mTextViewTitle.setText(bean.mTextViewTitle);
holder.mTextViewContent.setText(bean.mTextViewContent);
return convertView;
}
private class ViewHolder
{
public ImageView mImageView = null;
public TextView mTextViewTitle = null;
public TextView mTextViewContent = null;
}
}
从这个原始的写法,可以看出一个Adapter只能被一个ListView所用,同时一个ViewHolder只能被一个Adapter所用,代码的重用性太低了。
1.定义通用的ViewHolder
package com.example.android_commonadapter;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by 前世诀别的一纸书 on 2017/2/28.
*/
public class ViewHolder {
private int mPosition = 0;
private View mConvertView = null;
private SparseArray mViews;
private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
mPosition = position;
mViews = new SparseArray<>();
mConvertView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
mConvertView.setTag(this);
}
public static ViewHolder get(Context context, ViewGroup parent, int layoutId, int position, View convertView)
{
if(convertView == null)
{
return new ViewHolder(context, parent, layoutId, position);
}
else
{
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
public 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;
}
}
从上面的代码中,要注意几点:
1、通常我们使用Map来存储每一个控件,对应的key则是view的id。如果我们在安卓中,key是int类型,#### value是Object类型的话,则可以SparseArray类来代替Map,因为SparseArray是效率比Map更高。
2、在我们重用ViewHolder的时候,一定要更新ViewHolder的position, 比如上述代码中的35行。
3、getView方法则是根据View的id来获取View控件。
定义了通用的ViewHoler,我们在Adapter中只需要这样使用就行。
Bean bean = mDatas.get(position);
com.example.android_commonadapter.ViewHolder viewHolder = com.example.android_commonadapter.ViewHolder.get(mContext, parent, R.layout.item_layout, position, convertView);
((ImageView)viewHolder.getView(R.id.id_imageView)).setImageResource(bean.mImageViewIcon);
((TextView)viewHolder.getView(R.id.id_textViewTitle)).setText(bean.mTextViewTitle);
((TextView)viewHolder.getView(R.id.id_textViewContent)).setText(bean.mTextViewContent);
return viewHolder.getConvertView();
2.定义通用的Adapter
首先我们定义一个抽象类,继承BaseAdapter,然后实现一些必须的方法;
其次,我们在定义一个我们自己的Adapter继承这个抽象类,实现该实现的方法。
package com.example.android_commonadapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Created by 前世诀别的一纸书 on 2017/2/28.
*/
public abstract class CommonAdapter extends BaseAdapter{
protected List mDatas = null;
protected Context mContext = null;
public CommonAdapter(Context context, List datas)
{
mDatas = datas;
mContext = context;
}
@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 viewHolder = ViewHolder.get(mContext, parent, R.layout.item_layout, position, convertView);
convert(getItem(position), viewHolder);
return viewHolder.getConvertView();
}
protected abstract void convert(T t, ViewHolder viewHolder);
}
定义自己的Adapter,继承抽象类
public class MyAdapter2 extends CommonAdapter {
public MyAdapter2(Context context, List datas) {
super(context, datas);
}
@Override
protected void convert(Bean bean, ViewHolder viewHolder) {
((ImageView)viewHolder.getView(R.id.id_imageView)).setImageResource(bean.mImageViewIcon);
((TextView)viewHolder.getView(R.id.id_textViewTitle)).setText(bean.mTextViewTitle);
((TextView)viewHolder.getView(R.id.id_textViewContent)).setText(bean.mTextViewContent);
}
}
3.Item抢占焦点
当我们在在ListView的每一个Item里面加一个CheckBox,会发现Item不能点击,而CheckBox却能点击,这是为什么呢?
那是因为CheckBox抢占了焦点,解决办法就是:1、在CheckBox上面设置Android:focusable = false; 2、在父布局上设置 android:descendantFocusability="beforeDescendants"