标签:
一个抽象类继承BaseAdapter
package com.lei.adapter; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public abstract class MyBaseAdapter<T> extends android.widget.BaseAdapter { private List<T> data = new ArrayList<T>(); private Context context; private int layoutId; public MyBaseAdapter(Context context, int layoutId, List<T> datas) { this(context, layoutId); this.data = datas; } public MyBaseAdapter(Context context, int layoutId) { this.context = context; this.layoutId = layoutId; } public int getCount() { return data.size(); } public T getItem(int position) { return data.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.newInstance(context, convertView, layoutId, parent, position); setViewDetails(holder, getItem(position)); return holder.getConvertView(); } <span style="white-space:pre"> </span>//要实现的抽象方法,具体为控件赋值 public abstract void setViewDetails(ViewHolder holder, T data); }
根据自己需求,增加一些方法
package com.lei.adapter; 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 View mContentView; private SparseArray<View> mViews;// 装控件 private int mPosition; private Context context; public static ViewHolder newInstance(Context context, View convertView, int layoutId, ViewGroup group, int position) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(context, layoutId, group, position); } else { holder = (ViewHolder) convertView.getTag(); holder.mPosition = position; } return holder; } private ViewHolder(Context context, int layoutId, ViewGroup group, int position) { this.context = context; mViews = new SparseArray<View>(); this.mPosition = position; mContentView = LayoutInflater.from(context).inflate(layoutId, group, false); mContentView.setTag(this); } public <T extends View> T getViewById(int resId) { View view = mViews.get(resId); if (null == view) { view = mContentView.findViewById(resId); mViews.put(resId, view); } return (T) view; } /** * 为textview设置内容 * * @param resId * @param text */ public void setTextForTv(int resId, String text) { TextView tv = getViewById(resId); tv.setText(text); } /** * 设置文字颜色 * * @param resId * @param colorId */ public void setTextColor(int resId, int colorId) { TextView tv = getViewById(resId); tv.setTextColor(colorId); } public void setImageResource(int resId, int imgId) { ImageView img = getViewById(resId); img.setImageResource(imgId); } public void setImageBitmap(int resId, Bitmap bitmap) { ImageView img = getViewById(resId); img.setImageBitmap(bitmap); if (null != bitmap) { bitmap.recycle(); } } public View getConvertView() { return mContentView; } public int getPosition() { return mPosition; } }
package com.lei.vpdemo; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import com.lei.adapter.MyBaseAdapter; import com.lei.adapter.ViewHolder; public class MyListAct extends ListActivity { List<String> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initData(); ListAdapter adapter = new ListAdapter(this, android.R.layout.simple_list_item_multiple_choice, mData); setListAdapter(adapter); } private void initData() { mData = new ArrayList<String>(); mData.add("JAVA"); mData.add("Android"); mData.add("浪潮之巅"); mData.add("疯狂讲义"); }
<span style="white-space:pre"> </span>//adapter 实现类,只需要实现它的抽象方法和构造函数
public class ListAdapter extends MyBaseAdapter<String> { Context context; public ListAdapter(Context context, int layoutId, List<String> datas) { super(context, layoutId, datas); this.context = context; } // 回调,为item赋值 @Override public void setViewDetails(ViewHolder holder, final String data) { holder.setTextForTv(android.R.id.text1, data); //通过个体position为item设置个性化 if (holder.getPosition() == 2) { holder.setTextColor(android.R.id.text1, Color.GREEN); } if (holder.getPosition() == 3) { holder.getViewById(android.R.id.text1).setOnClickListener( new OnClickListener() { public void onClick(View v) { Toast.makeText(context, data, Toast.LENGTH_LONG) .show(); } }); } } } }
效果图如下:
标签:
原文地址:http://blog.csdn.net/qq_31116683/article/details/51329850