- Notifications
You must be signed in to change notification settings - Fork749
Flexible multiple types for Android RecyclerView.
License
drakeet/MultiType
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An Android library to create multiple item types list views easily and flexibly.
Previously, when we need to develop a complex RecyclerView / ListView, it is a difficult andtroublesome work. We should override thegetItemViewType()
ofRecyclerView.Adapter
, add some types, and create someViewHolder
s relating to those types. Once we need to add a new item type, we have to go to the original adapter file and modify some old codes carefully,and these adapter classes will get more complicated.
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 any old adapter codes and make them more readable.
In yourbuild.gradle
:
MultiType has been rebuilt based on androidx. If you are still using the android support library, please usemultitype:3.4.4
andmultitype-kotlin:3.4.4
.
dependencies { implementation'me.drakeet.multitype:multitype:3.5.0'}
Optional: Extension for Kotlin:
implementation'me.drakeet.multitype:multitype-ktx:3.5.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. Justregister
your types and setup yourRecyclerView
andList<Object>
in yourActivity
, 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);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 fromItemViewBinder:
protectedlonggetItemId(@NonNullTitem)protectedvoidonViewRecycled(@NonNullVHholder)protectedbooleanonFailedToRecycleView(@NonNullVHholder)protectedvoidonViewAttachedToWindow(@NonNullVHholder)protectedvoidonViewDetachedFromWindow(@NonNullVHholder)
Kotlin:
MultiTypeAdapter
- Added
register(binder: ItemViewBinder<T, *>)
- Added
register(clazz: KClass<out T>, binder: ItemViewBinder<T, *>)
- Added
register(clazz: KClass<out T>): OneToManyFlow<T>
TypePool
- Added
register(clazz: KClass<out T>, binder: ItemViewBinder<T, *>, linker: Linker<T>)
- Added
unregister(clazz: KClass<out T>)
- Added
firstIndexOf(clazz: KClass<out T>)
OneToManyEndpoint
- Added
withKClassLinker(classLinker: KClassLinker<T>)
- Added
withKClassLinker(classLinker: (position: Int, t: T) -> KClass<out ItemViewBinder<T, *>>)
An intellij idea plugin for Android to generateMultiType
Item
andItemViewBinder
easily.
Pages created with MultiType:
https://github.com/drakeet/MultiType/releases
Copyright 2016-2018 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
About
Flexible multiple types for Android RecyclerView.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.