一个上线项目实践的简单易用的RecyclerView通用全能型适配器。

      年初上班,事情出奇的少,感觉项目新版本的更新需要策划很久,应该会闲很久,闲来无事也不能荒废度日啊。于是就打开了之前的另一个项目想着看看自己之前的代码是怎么写。无意间看到另一个同事负责的模块,使用了大量RecyclerView,但是适配器居然都是同一个,稍微看了下,哎呀,这家伙好东西藏着掖着......

        藏着一个很简单易用且通用的适配器,这种好东西就该拿出来分享嘛。。。

        源码虽然不多但是全部贴出来也看得也比较麻烦,这种东西先要会用然后再去研究源码,所以我直接贴出这个CommonAdapter的源码。

public abstract class CommonAdapter<T> extends MultiItemTypeAdapter<T>
{
    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;

    public CommonAdapter(final Context context, final int layoutId, List<T> datas)
    {
        super(context, datas);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;

        addItemViewDelegate(new ItemViewDelegate<T>()
        {
            @Override
            public int getItemViewLayoutId()
            {
                return layoutId;
            }

            @Override
            public boolean isForViewType( T item, int position)
            {
                return true;
            }

            @Override
            public void convert(ViewHolder holder, T t, int position)
            {
                CommonAdapter.this.convert(holder, t, position);
            }
        });
    }
    protected abstract void convert(ViewHolder holder, T t, int position);
}

使用起来很简单,正常的new 一个,三个参数很好理解,一个上下文、item的布局、泛型List,重写convert方法,添加数据到适配器,在这个方法里是不是看到了熟悉的ViewHolder和position,第二个参数是根据你指定的List的数据类型而变的。贴出代码看可能会比较清晰

//第一参数 上下文 第二个 item布局 第三个 泛型list的绑定数据集
        adapter = new CommonAdapter<Integer>(this, R.layout.item_collection_zi, list) {
            @Override
            protected void convert(ViewHolder holder, Integer integer, int position) {
                //holder 就是这个item布局的映射
                switch (position) {
                    case 0:
                        setCard(holder, integer, R.drawable.ic_collection_pa, R.drawable.ic_collection_pa_normal);
                        break;
                    case 1:
                        setCard(holder, integer, R.drawable.ic_collection_pa, R.drawable.ic_collection_pa_normal);
                        break;
                    case 2:
                        setCard(holder, integer, R.drawable.ic_collection_you, R.drawable.ic_collection_you_normal);
                        break;
                    case 3:
                        setCard(holder, integer, R.drawable.ic_collection_xin, R.drawable.ic_collection_xin_normal);
                        break;
                    case 4:
                        setCard(holder, integer, R.drawable.ic_collection_nian, R.drawable.ic_collection_nian_normal);
                        break;
                    case 5:
                        setCard(holder, integer, R.drawable.ic_collection_kuai, R.drawable.ic_collection_kuai_normal);
                        break;
                    case 6:
                        setCard(holder, integer, R.drawable.ic_collection_le, R.drawable.ic_collection_le_normal);
                        break;
                }
            }
        };

        rv.setAdapter(adapter);
//给item设置布局
    public void setCard(ViewHolder holder, Integer integer, int ic_collected, int ic_uncollect) {
        if (integer > 1) {//集了一张
            holder.setText(R.id.stv_num, integer + "");//显示集了几张
            holder.setBackgroundRes(R.id.iv_card, ic_collected);//指定控件和图片作为背景
        } else if (integer == 0) {
            holder.setText(R.id.stv_num, 0 + "");
            holder.setBackgroundRes(R.id.iv_card, ic_uncollect);
        }
    }

setCard()方法也给出了,这样看就很明了了。当然这里是项目的,所以只有7个数据,如果数据是从服务器请求的,list的个数不确定,可以这样。下面模拟了下。

final ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(111111);
        list1.add(222222);
        list1.add(333333);
        adapter = new CommonAdapter<Integer>(this,R.layout.item_collection_zi,list1) {
            @Override
            protected void convert(ViewHolder holder, Integer integer, int position) {
                holder.setText(R.id.su,list1.get(position)+"");
            }
        };

holder里有很多方法,setText是设置文本显示,setBackGroundRes可以设置控件的背景,还有setTextColor等等,这里已经把ViewHolder抽取出来了,如果要设置某个控件,只要把控件id和一些指定参数传进去就OK了。然后RecyclerView绑定适配器就可以了。上个实现的gif。源码不多也很很容易读懂,然后想要这个开源项目试试手需要源码的可以直接找我要。QQ : 642360920,源码下载传送门:RecyclerView通用全能适配器

http://download.csdn.net/download/ycocol/10257474

后面再写一篇更多的使用方法,包括添加和删除item,非常非常非常的简单便捷。




        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值