ChameleonAdapter-利用注解快速完成多類型列表創(chuàng)建

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绑榴,一起剝皮案震驚了整個濱河市渴杆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌店量,老刑警劉巖擎浴,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件歉嗓,死亡現(xiàn)場離奇詭異学搜,居然都是意外死亡射赛,警方通過查閱死者的電腦和手機多柑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來楣责,“玉大人竣灌,你說我怎么就攤上這事「阳铮” “怎么了初嘹?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長沮趣。 經(jīng)常有香客問我屯烦,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任驻龟,我火速辦了婚禮温眉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘翁狐。我一直安慰自己类溢,他們只是感情好,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布露懒。 她就那樣靜靜地躺著闯冷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪懈词。 梳的紋絲不亂的頭發(fā)上蛇耀,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音坎弯,去河邊找鬼纺涤。 笑死,一個胖子當著我的面吹牛荞怒,可吹牛的內(nèi)容都是我干的洒琢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼褐桌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了象迎?” 一聲冷哼從身側(cè)響起荧嵌,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎砾淌,沒想到半個月后啦撮,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡汪厨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年赃春,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片劫乱。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡织中,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出衷戈,到底是詐尸還是另有隱情狭吼,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布殖妇,位于F島的核電站刁笙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜疲吸,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一座每、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摘悴,春花似錦峭梳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至叉橱,卻和暖如春挫以,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背窃祝。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工掐松, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人粪小。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓大磺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親探膊。 傳聞我的和親對象是個殘疾皇子杠愧,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評論 0 10
  • 最近懶得不成人形,突然覺得賺錢這事特別無聊逞壁,人生不就是應(yīng)該做點自己喜歡的事情嗎流济?我最喜歡窩在臥室里關(guān)上燈,拉上窗簾...
    呼_吸閱讀 155評論 0 0