base-adapter
Android 万能的Adapter for ListView,GridView等,支持多种Item类型的情况。
可以直接导入项目参考,在Android Studio中,使用Import Module进行导入,如下图:
单种类型Item
listView.setAdapter(new CommonAdapter<Bean>(getActivity(), mDatas,
R.layout.item_single_listview) { @Override public void convert(final ViewHolder holder, final Bean bean) { holder.setText(R.id.id_title, bean.getTitle()) .setText(R.id.id_desc, bean.getDesc()) .setText(R.id.id_time, bean.getTime()) .setText(R.id.id_phone, bean.getPhone()); holder.setOnClickListener(R.id.id_title, new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getActivity(), bean.getTitle(), Toast.LENGTH_SHORT).show(); } }); } });
只需要简单的将Adapter继承CommonAdapter,复写convert方法即可。省去了自己编写ViewHolder等大量的重复的代码。
- 可以通过holder.getView(id)拿到任何控件。
- ViewHolder中封装了大量的常用的方法,比如holder.setText(id,text),holder.setOnClickListener(id,listener)等,可以支持使用。
效果图:
多种类型的Item
package com.zhy.quickdev.adapter.sample.adapter;
import android.content.Context;
import com.zhy.quickdev.adapter.MultiItemCommonAdapter; import com.zhy.quickdev.adapter.MultiItemTypeSupport; import com.zhy.quickdev.adapter.R; import com.zhy.quickdev.adapter.ViewHolder; import com.zhy.quickdev.adapter.sample.bean.ChatMessage; import java.util.List; /** * Created by zhy on 15/9/4. */ public class ChatAdapter extends MultiItemCommonAdapter<ChatMessage> { public ChatAdapter(Context context, List<ChatMessage> datas) { super(context, datas, new MultiItemTypeSupport<ChatMessage>() { @Override public int getLayoutId(int position, ChatMessage msg) { if (msg.isComMeg()) { return R.layout.main_chat_from_msg; } return R.layout.main_chat_send_msg; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int postion, ChatMessage msg) { if (msg.isComMeg()) { return ChatMessage.RECIEVE_MSG; } return ChatMessage.SEND_MSG; } }); } @Override public void convert(ViewHolder holder, ChatMessage chatMessage) { switch (holder.getLayoutId()) { case R.layout.main_chat_from_msg: holder.setText(R.id.chat_from_content, chatMessage.getContent()); holder.setText(R.id.chat_from_name, chatMessage.getName()); holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon()); break; case R.layout.main_chat_send_msg: holder.setText(R.id.chat_send_content, chatMessage.getContent()); holder.setText(R.id.chat_send_name, chatMessage.getName()); holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon()); break; } } }
需要在构造中传入MultiItemTypeSupport对象,然后在convert方法中,根据holder.getLayoutId(),根据不同的layoutId进行相应的控件初始化即可。
效果图:
1 package com.example.adapter; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 11 public abstract class CommonAdapter<T> extends BaseAdapter { 12 protected Context mContext; 13 protected List<T> mDatas; 14 protected LayoutInflater mInflater; 15 private int layoutId; 16 17 public CommonAdapter(Context context, List<T> datas, int layoutId) { 18 this.mContext = context; 19 mInflater = LayoutInflater.from(context); 20 this.mDatas = datas; 21 this.layoutId = layoutId; 22 } 23 24 @Override 25 public int getCount() { 26 return mDatas.size(); 27 } 28 29 @Override 30 public T getItem(int position) { 31 return mDatas.get(position); 32 } 33 34 @Override 35 public long getItemId(int position) { 36 return position; 37 } 38 39 @Override 40 public View getView(int position, View convertView, ViewGroup parent) { 41 ViewHolder holder = ViewHolder.get(mContext, convertView, parent, 42 layoutId, position); 43 convert(holder, getItem(position)); 44 return holder.getConvertView(); 45 } 46 47 public abstract void convert(ViewHolder holder, T t); 48 49 }
1 package com.example.adapter; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewGroup; 6 7 import java.util.List; 8 9 public abstract class MultiItemCommonAdapter<T> extends CommonAdapter<T> { 10 11 protected MultiItemTypeSupport<T> mMultiItemTypeSupport; 12 13 public MultiItemCommonAdapter(Context context, List<T> datas, 14 MultiItemTypeSupport<T> multiItemTypeSupport) { 15 super(context, datas, -1); 16 mMultiItemTypeSupport = multiItemTypeSupport; 17 } 18 19 @Override 20 public int getViewTypeCount() { 21 if (mMultiItemTypeSupport != null) 22 return mMultiItemTypeSupport.getViewTypeCount(); 23 return super.getViewTypeCount(); 24 } 25 26 @Override 27 public int getItemViewType(int position) { 28 if (mMultiItemTypeSupport != null) 29 return mMultiItemTypeSupport.getItemViewType(position, 30 mDatas.get(position)); 31 return super.getItemViewType(position); 32 33 } 34 35 @Override 36 public View getView(int position, View convertView, ViewGroup parent) { 37 if (mMultiItemTypeSupport == null) 38 return super.getView(position, convertView, parent); 39 40 int layoutId = mMultiItemTypeSupport.getLayoutId(position, 41 getItem(position)); 42 ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, 43 layoutId, position); 44 convert(viewHolder, getItem(position)); 45 return viewHolder.getConvertView(); 46 } 47 48 }
1 package com.example.adapter; 2 3 public interface MultiItemTypeSupport<T> { 4 int getLayoutId(int position, T t); 5 6 int getViewTypeCount(); 7 8 int getItemViewType(int postion, T t); 9 }
1 package com.example.adapter; 2 3 import android.annotation.SuppressLint; 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.graphics.Paint; 7 import android.graphics.Typeface; 8 import android.graphics.drawable.Drawable; 9 import android.os.Build; 10 import android.text.util.Linkify; 11 import android.util.SparseArray; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.view.animation.AlphaAnimation; 16 import android.widget.Checkable; 17 import android.widget.ImageView; 18 import android.widget.ProgressBar; 19 import android.widget.RatingBar; 20 import android.widget.TextView; 21 22 public class ViewHolder { 23 private SparseArray<View> mViews; 24 private int mPosition; 25 private View mConvertView; 26 private Context mContext; 27 private int mLayoutId; 28 29 public ViewHolder(Context context, ViewGroup parent, int layoutId, 30 int position) { 31 mContext = context; 32 mLayoutId = layoutId; 33 this.mPosition = position; 34 this.mViews = new SparseArray<View>(); 35 mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, 36 false); 37 mConvertView.setTag(this); 38 } 39 40 public static ViewHolder get(Context context, View convertView, 41 ViewGroup parent, int layoutId, int position) { 42 if (convertView == null) { 43 return new ViewHolder(context, parent, layoutId, position); 44 } else { 45 ViewHolder holder = (ViewHolder) convertView.getTag(); 46 holder.mPosition = position; 47 return holder; 48 } 49 } 50 51 public int getPosition() { 52 return mPosition; 53 } 54 55 public int getLayoutId() { 56 return mLayoutId; 57 } 58 59 /** 60 * 通过viewId获取控件 61 * 62 * @param viewId 63 * @return 64 */ 65 public <T extends View> T getView(int viewId) { 66 View view = mViews.get(viewId); 67 if (view == null) { 68 view = mConvertView.findViewById(viewId); 69 mViews.put(viewId, view); 70 } 71 return (T) view; 72 } 73 74 public View getConvertView() { 75 return mConvertView; 76 } 77 78 /** 79 * 设置TextView的值 80 * 81 * @param viewId 82 * @param text 83 * @return 84 */ 85 public ViewHolder setText(int viewId, String text) { 86 TextView tv = getView(viewId); 87 tv.setText(text); 88 return this; 89 } 90 91 public ViewHolder setImageResource(int viewId, int resId) { 92 ImageView view = getView(viewId); 93 view.setImageResource(resId); 94 return this; 95 } 96 97 public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) { 98 ImageView view = getView(viewId); 99 view.setImageBitmap(bitmap); 100 return this; 101 } 102 103 public ViewHolder setImageDrawable(int viewId, Drawable drawable) { 104 ImageView view = getView(viewId); 105 view.setImageDrawable(drawable); 106 return this; 107 } 108 109 public ViewHolder setBackgroundColor(int viewId, int color) { 110 View view = getView(viewId); 111 view.setBackgroundColor(color); 112 return this; 113 } 114 115 public ViewHolder setBackgroundRes(int viewId, int backgroundRes) { 116 View view = getView(viewId); 117 view.setBackgroundResource(backgroundRes); 118 return this; 119 } 120 121 public ViewHolder setTextColor(int viewId, int textColor) { 122 TextView view = getView(viewId); 123 view.setTextColor(textColor); 124 return this; 125 } 126 127 public ViewHolder setTextColorRes(int viewId, int textColorRes) { 128 TextView view = getView(viewId); 129 view.setTextColor(mContext.getResources().getColor(textColorRes)); 130 return this; 131 } 132 133 @SuppressLint("NewApi") 134 public ViewHolder setAlpha(int viewId, float value) { 135 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 136 getView(viewId).setAlpha(value); 137 } else { 138 // Pre-honeycomb hack to set Alpha value 139 AlphaAnimation alpha = new AlphaAnimation(value, value); 140 alpha.setDuration(0); 141 alpha.setFillAfter(true); 142 getView(viewId).startAnimation(alpha); 143 } 144 return this; 145 } 146 147 public ViewHolder setVisible(int viewId, boolean visible) { 148 View view = getView(viewId); 149 view.setVisibility(visible ? View.VISIBLE : View.GONE); 150 return this; 151 } 152 153 public ViewHolder linkify(int viewId) { 154 TextView view = getView(viewId); 155 Linkify.addLinks(view, Linkify.ALL); 156 return this; 157 } 158 159 public ViewHolder setTypeface(Typeface typeface, int... viewIds) { 160 for (int viewId : viewIds) { 161 TextView view = getView(viewId); 162 view.setTypeface(typeface); 163 view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG); 164 } 165 return this; 166 } 167 168 public ViewHolder setProgress(int viewId, int progress) { 169 ProgressBar view = getView(viewId); 170 view.setProgress(progress); 171 return this; 172 } 173 174 public ViewHolder setProgress(int viewId, int progress, int max) { 175 ProgressBar view = getView(viewId); 176 view.setMax(max); 177 view.setProgress(progress); 178 return this; 179 } 180 181 public ViewHolder setMax(int viewId, int max) { 182 ProgressBar view = getView(viewId); 183 view.setMax(max); 184 return this; 185 } 186 187 public ViewHolder setRating(int viewId, float rating) { 188 RatingBar view = getView(viewId); 189 view.setRating(rating); 190 return this; 191 } 192 193 public ViewHolder setRating(int viewId, float rating, int max) { 194 RatingBar view = getView(viewId); 195 view.setMax(max); 196 view.setRating(rating); 197 return this; 198 } 199 200 public ViewHolder setTag(int viewId, Object tag) { 201 View view = getView(viewId); 202 view.setTag(tag); 203 return this; 204 } 205 206 public ViewHolder setTag(int viewId, int key, Object tag) { 207 View view = getView(viewId); 208 view.setTag(key, tag); 209 return this; 210 } 211 212 public ViewHolder setChecked(int viewId, boolean checked) { 213 Checkable view = (Checkable) getView(viewId); 214 view.setChecked(checked); 215 return this; 216 } 217 218 /** 219 * 关于事件的 220 */ 221 public ViewHolder setOnClickListener(int viewId, 222 View.OnClickListener listener) { 223 View view = getView(viewId); 224 view.setOnClickListener(listener); 225 return this; 226 } 227 228 public ViewHolder setOnTouchListener(int viewId, 229 View.OnTouchListener listener) { 230 View view = getView(viewId); 231 view.setOnTouchListener(listener); 232 return this; 233 } 234 235 public ViewHolder setOnLongClickListener(int viewId, 236 View.OnLongClickListener listener) { 237 View view = getView(viewId); 238 view.setOnLongClickListener(listener); 239 return this; 240 } 241 242 }