框架整理系列十七(牛逼的万能适配器-比十三更牛逼!)

引用

compile 'net.idik:slimadapter:2.0.6'

框架整理系列十七(牛逼的万能适配器-比十三更牛逼!)_第1张图片
img

使用
Step 1: 初始化

SlimAdapter.create()
                .register(R.layout.item_user, new SlimInjector() {
                    @Override
                    protected void onInject(User data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .register(R.layout.item_interger, new SlimInjector() {
                    @Override
                    protected void onInject(Integer data, IViewInjector injector) {
                        ...// inject data into views,step 2
                    }
                })
                .register(R.layout.item_string, new SlimInjector() {
                    @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);
    }

Step 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() {
                                    @Override
                                    public void action(TextView view) {
                                        //do stuff...
                                    }
                                })
        ...;

Step 3: 数据

   List data = new ArrayList<>();

    {
        data.add(new SectionHeader("My Friends"));
        data.add(new User("Jack", 21, R.drawable.icon1, "123456789XX"));
        data.add(new User("Marry", 17, R.drawable.icon2, "123456789XX"));
        data.add(new SectionHeader("My Images"));
        data.add(new Image(R.drawable.cover1));
        data.add(new Image(R.drawable.cover2));
        data.add(new Image(R.drawable.cover3));
        data.add(new Image(R.drawable.cover4));
        data.add(new Image(R.drawable.cover5));
        data.add(new Image(R.drawable.cover6));
        data.add(new Image(R.drawable.cover7));
        data.add(new Image(R.drawable.cover8));
        data.add(new Image(R.drawable.cover9));
        data.add(new Image(R.drawable.cover10));
        data.add(new Image(R.drawable.cover11));
        data.add(new SectionHeader("My Musics"));
        data.add(new Music("Love story", R.drawable.icon3));
        data.add(new Music("Nothing's gonna change my love for u", R.drawable.icon4));
        data.add(new Music("Just one last dance", R.drawable.icon5));
    }
    
    slimAdapter.updateData(data);
 
 

Kotlin

SlimAdapter.create()
                .register(R.layout.item_string) { data, injector ->
                    ...// inject data into views
                }
                .register(R.layout.item_user) { data, injector ->
                    ...// inject data into views
                }
                .register(R.layout.item_interger) { data, injector ->
                    ...// inject data into views
                }
                .registerDefault(R.layout.item_string) { data, injector ->
                    ...// inject data into views
                }
                .attachTo(recyclerView)

github大神:https://github.com/MEiDIK/SlimAdapter

你可能感兴趣的:(框架整理系列十七(牛逼的万能适配器-比十三更牛逼!))