介面卡基類--萬能介面卡(BaseAdapter)
阿新 • • 發佈:2019-01-04
一個抽象類繼承BaseAdapter
ViewHolderpackage 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); }
根據自己需求,增加一些方法
下面是一個Demopackage 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();
}
});
}
}
}
}
效果圖如下: