Android 适配器-mouseadapter
来源:互联网 发布:windows如何隐藏分区 编辑:程序博客网 时间:2024/05/16 15:04
适配器是当前开发必不可少的,一个好的适配器在开发中能提高效率,能减少代码量,并且复用性强,不管是什么项目只要导入mouseadapter 就能直接使用,mouseadapter 是一个扩展性极强的适配器,支持listview、gridview、recyclerView
mouseadapter 是一个万能适配器
git下载地址:https://github.com/wukaide/AdapterController
## 主要实现类
public class BaseViewHolder { private SparseArray<View> mViews; private View mConvertView;
public BaseViewHolder (Context context,ViewGroup parent,int layoutId,int position){ this.mViews = new SparseArray<>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false); mConvertView.setTag(this);}
public static BaseViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){ if (convertView == null) { return new BaseViewHolder(context, parent, layoutId, position); }else { BaseViewHolder holder = (BaseViewHolder) convertView.getTag(); return holder; }}/** * 通过viewId获取控件 * @param viewId * @return */public <T extends View>T getView(int viewId){ View view = mViews.get(viewId);
if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T)view;}
public View getConvertView(){ return mConvertView;}
/** * 设置TextView的值 * @param viewId * @param text * @return */public TextView setText(int viewId,String text){ TextView tv = getView(viewId); if (tv instanceof TextView){ tv.setText(text); } return tv;}
/** * 设置ImageView背景 * @param viewId * @param reId * @return */public BaseViewHolder setImageResource(int viewId,int reId){ ImageView view = getView(viewId); view.setImageResource(reId); return this;}
public BaseViewHolder setImageBitmap(int viewId,Bitmap bitmap){ ImageView view = getView(viewId); view.setImageBitmap(bitmap); return this;}
public BaseViewHolder setImageUrl(int viewId,String url){ ImageView view = getView(viewId); //view.setImageBitmap(bitmap); //Imageloader.getInstance().loadImg(view,url); return this; }##public abstract class BaseControllerAdapter<T> extends BaseAdapter { protected Context mContext; protected List<T> mDatas; protected LayoutInflater mInflater; protected int layoutId;
public BaseControllerAdapter(Context context,List<T> datas,int layoutId){ this.mContext = context; this.mInflater = LayoutInflater.from(context); this.mDatas = datas; this.layoutId = layoutId;}
/** * How many items are in the data set represented by this Adapter. * * @return Count of items. */@Overridepublic int getCount() { return mDatas.size();}
/** * Get the data item associated with the specified position in the data set. * * @param position Position of the item whose data we want within the adapter's * data set. * @return The data at the specified position. */@Overridepublic T getItem(int position) { return mDatas.get(position);}
/** * Get the row id associated with the specified position in the list. * * @param position The position of the item within the adapter's data set whose row id we want. * @return The id of the item at the specified position. */@Overridepublic long getItemId(int position) { return position;}
/** * Get a View that displays the data at the specified position in the data set. You can either * create a View manually or inflate it from an XML layout file. When the View is inflated, the * parent View (GridView, ListView...) will apply default layout parameters unless you use * {@link LayoutInflater#inflate(int, ViewGroup, boolean)} * to specify a root view and to prevent attachment to the root. * * @param position The position of the item within the adapter's data set of the item whose view * we want. * @param convertView The old view to reuse, if possible. Note: You should check that this view * is non-null and of an appropriate type before using. If it is not possible to convert * this view to display the correct data, this method can create a new view. * Heterogeneous lists can specify their number of view types, so that this View is * always of the right type (see {@link #getViewTypeCount()} and * {@link #getItemViewType(int)}). * @param parent The parent that this view will eventually be attached to * @return A View corresponding to the data at the specified position. */@Overridepublic View getView(int position, View convertView, ViewGroup parent) { BaseViewHolder holder = BaseViewHolder.get(mContext, convertView, parent, layoutId, position); convert(holder, getItem(position),position,parent); return holder.getConvertView();}
public abstract void convert(BaseViewHolder holder,T item,int position,ViewGroup parent);}## mouseadapter 的实现极其简单代码简洁
0 0
- Android 适配器-mouseadapter
- android&&适配器
- android 适配器
- android适配器
- Android适配器
- android 适配器
- android适配器
- Android适配器
- Android 适配器
- Android 适配器
- Android适配器
- Android适配器
- Android适配器
- MouseListener与MouseAdapter的区别
- MouseListener与MouseAdapter的区别
- Android适配器-附万能适配器工具类
- Android适配器视图与适配器AdapterView & Adapter
- Android Adapter适配器
- 数据输出——print方法
- 排序:希尔排序
- 【Leetcode】H-Index
- 第十二周上机实践项目1(3):实现复数类中的运算符重载
- 我的第一篇博客(2016年6月3日晚)
- Android 适配器-mouseadapter
- 快速排序算法
- Linux零散知识点笔记
- 《图解TCP/IP》读书笔记(6)
- SecureCRT删除键、方向键乱码处理
- 借助HBuilder快速开发APP入门
- html格式标记与文本标记
- Javascript基础
- 16.6.3 How2j 关于eclipse快捷键和浮点数易错