一个简单干练的无需写ViewHolder的Adapter,支持多item类型,支持kotlin。


介绍:

一个简单干练的无需写ViewHolder的Adapter,支持多item类型,支持kotlin。

运行效果:

使用说明:

特点

  • 无ViewHolder

  • 无反射

  • API简单干练的API

  • 支持多item类型

  • 支持kotlin

依赖

compile 'net.idik:slimadapter:2.0.6'

步骤 1:创建一个SlimAdapter并设置到目标RecyclerView

  • register(layoutRes, SlimInjector<DataType>)

    将一个数据型为DataType的SlimInjector与布局layoutRes相关联

  • registerDefault(layoutRes, SlimInjector)

  • 注册一个适合所有类型数据的SlimInjector

 SlimAdapter.create()
                .register(R.layout.item_user, new SlimInjector<User>() {
                    @Override
                    protected void onInject(User data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .register(R.layout.item_interger, new SlimInjector<Integer>() {
                    @Override
                    protected void onInject(Integer data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .register(R.layout.item_string, new SlimInjector<String>() {
                    @Override
                    protected void onInject(String data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .registerDefault(R.layout.item_string, new SlimInjector() {
                    @Override
                    protected void onInject(Object data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .attachTo(recyclerView);
    }

步骤 2: 绑定数据

injector.text(R.id.name, data.getName())
        .text(R.id.age, String.valueOf(data.getAge()))
        .textColor(R.id.age, Color.RED)
        .textSize(R.id.age, 8)
        .longClicked(R.id.name, new View.OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {
                                        //do stuff...
                                        return false;
                                    }
                                })
        .clicked(R.id.text, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //do stuff...
                                    }
                                })
        .with(R.id.name, new IViewInjector.Action<TextView>() {
                                    @Override
                                    public void action(TextView view) {
                                        //do stuff...
                                    }
                                })
        ...;

步骤 3: 像普通adapter那样使用 SlimAdapter 

    List<Object> data = new ArrayList<>();

    {
        data.add("hello");
        data.add(",");
        data.add(new User("iDIK", 27));
        data.add("world");
        data.add("!");
        data.add(666666);
        data.add(34234);
        data.add(666669L);
    }
    
    slimAdapter.setData(data).notifyDataSetChanged();
点击快速下载源码
最新回复 (0)
返回