传统的
package cct.commonadapter.bean;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import cct.commonadapter.R;
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List
public MyAdapter(Context context,List
inflater=LayoutInflater.from(context);
this.mdata=mdata;
}
@Override
public int getCount() {
return 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) {
ViewHolders holder;
if(convertView==null){
convertView=inflater.inflate(R.layout.item,parent,false);
holder=new ViewHolders();
holder.mtitle= (TextView) convertView.findViewById(R.id.mtitle);
holder.mdesc= (TextView) convertView.findViewById(R.id.mdesc);
holder.mtime= (TextView) convertView.findViewById(R.id.mtime);
holder.mphone= (TextView) convertView.findViewById(R.id.mphone);
convertView.setTag(holder);
}
else {
holder = (ViewHolders) convertView.getTag();
}
Message ms = mdata.get(position);
holder.mtitle.setText(ms.getTitle());
holder.mdesc.setText(ms.getDesc());
holder.mtime.setText(ms.getTime());
holder.mphone.setText(ms.getPhone());
return convertView;
}
private class ViewHolders
{
TextView mtitle;
TextView mdesc;
TextView mtime;
TextView mphone;
}
}
附上Msessage类
public class Message {
private String title;
private String desc;
private String time;
private String phone;
public Message(int position) {
}
public Message(String title, String desc, String time, String phone) {
this.title = title;
this.desc = desc;
this.time = time;
this.phone = phone;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
万能适配器
//CommonAdapter
1 package cct.commonadapter.adapterUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
import cct.commonadapter.R;
public abstract class CommonAapter
protected Context context;
protected LayoutInflater inflater;
protected List
public CommonAapter(Context context, List<T> mdata) {
this.context=context;
inflater=LayoutInflater.from(context);
this.mdata=mdata;
}
@Override
public int getCount() {
return 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 holder=ViewHolder.get(context,convertView,parent, R.layout.item,position);
covert(holder,getItem(position));
return holder.getMconvertView();
}
public abstract void covert(ViewHolder holder, T t) ;
}
通用的Viewholder
package cct.commonadapter.adapterUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewHolder {
private SparseArray
protected int mPosition;
private View mConvertView;
private Context mContext;
protected int mLayoutId;
/\*\*初始化一个ViewHolder
\* @param context
\* @param itemView
\* @param parent
\* @param position
\*/
public ViewHolder(Context context, View itemView, ViewGroup parent, int position) {
mContext = context;
mConvertView = itemView;
mPosition = position;
mViews = new SparseArray<View>();
mConvertView.setTag(this);
}
/\*\*入口方法:用来判断页面是否加载过
\* @param context
\* @param convertView
\* @param parent
\* @param layoutId
\* @param position
\* @return
\*/
public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null)
{
View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
ViewHolder holder = new ViewHolder(context, itemView, parent, position);
holder.mLayoutId = layoutId;
return holder;
} else
{
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
return holder;
}
}
public View getMconvertView() {
return mConvertView;
}
/\*\*
\* 通过viewId获取View
\* @param viewId
\* @param <T>
\* @return
\*/
public <T extends View>T getView(int viewId){
View view=mview.get(viewId);
if(view==null)
{view=mconvertView.findViewById(viewId);
mview.put(viewId,view);
}
return (T)view;
}
public int getLayoutId()
{
return mLayoutId;
}
public void updatePosition(int position)
{
mPosition = position;
}
public int getItemPosition()
{
return mPosition;
}
/\*\*通过得到控件id,和类的属性名
\* @param viewId
\* @param text
\* @return
\*/
public ViewHolder setTexts(int viewId,String text){
TextView tv=getView(viewId);
tv.setText(text);
return this;
}
public ViewHolder setImageBitmap(int viewId, Bitmap map){
ImageView iv=getView(viewId);
iv.setImageBitmap(map);
return this;
}
public ViewHolder setImageResource(int viewId, int resId)
{
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
public ViewHolder setImageDrawable(int viewId, Drawable drawable)
{
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
public ViewHolder setBackgroundColor(int viewId, int color)
{
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
public ViewHolder setBackgroundRes(int viewId, int backgroundRes)
{
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
public ViewHolder setTextColor(int viewId, int textColor)
{
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
public ViewHolder setTextColorRes(int viewId, int textColorRes)
{
TextView view = getView(viewId);
view.setTextColor(mContext.getResources().getColor(textColorRes));
return this;
}
}
任意一个适配器只需要继承上面封装好的适配器
package cct.commonadapter.bean;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import cct.commonadapter.R;
import cct.commonadapter.adapterUtil.CommonAapter;
import cct.commonadapter.adapterUtil.ViewHolder;
import cct.commonadapter.bean.Message;
public class MyCommonAdapter extends CommonAapter
public MyCommonAdapter(Context context, List
super(context,mdata);
}
@Override
public void covert(ViewHolder holder, Message ms) {
holder.setTexts(R.id.mtitle,ms.getTitle())
.setTexts(R.id.mdesc,ms.getDesc())
.setTexts(R.id.mtime,ms.getTime())
.setTexts(R.id.mphone,ms.getPhone());
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章