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 Yangzb on 2016/8/1.
* 所有 adapter 基类
* 抽象类不能被实例化
*/
public abstract class CommonAdapter< T> extends BaseAdapter {
protected Context mContext;
protected List< T> mDatas;
protected LayoutInflater mInflater;
private int layoutId;
public CommonAdapter(Context context,List< T> lists, int layoutId){
this. mContext = context;
mInflater = LayoutInflater. from(context);
this. layoutId = layoutId;
this. mDatas = lists;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem( int position) {
return mDatas.get(position);
}
@Override
public long getItemId( int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = ViewHolder. getViewHolder( mContext,convertView,parent, layoutId,position);
conVert(viewHolder,getItem(position));
return viewHolder.getConvertView();
}
public abstract void conVert(ViewHolder viewHolder, T t);
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Created by Yangzb on 2016/8/1.
* 所有 adapter 基类
* 抽象类不能被实例化
*/
public abstract class CommonAdapter< T> extends BaseAdapter {
protected Context mContext;
protected List< T> mDatas;
protected LayoutInflater mInflater;
private int layoutId;
public CommonAdapter(Context context,List< T> lists, int layoutId){
this. mContext = context;
mInflater = LayoutInflater. from(context);
this. layoutId = layoutId;
this. mDatas = lists;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem( int position) {
return mDatas.get(position);
}
@Override
public long getItemId( int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = ViewHolder. getViewHolder( mContext,convertView,parent, layoutId,position);
conVert(viewHolder,getItem(position));
return viewHolder.getConvertView();
}
public abstract void conVert(ViewHolder viewHolder, T t);
}
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Yangzb on 2016/8/1.
* 通用 veiwholder 类
*/
public class ViewHolder {
private SparseArray<View> mViews; // 存储 itme 控件
private int mPosition;
private View mConvertView;
public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this. mPosition = position;
this. mViews = new SparseArray<>();
mConvertView = LayoutInflater. from(context).inflate(layoutId,parent, false);
mConvertView.setTag( this); //this 就是当前的 viewholder
}
/**
* 获取 viewholder
* @param context
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static ViewHolder getViewHolder(Context context, View convertView, ViewGroup parent,
int layoutId, int position) {
if(convertView == null){
return new ViewHolder(context,parent,layoutId,position);
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder. mPosition = position; //convertView 复用 但 position 需要更新
return viewHolder;
}
}
/**
* 获取 converView
* @return
*/
public View getConvertView(){
return mConvertView;
}
/**
* 通过 viewId 获取控件
* @param viewId
* @param <T>
* @return
*/
public < T extends View> T getView( int viewId){
View view = mViews.get(viewId);
if(view == null){ // 判断当前 view 有没有存过
view = mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return ( T) view;
}
public int getmPosition() {
return mPosition;
}
/**
* 为 textview 设置值
* @param viewId
* @param text
* @return
*/
public ViewHolder setTvText( int viewId,String text){
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
/**
* 为 imgview 设置图片
* @param viewId
* @param imgResId
* @return
*/
public ViewHolder setImgRes( int viewId, int imgResId){
ImageView imageView = getView(viewId);
imageView.setImageResource(imgResId);
return this;
}
public ViewHolder setImgBtRes( int viewId, int imgResId){
ImageButton imageButton = getView(viewId);
imageButton.setImageResource(imgResId);
return this;
}
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Yangzb on 2016/8/1.
* 通用 veiwholder 类
*/
public class ViewHolder {
private SparseArray<View> mViews; // 存储 itme 控件
private int mPosition;
private View mConvertView;
public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this. mPosition = position;
this. mViews = new SparseArray<>();
mConvertView = LayoutInflater. from(context).inflate(layoutId,parent, false);
mConvertView.setTag( this); //this 就是当前的 viewholder
}
/**
* 获取 viewholder
* @param context
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static ViewHolder getViewHolder(Context context, View convertView, ViewGroup parent,
int layoutId, int position) {
if(convertView == null){
return new ViewHolder(context,parent,layoutId,position);
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
viewHolder. mPosition = position; //convertView 复用 但 position 需要更新
return viewHolder;
}
}
/**
* 获取 converView
* @return
*/
public View getConvertView(){
return mConvertView;
}
/**
* 通过 viewId 获取控件
* @param viewId
* @param <T>
* @return
*/
public < T extends View> T getView( int viewId){
View view = mViews.get(viewId);
if(view == null){ // 判断当前 view 有没有存过
view = mConvertView.findViewById(viewId);
mViews.put(viewId,view);
}
return ( T) view;
}
public int getmPosition() {
return mPosition;
}
/**
* 为 textview 设置值
* @param viewId
* @param text
* @return
*/
public ViewHolder setTvText( int viewId,String text){
TextView textView = getView(viewId);
textView.setText(text);
return this;
}
/**
* 为 imgview 设置图片
* @param viewId
* @param imgResId
* @return
*/
public ViewHolder setImgRes( int viewId, int imgResId){
ImageView imageView = getView(viewId);
imageView.setImageResource(imgResId);
return this;
}
public ViewHolder setImgBtRes( int viewId, int imgResId){
ImageButton imageButton = getView(viewId);
imageButton.setImageResource(imgResId);
return this;
}
}
//用法
/**
* 基于基类的适配器
*/
class MyAdapter extends CommonAdapter<Contact>{
public MyAdapter(Context context, List<Contact> lists) {
super(context, lists,R.layout. contact_listview_item);
}
@Override
public void conVert(ViewHolder viewHolder, final Contact contact) {
viewHolder.setTvText(R.id. id_tv_conname,contact.getContactName());
viewHolder.setTvText(R.id. id_tv_connumber,contact.getContactNumber());
TextView id_tv_sort = viewHolder.getView(R.id. id_tv_sort);
final CheckBox id_ck_select = viewHolder.getView(R.id. id_ck_select);
id_ck_select.setChecked(contact.isSelectTag());
int position = viewHolder.getmPosition();
if(position == 0){
id_tv_sort.setVisibility(View. VISIBLE);
id_tv_sort.setText(contact.getSortLetters());
} else{
String lastCatalog = mDatas.get(position - 1).getSortLetters();
if(lastCatalog.equals(contact.getSortLetters())){ // 同一分组
id_tv_sort.setVisibility(View. GONE);
} else {
id_tv_sort.setVisibility(View. VISIBLE);
id_tv_sort.setText(contact.getSortLetters());
}
}
id_ck_select.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
contact.setSelectTag( id_ck_select.isChecked());
}
});
}
}
* 基于基类的适配器
*/
class MyAdapter extends CommonAdapter<Contact>{
public MyAdapter(Context context, List<Contact> lists) {
super(context, lists,R.layout. contact_listview_item);
}
@Override
public void conVert(ViewHolder viewHolder, final Contact contact) {
viewHolder.setTvText(R.id. id_tv_conname,contact.getContactName());
viewHolder.setTvText(R.id. id_tv_connumber,contact.getContactNumber());
TextView id_tv_sort = viewHolder.getView(R.id. id_tv_sort);
final CheckBox id_ck_select = viewHolder.getView(R.id. id_ck_select);
id_ck_select.setChecked(contact.isSelectTag());
int position = viewHolder.getmPosition();
if(position == 0){
id_tv_sort.setVisibility(View. VISIBLE);
id_tv_sort.setText(contact.getSortLetters());
} else{
String lastCatalog = mDatas.get(position - 1).getSortLetters();
if(lastCatalog.equals(contact.getSortLetters())){ // 同一分组
id_tv_sort.setVisibility(View. GONE);
} else {
id_tv_sort.setVisibility(View. VISIBLE);
id_tv_sort.setText(contact.getSortLetters());
}
}
id_ck_select.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
contact.setSelectTag( id_ck_select.isChecked());
}
});
}
}