{"id":13427829,"url":"https://github.com/drakeet/MultiType","last_synced_at":"2025-03-16T00:32:18.566Z","repository":{"id":37285511,"uuid":"64845938","full_name":"drakeet/MultiType","owner":"drakeet","description":"Flexible multiple types for Android RecyclerView.","archived":false,"fork":false,"pushed_at":"2022-08-28T04:51:44.000Z","size":8617,"stargazers_count":5766,"open_issues_count":12,"forks_count":751,"subscribers_count":122,"default_branch":"master","last_synced_at":"2024-10-22T22:14:14.130Z","etag":null,"topics":["android-library","multitype","one-to-many","one2many","recyclerview","recyclerview-multi-type"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drakeet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-03T12:55:32.000Z","updated_at":"2024-10-11T08:50:20.000Z","dependencies_parsed_at":"2022-08-08T20:00:51.963Z","dependency_job_id":null,"html_url":"https://github.com/drakeet/MultiType","commit_stats":null,"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drakeet%2FMultiType","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drakeet%2FMultiType/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drakeet%2FMultiType/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drakeet%2FMultiType/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drakeet","download_url":"https://codeload.github.com/drakeet/MultiType/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221631816,"owners_count":16855013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-library","multitype","one-to-many","one2many","recyclerview","recyclerview-multi-type"],"created_at":"2024-07-31T01:00:40.966Z","updated_at":"2024-10-27T05:30:27.229Z","avatar_url":"https://github.com/drakeet.png","language":"Kotlin","readme":"# MultiType\nEasier and more flexible to create multiple types for Android RecyclerView.\n\n[![Build Status](https://travis-ci.org/drakeet/MultiType.svg?branch=3.x)](https://travis-ci.org/drakeet/MultiType)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/drakeet/MultiType/blob/master/LICENSE)\n![maven-central](https://img.shields.io/maven-central/v/com.drakeet.multitype/multitype.svg)\n![jetbrains-plugin](https://img.shields.io/jetbrains/plugin/v/9202-a8translate.svg)\n\nPreviously, when we need to develop a complex RecyclerView / ListView, it is difficult and\ntroublesome work. We should override the `getItemViewType()` of `RecyclerView.Adapter` , add some types, and create some `ViewHolder`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, \nand these adapter classes will get more complicated.\n\nNowadays, I created a new intuitive and flexible way to easily create complex RecyclerViews,\n**with the MultiType library, we could insert a new item type without changing any old adapter codes and make them more readable.**\n\n## Getting started\n\nIn your `build.gradle`:\n\n_MultiType has been rebuilt based on [AndroidX](https://developer.android.com/jetpack/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`._\n\n_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](https://github.com/drakeet/MultiType/tree/3.x)._\n\n```groovy\ndependencies {\n  implementation 'com.drakeet.multitype:multitype:4.3.0'\n}\n```\n\n## Usage\n\n#### Step 1. Create a Kotlin `class` or `data class`, for example:\n\n```kotlin\ndata class Foo(\n  val value: String\n)\n```\n\n#### Step 2. Create a class extends `ItemViewDelegate\u003cT, VH : ViewHolder\u003e`, for example:\n\n```kotlin\nclass FooViewDelegate: ItemViewDelegate\u003cFoo, FooViewDelegate.ViewHolder\u003e() {\n\n  override fun onCreateViewHolder(context: Context, parent: ViewGroup): ViewHolder {\n    // If you want a LayoutInflater parameter instead of a Context,\n    // you can use ItemViewBinder as the parent of this class.\n    return ViewHolder(FooView(context))\n  }\n\n  override fun onBindViewHolder(holder: ViewHolder, item: Foo) {\n    holder.fooView.text = item.value\n\n    Log.d(\"ItemViewDelegate API\", \"position: ${holder.bindingAdapterPosition}\")\n    Log.d(\"ItemViewDelegate API\", \"items: $adapterItems\")\n    Log.d(\"ItemViewDelegate API\", \"adapter: $adapter\")\n    Log.d(\"More\", \"Context: ${holder.itemView.context}\")\n  }\n\n  class ViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {\n    val fooView: TextView = itemView.findViewById(R.id.foo)\n  }\n}\n```\n\n##### Or if you are using a custom View instead of XML layout, you can use `ViewDelegate`:\n\n\u003e The `ViewDelegate` is a simple `ItemViewDelegate` that does not require to declare and provide a `RecyclerView.ViewHolder`.\n\n```kotlin\nclass FooViewDelegate : ViewDelegate\u003cFoo, FooView\u003e() {\n\n  override fun onCreateView(context: Context): FooView {\n    return FooView(context).apply { layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT) }\n  }\n\n  override fun onBindView(view: FooView, item: Foo) {\n    view.imageView.setImageResource(item.imageResId)\n    view.textView.text = item.text\n\n    view.textView.text = \"\"\"\n      |${item.text}\n      |viewHolder: ${view.holder}\n      |layoutPosition: ${view.layoutPosition}\n      |absoluteAdapterPosition: ${view.absoluteAdapterPosition}\n      |bindingAdapterPosition: ${view.bindingAdapterPosition}\n    \"\"\".trimMargin()\n  }\n}\n```\n\n(See [`RichViewDelegate`](sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichViewDelegate.kt) \u0026 [`RichView`](sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichView.kt) examples for more details)\n\n#### Step 3. `register` your types and setup your `RecyclerView`, for example:\n\n```kotlin\nclass SampleActivity : AppCompatActivity() {\n\n  private val adapter = MultiTypeAdapter()\n  private val items = ArrayList\u003cAny\u003e()\n\n  override fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    setContentView(R.layout.activity_list)\n    val recyclerView = findViewById\u003cRecyclerView\u003e(R.id.list)\n\n    adapter.register(TextItemViewDelegate())\n    adapter.register(ImageItemViewDelegate())\n    adapter.register(RichItemViewDelegate())\n    recyclerView.adapter = adapter\n\n    val textItem = TextItem(\"world\")\n    val imageItem = ImageItem(R.mipmap.ic_launcher)\n    val richItem = RichItem(\"小艾大人赛高\", R.drawable.img_11)\n\n    for (i in 0..19) {\n      items.add(textItem)\n      items.add(imageItem)\n      items.add(richItem)\n    }\n    adapter.items = items\n    adapter.notifyDataSetChanged()\n  }\n}\n```\n\n**That's all, you're good to go!**\n\n## Advanced usage \n\n**One to many**:  \n\n```kotlin\nadapter.register(Data::class).to(\n  DataType1ViewDelegate(),\n  DataType2ViewDelegate()\n).withKotlinClassLinker { _, data -\u003e\n  when (data.type) {\n    Data.TYPE_2 -\u003e DataType2ViewDelegate::class\n    else -\u003e DataType1ViewDelegate::class\n  }\n}\n```\n\nSee `OneDataToManyActivity`, `OneToManyFlow` and `OneToManyEndpoint` for more details.\n\n**More methods that you can override from [ItemViewDelegate](library/src/main/kotlin/me/drakeet/multitype/ItemViewDelegate.kt)**:\n\n```kotlin\nopen fun onBindViewHolder(holder: VH, item: T, payloads: List\u003cAny\u003e)\nopen fun getItemId(item: T): Long\nopen fun onViewRecycled(holder: VH)\nopen fun onFailedToRecycleView(holder: VH): Boolean\nopen fun onViewAttachedToWindow(holder: VH)\nopen fun onViewDetachedFromWindow(holder: VH)\n```\n\n## Android Studio Plugin\n\n- **[drakeet/MultiTypeTemplates](https://github.com/drakeet/MultiTypeTemplates)**\n\n An intellij idea plugin for Android to generate `MultiType` `Item` and `ItemViewDelegate` easily.\n\n\u003cimg src=\"http://ww4.sinaimg.cn/large/86e2ff85gw1f8yj0sejd6j21340ben1s.jpg\" width=640/\u003e\n\n## Screenshots\n\nPages created with MultiType:\n\n\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa7d5968b.png\" width=216/\u003e\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa7d83aec.png\" width=216/\u003e\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa7fbbc87.png\" width=216/\u003e\n\n\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa83af0f7.png\" width=216/\u003e\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa843e488.png\" width=216/\u003e\u003cimg src=\"https://i.loli.net/2018/06/05/5b16aa86c52e7.png\" width=216/\u003e\n\n\u003cimg src=\"https://i.loli.net/2017/10/20/59e95e4c78f5b.png\" width=270/\u003e \u003cimg src=\"https://i.loli.net/2017/10/20/59e95e4c8243c.png\" width=270/\u003e\n\nLicense\n-------\n\n    Copyright (c) 2016-present. Drakeet Xu\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","funding_links":[],"categories":["Libraries","UI","Kotlin","RecyclerView","Mobile Development","Java"],"sub_categories":["C++/C Toolkit"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrakeet%2FMultiType","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrakeet%2FMultiType","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrakeet%2FMultiType/lists"}