万能适配器,支持自由扩展。用得好,可以将200行代码缩减为20行。好东西大家一起用。
- /**
- * Adapter万能适配器
- *
- * @param <T> 实体对象
- */
- public abstract class CommonAdapter<T> extends BaseAdapter{
- protected List<T> mDatas;
- protected Context mContext;
- protected LayoutInflater mInflater;
- protected int mLayoutId;
-
- public List<T> getDatas() {
- return mDatas;
- }
-
- public void setDatas(List<T> list){
- this.mDatas = list;
- }
- protected CommonAdapter(Context context, List<T> list, int layoutId){
- mDatas = list;
- mContext = context;
- mInflater = LayoutInflater.from(context);
- mLayoutId = layoutId;
- }
-
- @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;
- }
- protected abstract void setHolder(ViewHolder holder, T bean);
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent){
- ViewHolder holder = ViewHolder.get(mContext, convertView, position, parent, mLayoutId);
-
- setHolder(holder, mDatas.get(position));
-
- return holder.getConvertView();
- }
- }
复制代码
|
|