drakeet /MultiType

Flexible multiple types for Android RecyclerView.

★ 5,743⑂ 745👁 117⑃ 91● 247▤ 36
KotlinApache-2.0Created 2016-08-03
下载:4.3.04.3.0 ↓4.3.02021-06-21
+0
日增长
5,743
stars
745
forks
117
watchers
91
prs
247
issues
36
releases

README

MultiType
Easier and more flexible to create multiple types for Android RecyclerView.



!maven-central
!jetbrains-plugin

Previously, when we need to develop a complex RecyclerView / ListView, it is difficult and
troublesome work. We should override the getItemViewType() of RecyclerView.Adapter , add some types, and create some ViewHolders 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.

Nowadays, 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 your build.gradle:

_MultiType has been rebuilt based on AndroidX. If you are still using the android support library, please use me.drakeet.multitype:multitype:3.4.4 and me.drakeet.multitype:multitype-kotlin:3.4.4._

_In addition, since 4.0.0 we have migrated to fully build with Kotlin. If you don't want to use Kotlin, you can use the last stable version me.drakeet.multitype:multitype:3.5.0 and see 3.x._

groovy
dependencies {
  implementation 'com.drakeet.multitype:multitype:4.3.0'
}


 Usage

 Step 1. Create a Kotlin class or data class, for example:

kotlin
data class Foo(
  val value: String
)


 Step 2. Create a class extends ItemViewDelegate, for example:

kotlin
class FooViewDelegate: ItemViewDelegate() {

  override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder {
    // If you want a LayoutInflater parameter instead of a Context,
    // you can use ItemViewBinder as the parent of this class.
    return ViewHolder(FooView(context))
  }

  override fun onBindViewHolder(holder: ViewHolder, item: Foo) {
    holder.fooView.text = item.value

    Log.d("ItemViewDelegate API", "position: ${holder.bindingAdapterPosition}")
    Log.d("ItemViewDelegate API", "items: $adapterItems")
    Log.d("ItemViewDelegate API", "adapter: $adapter")
    Log.d("More", "Context: ${holder.itemView.context}")
  }

  class ViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
    val fooView: TextView = itemView.findViewById(R.id.foo)
  }
}


 Or if you are using a custom View instead of XML layout, you can use ViewDelegate:

 The ViewDelegate is a simple ItemViewDelegate that does not require to declare and provide a RecyclerView.ViewHolder.

kotlin
class FooViewDelegate : ViewDelegate() {

  override fun onCreateView(context: Context): FooView {
    return FooView(context).apply { layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT) }
  }

  override fun onBindView(view: FooView, item: Foo) {
    view.imageView.setImageResource(item.imageResId)
    view.textView.text = item.text

    view.textView.text = """
      |${item.text}
      |viewHolder: ${view.holder}
      |layoutPosition: ${view.layoutPosition}
      |absoluteAdapterPosition: ${view.absoluteAdap

Top issues

Latest release

4.3.0 4.3.0 2021-06-21
RecyclerView 1.2.1
 Support explicitly register Kotlin primitive types (see 302)
 ViewDelegate : Fix View.layoutPosition and remove View.~~adapterPosition~~ (see 315), in the meantime, add View.holder, View.absoluteAdapterPosition and View.bindingAdapterPosition (see https://github.com/drakeet/MultiType/commit/7dca987d8b56fa1dc34636f9d59653e4e99e75a8)
 Remove ItemViewDelegate.getPosition(viewHolder), please use ViewHolder.getPosition instead
 Add two new experimental alias classes:
   ViewHolderDelegate: ItemViewDelegate
   ViewHolderInflater: ItemViewBinder

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON