android适配器什么,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 的实现极其简单代码简洁复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值