An Android library to create multiple item types list views easily and flexibly.
English Version | 《Android 复杂的列表视图新写法 · 详解篇》
Previously, when we need to develop a complex RecyclerView / ListView, it is a difficult and
troublesome work. We should override the getItemViewType() of RecyclerView.Adapter , add some
types, and create some ViewHolders relating to those types. The process is cumbersome.
Once we need to add a new item type, we have to go to the original adapter and modify some old codes, and adapter classes will be bulkier.
Today, I created a new intuitive and flexible way to easily create complex RecyclerViews, with the MultiType library, we could insert a new item type without changing the old adapter codes and make them more readable.
In your build.gradle:
dependencies {
implementation 'me.drakeet.multitype:multitype:3.4.2'
}Note: MultiType does not support RecyclerView below version 23.0.0.
publicclassTextItem {
publicfinal@NonNullStringtext;
publicTextItem(@NonNullStringtext) {
this.text = text;
}
}publicclassTextItemViewBinderextendsItemViewBinder<TextItem, TextItemViewBinder.TextHolder> {
staticclassTextHolderextendsRecyclerView.ViewHolder {
private@NonNullfinalTextViewtext;
TextHolder(@NonNullViewitemView) {
super(itemView);
this.text = (TextView) itemView.findViewById(R.id.text);
}
}
@NonNull@OverrideprotectedTextHolderonCreateViewHolder(@NonNullLayoutInflaterinflater, @NonNullViewGroupparent) {
Viewroot = inflater.inflate(R.layout.item_text, parent, false);
returnnewTextHolder(root);
}
@OverrideprotectedvoidonBindViewHolder(@NonNullTextHolderholder, @NonNullTextItemtextItem) {
holder.text.setText("hello: " + textItem.text);
Log.d("demo", "position: " + getPosition(holder));
Log.d("demo", "adapter: " + getAdapter());
}
}Step 3. Just register your types and setup your RecyclerView and List<Object> in your Activity, for example:
publicclassSampleActivityextendsAppCompatActivity {
privateMultiTypeAdapteradapter;
privateItemsitems;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerViewrecyclerView = (RecyclerView) findViewById(R.id.list);
adapter = newMultiTypeAdapter();
adapter.register(TextItem.class, newTextItemViewBinder());
adapter.register(ImageItem.class, newImageItemViewBinder());
adapter.register(RichItem.class, newRichItemViewBinder());
recyclerView.setAdapter(adapter);
/* Mock the data */TextItemtextItem = newTextItem("world");
ImageItemimageItem = newImageItem(R.mipmap.ic_launcher);
RichItemrichItem = newRichItem("小艾大人赛高", R.mipmap.avatar);
items = newItems();
for (inti = 0; i < 20; i++) {
items.add(textItem);
items.add(imageItem);
items.add(richItem);
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
}
}That's all, you're good to go!
One to many:
adapter.register(Data.class).to(
newDataType1ViewBinder(),
newDataType2ViewBinder()
).withLinker((position, data) ->
data.type == Data.TYPE_2 ? 1 : 0
);adapter.register(Data.class).to(
newDataType1ViewBinder(),
newDataType2ViewBinder()
).withClassLinker((position, data) -> {
if (data.type == Data.TYPE_2) {
returnDataType2ViewBinder.class;
} else {
returnDataType1ViewBinder.class;
}
});More methods that you can override by ItemViewBinder:
protectedlonggetItemId(@NonNullTitem)
protectedvoidonViewRecycled(@NonNullVHholder)
protectedbooleanonFailedToRecycleView(@NonNullVHholder)
protectedvoidonViewAttachedToWindow(@NonNullVHholder)
protectedvoidonViewDetachedFromWindow(@NonNullVHholder)https://github.com/drakeet/MultiType/releases
An intellij idea plugin for Android to generate MultiTypeItem and ItemViewBinder easily.
You could check the sample module for more details and after running it will look like:
And it has been used in drakeet/TimeMachine:
Copyright 2017 drakeet.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.







