+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.absoluteAdapTop issues
#316嵌套使用,ItemViewDelegate对象出现多次重复加载,一直死循环。7 comments · 2021-03-09#317在 ConcatAdapter 中,itemViewType获取错误导致的崩溃6 comments · 2021-04-15#327恕我直言,多类型实体类实现过于复杂!虽然item多样式,但后台返回外层json类型是一个,不可能根据你item的不同拆分bean的不同,不如MultiAdapter库。2 comments · 2021-08-10#333请问MultiType与AsyncListDiffer如何结合使用1 comments · 2022-06-02#337link 自定义问题0 comments · 2022-12-07#331在kotlin ItemViewBinder中, 在binder内部实现自我删除。items为List,不是mutableList。不能删除数据。items的类型 是否改为MutableList或者ArrayList<>0 comments · 2021-11-18#330请问 Demo中 BilibiliActivity 中嵌套的横向 PostAdapter 可以是 MultiTypeAdapter 吗?0 comments · 2021-09-03#329 java.lang.VerifyError: Rejecting class that attempts to sub-type erroneous class com.drakeet.multitype.ItemViewBinder 0 comments · 2021-08-23
Latest release
4.3.0 4.3.0 2021-06-21RecyclerView 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