All Projects → drakeet → Multitype

drakeet / Multitype

Licence: apache-2.0
Easier and more flexible to create multiple types for Android RecyclerView.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Multitype

LxAdapter
RecyclerView Adapter Library
Stars: ✭ 50 (-99.06%)
Mutual labels:  recyclerview, recyclerview-multi-type
MultiTypeAdapter
RecyclerView通用多类型适配器MultiTypeAdapter,以布局文件为单位更细粒度的条目复用。
Stars: ✭ 18 (-99.66%)
Mutual labels:  recyclerview, one-to-many
AccordionRecycler
Android RecyclerView Adapter with nested items & expand/contract functionality
Stars: ✭ 17 (-99.68%)
Mutual labels:  recyclerview, recyclerview-multi-type
Statik
A simple static list information backed by RecyclerView for Android in Kotlin
Stars: ✭ 22 (-99.58%)
Mutual labels:  recyclerview, recyclerview-multi-type
CommonRecycler
极度封装RecyclerView里的adapter,holder,让其支持各种点击事件,使用方便
Stars: ✭ 19 (-99.64%)
Mutual labels:  recyclerview, recyclerview-multi-type
kandy
Sweet Android libraries written in Kotlin
Stars: ✭ 19 (-99.64%)
Mutual labels:  recyclerview, recyclerview-multi-type
RecyclerELE
Android Library for easy addition of Empty, Loading and Error views in a RecyclerView
Stars: ✭ 27 (-99.49%)
Mutual labels:  recyclerview, recyclerview-multi-type
Recycler Fast Scroll
Provides fast scroll and section idexer for recycler view
Stars: ✭ 445 (-91.6%)
Mutual labels:  recyclerview
Suspensionbar
A RecyclerView suspension bar implementation like Instagram
Stars: ✭ 494 (-90.68%)
Mutual labels:  recyclerview
Recyclerview Gallery
Recyclerview-Gallery:This library shows you a gallery using RecyclerView.
Stars: ✭ 420 (-92.07%)
Mutual labels:  recyclerview
Recyclerviewtemplate
One Template which solves all frequently used RecyclerViews Code Snippets
Stars: ✭ 404 (-92.37%)
Mutual labels:  recyclerview
Androidwithkotlin
🚀 These are android sample projects which are written in Kotlin. It covers video streaming, mp3 player, sqlite, location services, custom camera, o-notifications, simple compass etc.
Stars: ✭ 447 (-91.56%)
Mutual labels:  recyclerview
Coder
Android Material Design 风格控件的学习及遇到的问题;Tablayout | 横向布局标签,TextInputLayout | 文字输入布局 ,FloatingActionButton | 悬浮按钮, CoordinatorLayout APPBarLayout CollapsingTabLayout实现折叠头布局,BottomSheetDialog | 底部对话框,Touch Feedback| 触摸反馈,Reveal Effect| 揭示效果,Curved motion | 曲线运动,Animated Vector Drawables | 矢量图片动画
Stars: ✭ 502 (-90.52%)
Mutual labels:  recyclerview
Alphabetindex Fast Scroll Recyclerview
A Powerful AlphabetIndex FastScroller Library for Android's RecyclerView!
Stars: ✭ 444 (-91.62%)
Mutual labels:  recyclerview
Android Tv Widget
Android tv,盒子,投影仪 控件
Stars: ✭ 536 (-89.88%)
Mutual labels:  recyclerview
Scrollingpagerindicator
Pager indicator inspired by Instagram. Lightweight and easy to set up.
Stars: ✭ 419 (-92.09%)
Mutual labels:  recyclerview
Baserecyclerviewadapterhelper
BRVAH:Powerful and flexible RecyclerAdapter
Stars: ✭ 22,524 (+325.14%)
Mutual labels:  recyclerview
Gravitysnaphelper
A SnapHelper that snaps a RecyclerView to an edge.
Stars: ✭ 4,822 (-8.98%)
Mutual labels:  recyclerview
Spannedgridlayoutmanager
Android RecyclerView.LayoutManager that resizes and reorders views based on SpanSize
Stars: ✭ 492 (-90.71%)
Mutual labels:  recyclerview
Indicatordialog
a dialog with arrow indicator in the location where you want
Stars: ✭ 485 (-90.85%)
Mutual labels:  recyclerview

MultiType

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

Build Status License 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.

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

Usage

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

data class Foo(
  val value: String
)

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

class FooViewDelegate: ItemViewDelegate<Foo, FooViewDelegate.ViewHolder>() {

  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.

class FooViewDelegate : ViewDelegate<Foo, FooView>() {

  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.absoluteAdapterPosition}
      |bindingAdapterPosition: ${view.bindingAdapterPosition}
    """.trimMargin()
  }
}

(See RichViewDelegate & RichView examples for more details)

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

class SampleActivity : AppCompatActivity() {

  private val adapter = MultiTypeAdapter()
  private val items = ArrayList<Any>()

  override fun onCreate(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 = adapter

    val textItem = TextItem("world")
    val imageItem = ImageItem(R.mipmap.ic_launcher)
    val richItem = RichItem("小艾大人赛高", R.drawable.img_11)

    for (i in 0..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::class
    else -> DataType1ViewDelegate::class
  }
}

See OneDataToManyActivity, OneToManyFlow and OneToManyEndpoint for more details.

More methods that you can override from ItemViewDelegate:

open fun onBindViewHolder(holder: VH, item: T, payloads: List<Any>)
open fun getItemId(item: T): Long
open fun onViewRecycled(holder: VH)
open fun onFailedToRecycleView(holder: VH): Boolean
open fun onViewAttachedToWindow(holder: VH)
open fun onViewDetachedFromWindow(holder: VH)

Android Studio Plugin

An intellij idea plugin for Android to generate MultiType Item and ItemViewDelegate easily.

Screenshots

Pages created with MultiType:

License

Copyright (c) 2016-present. Drakeet Xu

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

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].