Android 适配器-mouseadapter

适配器是当前开发必不可少的,一个好的适配器在开发中能提高效率,能减少代码量,并且复用性强,不管是什么项目只要导入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.
 */
@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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值