Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Flexible multiple types for Android RecyclerView.

License

NotificationsYou must be signed in to change notification settings

drakeet/MultiType

 
 

Repository files navigation

An Android library to create multiple item types list views easily and flexibly.

Build StatusLicensemaven-centraljetbrains-plugin

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 someViewHolders 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.

Getting started

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'

Usage

Step 1. Create a class, It would be yourdata model/Java bean, for example:

publicclassTextItem {publicfinal@NonNullStringtext;publicTextItem(@NonNullStringtext) {this.text =text;    }}

Step 2. Create a class extendsItemViewBinder<T, VH extends ViewHolder>, for example:

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!

Advanced usage

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

  • Addedregister(binder: ItemViewBinder<T, *>)
  • Addedregister(clazz: KClass<out T>, binder: ItemViewBinder<T, *>)
  • Addedregister(clazz: KClass<out T>): OneToManyFlow<T>

TypePool

  • Addedregister(clazz: KClass<out T>, binder: ItemViewBinder<T, *>, linker: Linker<T>)
  • Addedunregister(clazz: KClass<out T>)
  • AddedfirstIndexOf(clazz: KClass<out T>)

OneToManyEndpoint

  • AddedwithKClassLinker(classLinker: KClassLinker<T>)
  • AddedwithKClassLinker(classLinker: (position: Int, t: T) -> KClass<out ItemViewBinder<T, *>>)

Android Studio Plugin

An intellij idea plugin for Android to generateMultiTypeItem andItemViewBinder easily.

Screenshots

Pages created with MultiType:

Wiki

Change Log

https://github.com/drakeet/MultiType/releases

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors7

Languages


[8]ページ先頭

©2009-2025 Movatter.jp