美文网首页Android开发经验谈Android开发Android技术知识
ChameleonAdapter-利用注解快速完成多类型列表创建

ChameleonAdapter-利用注解快速完成多类型列表创建

作者: Leo_Zheng | 来源:发表于2018-05-11 16:07 被阅读33次

ChameleonAdapter

chameleon

An easy way to create multiple item type list using annotation

In daily work, it's hard to manage the adapter with various item type so that we must add lots of code to control the behavior. ChameleonAdapter allows us to create item controller for specific item type using annotation and register the controller into adapter.

github link

Getting started

Download

dependencies {
    compile project(':chameleonadapterlib')
    annotationProcessor project(':chameleon-compiler')
}

Step 1

Bind the itemViewBinder with the Activity

public abstract class BaseActivity extends AppCompatActivity {

    private Unbinder mUnbinder; //create the unbinder

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mUnbinder = ChameleonAdapter.bind(this); //bind the itemViewBinder with the Activity
        
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();  //unbind the itemViewBInder
    }

Step 2

Define the DefaultItemBinder using annotation (inject the item entity&layout)

@BindItem(value = FirstItemEntity.class, layout = R.layout.item_first_layout)
    DefaultItemBinder mFirstItemBinder;
    
@BindItem(value = SecondItemEntity.class, layout = R.layout.item_second_layout)
    DefaultItemBinder mSecondItemBinder;

Step 3

Create the ChameleonAdapter and link the itemViewBinder with the Adapter. Finally set the adapter for RecyclerView

ChameleonAdapter mAdapter;
List<BaseEntity> mDatas = new ArrayList();
    
    ..................
 mAdapter = new ChameleonAdapter(this);
        mAdapter.setItems(mDatas);
        mAdapter.link(mFirstItemBinder);
        mAdapter.link(new SecondItemViewBinder(R.layout.item_second_layout));
        mAdapter.addHeaderView(textView);
        homepageList.setAdapter(mAdapter);

Step 4

mFirstItemBinder.setOnBindListener(new BaseItemBinder.OnBindListener() {
            @Override
            public void onBindViewHolder(RecyclerViewBaseViewHolder holder, int position, Object item) {
                TextView txtFirst = (TextView) holder.getViewById(R.id.txt_first);
                txtFirst.setText("haha" + position);

            }
        });

Other usage

You can also create the itemViewBinder by yourself. Just extend the BaseItemBinder class.

public class FirstItemViewBinder extends BaseItemBinder {
    int layoutId;
    public FirstItemViewBinder(int layoutId) {
        this.layoutId = layoutId;
    }
    @Override
    public RecyclerViewBaseViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent) {
        return new RecyclerViewBaseViewHolder(inflater.inflate(layoutId, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerViewBaseViewHolder holder, int position, Object item) {
    }

    @Override
    public Class getItemClass() {
        return FirstItemEntity.class;
    }
}

Screenshot

screenshot1 screenshot2

TODO

  • upload to jcenter
  • support one-many mode

Reference

License

Apache License 2.0

相关文章

  • ChameleonAdapter-利用注解快速完成多类型列表创建

    ChameleonAdapter An easy way to create multiple item type...

  • Python列表用法

    列表基础 创建列表 创建一个空列表 创建一个含有元素的列表 创建一个多类型元素列表 创建嵌套列表 访问元素列表访问...

  • python基础B

    列表数据类型 字典数据类型 字符串操作 列表数据类型 1.列表 2.利用下标获取列表中的单个值 3.列表可以表示多...

  • 列表

    列表可以存储任何的数据 列表的创建 1.利用基本语法[]创建 2.利用list()创建 3.range创建整数列表...

  • 窝的Python学习之路001 列表

    数组:各元素类型 必须 一致列表:各元素类型 不必 一致 创建列表 创建一个普通列表 创建一个混合列表 创建一个空...

  • 列表List Day0816

    列表可以存储任何类型的数据,在创建列表对象的时候首先要制定你要创建的这个列表要存储什么类型的(泛型) 1.创建列表...

  • 注解Annotation--java26(02/19/2016)

    主要内容 JDK内置的基本注解类型(3个)自定义注解类型对注解进行注解(4个)利用反射获取注解信息(在反射部分涉及...

  • Panda - 3. Series 和 DataFrame

    创建Series 传入列表 指定索引index 当传入混合类型的列表时,类型为object 创建DataFrame...

  • python生成器的简介

    用过python的人都知道,在python中利用列表生成器能快速创建出一个列表。但是列表生成器也有它的局限性。其一...

  • (1)数组的基础知识

    1. 创建数组 1.1 array() 可以利用array()函数将Python列表或元组类型数据直接转化为num...

网友评论

    本文标题:ChameleonAdapter-利用注解快速完成多类型列表创建

    本文链接:https://www.haomeiwen.com/subject/xagfdftx.html