返回 登录
0

Android 适配器-mouseadapter

适配器是当前开发必不可少的,一个好的适配器在开发中能提高效率,能减少代码量,并且复用性强,不管是什么项目只要导入mouseadapter 就能直接使用,mouseadapter 是一个扩展性极强的适配器,支持listview、gridview、recyclerView

mouseadapter 是一个万能适配器
git下载地址:https://github.com/wukaide/AdapterController

主要实现类

public class BaseViewHolder { private SparseArray 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 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 extends BaseAdapter {
protected Context mContext;
protected List mDatas;
protected LayoutInflater mInflater;
protected int layoutId;
public BaseControllerAdapter(Context context,List 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.
*/
@Override
public 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.
*/
@Override
public 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.
*/
@Override
public 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.
*/
@Override
public 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 的实现极其简单代码简洁

评论