Android开发经验谈码农的世界Android开发

ListView之通用万能Adapter

2019-08-18  本文已影响446人  手指乐

1.CommonAdapter使用一个泛型来适配通用的数据结构:

public abstract class CommonAdapter<T> extends BaseAdapter
{
    protected LayoutInflater mInflater;

    protected Context mContext;

    protected List<T> mDatas;

    protected final int mItemLayoutId;

    protected int countSum = -1;

    public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId)
    {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
    }

    /**
     * 替换元素并刷新
     *
     * @param mDatas
     */
    public void refresh(List<T> mDatas)
    {
        this.mDatas = mDatas;
        this.notifyDataSetChanged();
    }

    /**
     * 删除元素并更新
     *
     * @param position
     */
    public void deleteList(int position)
    {
        this.mDatas.remove(position);
        this.notifyDataSetChanged();
    }

    /**
     * 定义itemCount
     */
    public CommonAdapter setCount(int i)
    {
        countSum = i;
        this.notifyDataSetChanged();
        return this;
    }

    @Override
    public int getCount()
    {
        if (countSum == -1)
        {
            return mDatas.size();
        }
        else
        {
            return countSum;
        }
    }

    @Override
    public T getItem(int position)
    {
        if (countSum == -1)
        {
            return mDatas.get(position);
        }
        else
        {
            return mDatas.get(countSum % mDatas.size());
        }

    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder viewHolder = getViewHolder(position, convertView,
                parent);
        convert(viewHolder, position, getItem(position));
        return viewHolder.getConvertView();

    }

    public abstract void convert(ViewHolder helper, int position, T item);

    private ViewHolder getViewHolder(int position, View convertView,
            ViewGroup parent)
    {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
                position);
    }

}

2.ViewHolder中处理convertView复用,缓存item里面的控件,避免重复使用findviewbyid带来的性能消耗:

public class ViewHolder
{
    private final SparseArray<View> mViews;//类似hashmap

    private int mPosition;

    private View mConvertView;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
            int position)
    {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        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 ViewHolder get(Context context, View convertView,
            ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件,如果没有则加入views
     *
     * @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;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param string
     * @return
     */
    public ViewHolder setText(int viewId, String string)
    {
        TextView view = getView(viewId);
        view.setText(string);
        return this;
    }

    /**
     * 获取edit文本
     *
     * @param viewId
     * @return
     */
    public String getEditText(int viewId)
    {
        EditText ed = getView(viewId);
        String str = ed.getText().toString();
        return str;
    }


    public void setRating(int viewId, int iScore){
        RatingBar ratingBar = getView(viewId);
        //ratingBar.setMax(10);
        ratingBar.setProgress(iScore);
    }

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

        return this;
    }

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

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public ViewHolder setImageByUrl(int viewId, String url,Context context)
    {
       ImageView iv = getView(viewId);
        Picasso.with(context).load(url).into(iv);
        return this;
    }

    /**
     * 给view设置背景色
     *
     * @param viewId
     * @param color
     * @return
     */
    public ViewHolder setBackgroundColor(int viewId, int color)
    {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public int getPosition()
    {
        return mPosition;
    }

}

3.使用起来非常简单,自定义adapter继承CommonAdapter,实现convert方法即可

lvFucs.setAdapter(new CommonAdapter<MeFuncBean>(getActivity(),arrfuncs,R.layout.layout_item_me) {

            @Override
            public void convert(ViewHolder helper, int position, MeFuncBean item) {
                helper.setText(R.id.tvfuncname,item.funcName);
                if (item.msgCnt == -1){
                    helper.getView(R.id.tvmsgcnt).setVisibility(View.INVISIBLE);
                }else {
                    helper.getView(R.id.tvmsgcnt).setVisibility(View.VISIBLE);
                }
            }
        });
上一篇下一篇

猜你喜欢

热点阅读