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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,211评论 0 10
  • 最近懒得不成人形,突然觉得赚钱这事特别无聊,人生不就是应该做点自己喜欢的事情吗?我最喜欢窝在卧室里关上灯,拉上窗帘...
    呼_吸阅读 1,522评论 0 0

友情链接更多精彩内容