标签:
一、分析
1通用Viewhold类:
(1)利用一个容器SparseArray将View进行封装
(2)提供Viewhold方法,new一个convertview;
提供Viewhold get方法,实现converview的复用;
提供getView方法,实现控件的findviewbyid
提供setText方法,实现控件值的设置
2抽象的通用Adpter类:
(1)类中的convet方法为抽象方法,继承Adpter时,可在此方法中,实现相应代码;
(2)提供 getcount、getitem、getitemid getview方法
3调用
(1)将Viewhold类和Adapter建一个包;
(2)自己的adpter继承通用adpter,在covert方法中,new一个Viewhold类
(3)hold.getview或者hold.setText两种方法设置相应控件的值
二、代码如下:
1通用Viewhold类:
package com.qixingbang.qxb.common.listviewutil;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by cr30 on 2015/9/24.
*/
public class MyViewHold {
private SparseArray<View> Views;
private SparseArray<RelativeLayout>Layouts;
private View mView;
private View mconvertview;
private int mposition;
public MyViewHold(Context context,int position,ViewGroup parent,int layoutId)
{
this.mconvertview= LayoutInflater.from(context).inflate(layoutId,parent,false);
Views=new SparseArray<View>();
Layouts=new SparseArray<RelativeLayout>();
this.mposition=position;
mconvertview.setTag(this);
}
public static MyViewHold get(Context context,int position,View mconvertview,ViewGroup parent,int layoutId)
{
if(mconvertview==null)
{
return new MyViewHold(context,position,parent,layoutId);
}
else {
MyViewHold myViewHold=(MyViewHold)mconvertview.getTag();
myViewHold.mposition=position;
return myViewHold;
}
}
public <T extends View>T getView(int viewId)
{
mView=Views.get(viewId);
if(mView==null)
{
mView=mconvertview.findViewById(viewId);
Views.put(viewId,mView);
}
return (T)mView;
}
// 公布方法,方便其他类使用内部参数
public View getconvert()
{
return mconvertview;
}
public int getposition()
{
return mposition;
}
// 补充方法可以直接设置控件内容
public MyViewHold setText(int viewId,String text)
{
TextView mTextView=getView(viewId);
mTextView.setText(text);
return this;
}
public MyViewHold setImageResourse(int layoutId,int resId)
{
ImageView imageView=getView(layoutId);
imageView.setImageResource(resId);
return this;
}
}
2、通用adpter:
package com.qixingbang.qxb.common.listviewutil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Created by cr30 on 2015/9/24.
*/
public abstract class MyCommonAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mDatas;
protected LayoutInflater mInflater;
protected int mlayoutId;
public MyCommonAdapter(Context context,List<T> datas,int layoutId) {
this.mContext=context;
this.mInflater=LayoutInflater.from(context);
this.mDatas=datas;
this.mlayoutId=layoutId;
}
public int getCount() {
// TODO 自动生成的方法存根
return mDatas.size();
}
public T getItem(int position) {
// TODO 自动生成的方法存根
return mDatas.get(position);
}
public long getItemId(int position) {
// TODO 自动生成的方法存根
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// 每次使用时进行初始化viewholder
MyViewHold holder=MyViewHold.get(mContext,position, convertView, parent,
mlayoutId);
convert(holder, getItem(position));
return holder.getconvert();
}
public abstract void convert(MyViewHold holder,T t);
}
标签:
原文地址:http://www.cnblogs.com/huoshenmanbu/p/4842002.html