Android开发之万能BaseAdapter

CommonAdapter直接上代码

public abstract class CommonAdapter extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List mDatas;
    protected final int mItemLayoutId;

    public CommonAdapter(List mDatas, int itemLayoutId) {
        this.mContext = UIUtils.getContext();
        this.mInflater = LayoutInflater.from(mContext);
        this.mItemLayoutId = itemLayoutId;
        setListData(mDatas);

    }

    /**
     * 添加集合数据到原集合首位,下拉刷新时使用
     *
     * @param list
     */
    public void addToFirst(List list) {
        if (list == null)
            return;
        this.mDatas.addAll(0, list);
        notifyDataSetChanged();
    }

    public void addToFirst(T t) {
        if (t == null)
            return;
        this.mDatas.add(0, t);
        notifyDataSetChanged();
    }

    /**
     * 添加数据到末尾,用于上拉加载等情况。
* 不清楚原集合,添加到末尾。 * * @param list */ public void addToLast(List list) { if (list == null) return; this.mDatas.addAll(list); notifyDataSetChanged(); } /** * 添加元素到集合末尾 * * @param t */ public void addToLast(T t) { if (t == null) return; this.mDatas.add(t); notifyDataSetChanged(); } /** * 设置数据。
* 会清空原集合所有数据,后添加。 * * @param list */ public void setListData(List list) { if (list == null) { list = new ArrayList(0); } this.mDatas = list; notifyDataSetChanged(); } @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; } @Override public View getView(int position, View convertView, ViewGroup parent) { final BaseViewHolder viewHolder = getViewHolder(position, convertView, parent); convert(position,viewHolder, getItem(position)); return viewHolder.getConvertView(); } public abstract void convert(int position, BaseViewHolder helper, T item); private BaseViewHolder getViewHolder(int position, View convertView, ViewGroup parent) { return BaseViewHolder.get(mContext, convertView, parent, mItemLayoutId, position); } }

BaseViewHolder 直接上代码

public class BaseViewHolder {
    private final SparseArray mViews;
    private View mConvertView;
    private int mPosition;

    private BaseViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false);
        //setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static BaseViewHolder get(Context context, View convertView,
                                     ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new BaseViewHolder(context, parent, layoutId, position);
        }
        return (BaseViewHolder) convertView.getTag();
    }


    /**
     * 通过控件的Id获取对于的控件,如果没有则加入views
     *
     * @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 BaseViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public BaseViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public BaseViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public BaseViewHolder setImageByUrl(int viewId, String url) {
        ImageLoader.getInstance().displayImage(url, (ImageView) getView(viewId), ImageLoadOptions.getOptions());
        return this;
    }

    public int getPosition() {
        return mPosition;
    }

}  

调用方法

  ProductionAdapter   adapter = new ProductionAdapter(orderlList, R.layout.layout_order);
  listView.setAdapter(adapter);

  public class ProductionAdapter extends CommonAdapter {
        public ProductionAdapter(List mDatas, int itemLayoutId) {
            super(mDatas, itemLayoutId);
            L.e("ProductionAdapter");
        }

        @Override
        public void convert(int position,BaseViewHolder helper, ProductListResult.DataEntity.ModelListEntity item) {
            RelativeLayout reView = helper.getView(R.id.item_button_layout);
            ImageView productImg = helper.getView(R.id.product_img);
            reView.setVisibility(View.GONE);
            helper.setText(R.id.product_name, item.getTitle());
            helper.setText(R.id.product_price, item.getPrice());
            helper.setText(R.id.product_norms, item.getBaseInfo());
            helper.setText(R.id.product_number, item.getNumber() + "");
            helper.setText(R.id.id_tv_information, item.getInfo());
            ImageLoader.getInstance().displayImage(item.getPic(), productImg, ImageLoadOptions.getOptions());
        }
    }

![OKADA9]R3JC4YMN)X~7]L$2.png](http://upload-images.jianshu.io/upload_images/433829-ae7e09d153b4151f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

一个ListView的Itme放不定个数进度条
    public class ListViewAdapter extends CommonAdapter{
        public ListViewAdapter(List mDatas, int itemLayoutId) {
            super(mDatas, itemLayoutId);
        }
        @Override
        public void convert(int position, BaseViewHolder helper, ProgressResult.DataEntity.OrderlListEntity item) {
            LinearLayout linearlayout = helper.getView(R.id.id_list_porgress);
            linearlayout.removeAllViews();
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,UIUtils.dip2px(30));
            lp.setMargins(10,10,10,10);
            List progressList=null;
            if (item!=null){
                progressList = item.getProgress();
            }
            if (progressList!=null){
                for (int i = 0; i < progressList.size(); i++) {
                    View view=getMyProgressView();
                    ProgressBar progress = (ProgressBar) view.findViewById(R.id.my_progress);
                    TextView textProgress = (TextView) view.findViewById(R.id.tv_progress);
                    progress.setMax(mFlowTotalCount);
                    progress.setProgress(progressList.get(i).getCurrentFlow());
                    textProgress.setText(progressList.get(i).getFlowInfo());
                    linearlayout.addView(view, lp);
                }
            }
        }
        public View getMyProgressView() {
            View inf_rel = LayoutInflater.from(mContext).inflate(R.layout.item_myprogress, null);
            return inf_rel;
        }
    }

你可能感兴趣的:(Android开发之万能BaseAdapter)