Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

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

Flexible multiple types for Android RecyclerView.

License

NotificationsYou must be signed in to change notification settings

drakeet/MultiType

Repository files navigation

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

Build StatusLicensemaven-centraljetbrains-plugin

Previously, when we need to develop a complex RecyclerView / ListView, it is 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.

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 yourbuild.gradle:

MultiType has been rebuilt based onAndroidX. If you are still using the android support library, please useme.drakeet.multitype:multitype:3.4.4 andme.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 versionme.drakeet.multitype:multitype:3.5.0 and see3.x.

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

Usage

Step 1. Create a Kotlinclass ordata class, for example:

data classFoo(valvalue:String)

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

classFooViewDelegate:ItemViewDelegate<Foo,FooViewDelegate.ViewHolder>() {overridefunonCreateViewHolder(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.returnViewHolder(FooView(context))  }overridefunonBindViewHolder(holder:ViewHolder,item:Foo) {    holder.fooView.text= item.valueLog.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}")  }classViewHolder(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 useViewDelegate:

TheViewDelegate is a simpleItemViewDelegate that does not require to declare and provide aRecyclerView.ViewHolder.

classFooViewDelegate :ViewDelegate<Foo,FooView>() {overridefunonCreateView(context:Context):FooView {returnFooView(context).apply { layoutParams=LayoutParams(MATCH_PARENT,WRAP_CONTENT) }  }overridefunonBindView(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.absoluteAdapterPosition}      |bindingAdapterPosition:${view.bindingAdapterPosition}""".trimMargin()  }}

(SeeRichViewDelegate &RichView examples for more details)

Step 3.register your types and setup yourRecyclerView, for example:

classSampleActivity :AppCompatActivity() {privateval adapter=MultiTypeAdapter()privateval items=ArrayList<Any>()overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState)    setContentView(R.layout.activity_list)val recyclerView= findViewById<RecyclerView>(R.id.list)    adapter.register(TextItemViewDelegate())    adapter.register(ImageItemViewDelegate())    adapter.register(RichItemViewDelegate())    recyclerView.adapter= adapterval textItem=TextItem("world")val imageItem=ImageItem(R.mipmap.ic_launcher)val richItem=RichItem("小艾大人赛高",R.drawable.img_11)for (iin0..19) {      items.add(textItem)      items.add(imageItem)      items.add(richItem)    }    adapter.items= items    adapter.notifyDataSetChanged()  }}

That's all, you're good to go!

Advanced usage

One to many:

adapter.register(Data::class).to(DataType1ViewDelegate(),DataType2ViewDelegate()).withKotlinClassLinker { _, data->when (data.type) {Data.TYPE_2->DataType2ViewDelegate::classelse->DataType1ViewDelegate::class  }}

SeeOneDataToManyActivity,OneToManyFlow andOneToManyEndpoint for more details.

More methods that you can override fromItemViewDelegate:

openfunonBindViewHolder(holder:VH,item:T,payloads:List<Any>)openfungetItemId(item:T):LongopenfunonViewRecycled(holder:VH)openfunonFailedToRecycleView(holder:VH):BooleanopenfunonViewAttachedToWindow(holder:VH)openfunonViewDetachedFromWindow(holder:VH)

Android Studio Plugin

An intellij idea plugin for Android to generateMultiTypeItem andItemViewDelegate easily.

Screenshots

Pages created with MultiType:

License

Copyright (c) 2016-present. Drakeet XuLicensed 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.

[8]ページ先頭

©2009-2025 Movatter.jp