1.ViewHolder类
原始类
class ViewHolder{
TextView name;
TextView age;
}
封装类
/**
* 封装视图类
* @author nate
*/
public class CommViewHolder
{
/**
* 视图容器
*/
private SparseArray<View> mViews;
/**
* 位置标识
*/
private int mPosition;
/**
* 视图
*/
private View mConvertView;
/**
* 私有化构造方法,不让外界进行使用
* @param context
* @param parent
* @param layoutId
* @param position
*/
private CommViewHolder(Context context, ViewGroup parent, int layoutId, int position)
{
this.mViews = new SparseArray<View>();
this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
this.mPosition = position;
mConvertView.setTag(this);
}
/**
*
* @param context
* @param parent
* @param layoutId
* @param position
* @param convertView
* @return 静态方法获取到viewholder类实例
*/
public static CommViewHolder get(Context context, ViewGroup parent, int layoutId, int position, View convertView)
{
if(convertView == null)
{
return new CommViewHolder(context, parent, layoutId, position);
}
else
{
CommViewHolder holder = (CommViewHolder) convertView.getTag();
// 复用视图,但是position要更新
holder.mPosition = position;
return holder;
}
}
/**
*
* @param viewId 控件id
* @return 根据控件id获取到控件
*/
@SuppressWarnings("unchecked")
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;
}
/**
* @return 返回视图类
*/
public View getmConvertView()
{
return mConvertView;
}
/**
* @param viewId
* @param content
* @return 设置textview相关
*/
public CommViewHolder setTextView(int viewId, String content)
{
TextView tv = getView(viewId);
tv.setText(content);
return this;
}
// TODO 可以根据自己的需要编写更多适用的方法。。。
}
2.Adapter类中Viewholder代码对比
使用原始holder
class MyAdapter extends ArrayAdapter<Person>
{
public MyAdapter(Context context, int resource, List<Person> objects)
{
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.age = (TextView) convertView.findViewById(R.id.age);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.name.setText(person.getName());
holder.age.setText(person.getAge());
return convertView;
}
class ViewHolder
{
TextView name;
TextView age;
}
}
使用封装holder
class MyAdapter extends ArrayAdapter<Person>{
public MyAdapter(Context context, int resource, List<Person> objects)
{
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Person person = getItem(position);
CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, R.layout.item_layout, position,convertView);
viewHolder.setTextView(R.id.name, person.getName()).setTextView(R.id.age, person.getAge());
return viewHolder.getmConvertView();
}
}
1.原始类
class MyAdapter extends ArrayAdapter<Person>
{
public MyAdapter(Context context, int resource, List<Person> objects)
{
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Person person = getItem(position);
CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, R.layout.item_layout, position,
convertView);
viewHolder.setTextView(R.id.name, person.getName()).setTextView(R.id.age, person.getAge());
return viewHolder.getmConvertView();
}
}
2.封装类
public abstract class CommAdapter<T> extends ArrayAdapter<T>
{
private int resourceId;
public CommAdapter(Context context, int resource, List<T> objects)
{
super(context, resource, objects);
this.resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
CommViewHolder viewHolder = CommViewHolder.get(getContext(), parent, resourceId, position, convertView);
setConvert(viewHolder, getItem(position));
return viewHolder.getmConvertView();
}
public abstract void setConvert(CommViewHolder viewHolder, T t);
}
抢占焦点控件抢占了焦点:列如button checkbox等
方法一:为抢占焦点的控件布局添加:android:focusable="false";
方法二:为整个listview的item布局添加android:descendantFocusability="blocksDescendants"
1.未bean对象添加isChecked属性,更具isChecked属性在adapter中动态设置checkBox是否选中
2.在adapter中设置一个记录选中checkbox的list集合;
final CheckBox cb = viewHolder.getView(R.id.cb);
cb.setChecked(false);
if(mPos.contains(viewHolder.getPosition()))
{
cb.setChecked(true);
}
cb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("guxuewu", "点击了cb");
if(cb.isChecked())
{
mPos.add(viewHolder.getPosition());
}
else
{
//希望执行remove object方法,所以进行强转
mPos.remove((Integer) viewHolder.getPosition());
}
}
});
原文:http://blog.csdn.net/u011771755/article/details/45270705