使适配器更加简单
传统写法
public class CustomerAdapter extends BaseAdapter {
private List<CustomerInfo> mCustomerList;
private LayoutInflater mInflater;
private Context context;
public CustomerAdapter(Context context, List<CustomerInfo> customerList)
{
mCustomerList = customerList;
mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(mCustomerList == null){
return 0;
}
return mCustomerList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView == null)
{
convertView =mInflater.inflate(R.layout.customer_item, null);
holder = new ViewHolder();
holder.tele=(TextView) convertView.findViewById(R.id.tv_tel);
holder.details=(TextView) convertView.findViewById(R.id.tv_write);
holder.time=(TextView) convertView.findViewById(R.id.tv_time);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.tele.setText(mCustomerList.get(position).getCustomerTel());
holder.details.setText(mCustomerList.get(position).getCustomerDetails());
holder.time.setText(mCustomerList.get(position).getCustomerTime());
return convertView;
}
public class ViewHolder
{
public TextView tele;
public TextView details;
public TextView time;
}
}
改进版本一
将ViewHolder作为一个通用的类
SparseArray //比传统Hashmap更加有效率 map<int,object>这种情况下
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.get(mContext,convertView,parent,R.layout.item,position);
CustomInfo a=mCustomerList.get(position);
((TextView)holder.getView(R.id.tele)).setText(a.getTele());
((TextView)holder.getView(R.id.details)).setText(a.getDetails());
((TextView)holder.getView(R.id.time)).setText(a.getTime());
return holder.getConvertView();
}
public class ViewHolder {
private SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
public ViewHolder(Context context,ViewGroup parent,int layoutId,int position)
{
this.mPosition=position;
this.mViews = new SparseArray<View>();
mConvertView=LayoutInflater.from(context).inflate(layoutId, parent,false);
mConvertView.setTag(this);
}
/**
* 入口方法
*/
public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position)
{
if(convertView == null)
{
return new ViewHolder(context, parent, layoutId, position);
}
else
{
ViewHolder holder = (ViewHolder) convertView.getTag();//convertView复用
holder.mPosition = position; //position需要不断变化
return holder;
}
}
/**
*返回ConvertView
*/
public View getConvertView()
{
return mConvertView;
}
/**
* 通过viewId获取控件
*/
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;
}
}
改进版本2
封装adapter
public abstract class CommonAdapter<T> extends BaseAdapter {
public Context mContext;
public List<T> mDatas;
public LayoutInflater mInflater;
public CommonAdapter(Context context , List<T> datas)
{
this.mContext = context;
mInflater = LayoutInflater.from(context);
this.mDatas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mDatas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mDatas.get(location);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public abstract View getView(int position, View convertView, ViewGroup parent);
}
public class Myadapter extends CommonAdapter<CustomInfo> {
public CustomerAdapter(Context context, List<CustomerInfo> customerList)
{
super(context,customerList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.get(mContext,convertView,parent,R.layout.item,position);
CustomInfo a=mCustomerList.get(position);
((TextView)holder.getView(R.id.tele)).setText(a.getTele());
((TextView)holder.getView(R.id.details)).setText(a.getDetails());
((TextView)holder.getView(R.id.time)).setText(a.getTime());
return holder.getConvertView();
}
}
再改进
public abstract class CommonAdapter<T> extends BaseAdapter {
public Context mContext;
public List<T> mDatas;
public LayoutInflater mInflater;
public CommonAdapter(Context context , List<T> datas)
{
this.mContext = context;
mInflater = LayoutInflater.from(context);
this.mDatas = datas;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mDatas.size();
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
return mDatas.get(location);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = ViewHolder.get(mContext,convertView,parent,R.layout.item,position);
convert(holder,getItem(position));
return holder.getConvertView();
}
private abstract void convert(ViewHolder holder, T t);
}
public class Myadapter extends CommonAdapter<CustomInfo> {
public CustomerAdapter(Context context, List<CustomerInfo> customerList)
{
super(context,customerList);
}
public void convert(ViewHolder holder,CustomInfo t)
{
((TextView)holder.getView(R.id.tele)).setText(a.getTele());
((TextView)holder.getView(R.id.details)).setText(a.getDetails());
((TextView)holder.getView(R.id.time)).setText(a.getTime());
}
}
再次小改进
ViewHolder类中
添加设置TextView的方法
public ViewHolder setText(int viewId,String text)
{
TextView tv=getView(viewId);
tv.setText(text);
return this;
}
Adapter中使用链式进一步简化代码量
holder.setText(,).setText.(,).setText(,);
MyaAdapter可以直接声明为内部类,减少代码量
mListView.setAdapter(new CommonAdapter<CustomInfo>(MainActivity.this,mDatas)
{
public void convert(ViewHolder holder,CustomInfo t)
{
public class Myadapter extends CommonAdapter<CustomInfo> {
public CustomerAdapter(Context context, List<CustomerInfo> customerList)
{
super(context,customerList);
}
public void convert(ViewHolder holder,CustomInfo t)
{
((TextView)holder.getView(R.id.tele)).setText(a.getTele());
((TextView)holder.getView(R.id.details)).setText(a.getDetails());
((TextView)holder.getView(R.id.time)).setText(a.getTime());
}
}
}
});
点击item没反应解决方法
1.focusable = false;//防止焦点被抢
2.descendantFocusablility="blockDescendants"//禁止焦点往下传递
conVertView复用出现的问题解决
private boolean ischecked;
private List<Integer> mPos = new ArrayList<Integer>();去记录点击的状态
final CheckBox cb = holder.getView(R.id......);
cb.setChecked(false);
if(mPos.contains(holder.getPosition()))
{
cb.setChecked(true);
}
cb.setOnClickListener(new OnClickListener()
public void onClick(View v)
{
mPos.add(holder.getPositon());
}else
{
mPos.remove((Integer)holder.getPosition());
}
);
本文来自博客园,作者:LeeJuly,转载请注明原文链接:https://www.cnblogs.com/peterleee/p/9373780.html