core

Core of TypedRecyclerview to create multi-typed RecyclerViews

Лицензия

Лицензия

Группа

Группа

io.gierla.typedrecyclerview
Идентификатор

Идентификатор

core
Последняя версия

Последняя версия

0.0.22
Дата

Дата

Тип

Тип

aar
Описание

Описание

core
Core of TypedRecyclerview to create multi-typed RecyclerViews
Ссылка на сайт

Ссылка на сайт

https://github.com/MaxGierlachowski/TypedRecyclerView
Система контроля версий

Система контроля версий

https://github.com/MaxGierlachowski/TypedRecyclerView

Скачать core

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/io.gierla.typedrecyclerview/core/ -->
<dependency>
    <groupId>io.gierla.typedrecyclerview</groupId>
    <artifactId>core</artifactId>
    <version>0.0.22</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/io.gierla.typedrecyclerview/core/
implementation 'io.gierla.typedrecyclerview:core:0.0.22'
// https://jarcasting.com/artifacts/io.gierla.typedrecyclerview/core/
implementation ("io.gierla.typedrecyclerview:core:0.0.22")
'io.gierla.typedrecyclerview:core:aar:0.0.22'
<dependency org="io.gierla.typedrecyclerview" name="core" rev="0.0.22">
  <artifact name="core" type="aar" />
</dependency>
@Grapes(
@Grab(group='io.gierla.typedrecyclerview', module='core', version='0.0.22')
)
libraryDependencies += "io.gierla.typedrecyclerview" % "core" % "0.0.22"
[io.gierla.typedrecyclerview/core "0.0.22"]

Зависимости

runtime (4)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.30
androidx.appcompat » appcompat jar 1.1.0
androidx.core » core-ktx jar 1.3.0
androidx.recyclerview » recyclerview jar 1.1.0

Модули Проекта

Данный проект не имеет модулей.

TypedRecyclerView

A lot of Android Apps today have complicated lists with multiple different views. The way to go today is to use a RecyclerView but the setup of a RecyclerView with a Adapter with multiple view types is tedious and error-prone.

I present to you TypedRecyclerView. A very small library to simplify the creation of multi-type RecyclerViews.

Setup

For every item type in your RecyclerView create a class that will represent the state of that item. This class has to implement the TypedRecyclerViewItem interface.

data class Item(
    val id: Long = 0L,
    val text: String = ""
) : TypedRecyclerViewItem {
    override fun getItemType(): Int {
        return 0
    }

    override fun isTheSameAs(other: TypedRecyclerViewItem): Boolean {
        return if (other is Item) {
            this.id == other.id
        } else {
            false
        }
    }

    override fun contentsAreTheSameAs(other: TypedRecyclerViewItem): Boolean {
        return if (other is Item) {
            this == other
        } else {
            false
        }
    }
}

Afterwards you just have to use TypedRecyclerViewAdapter as your RecyclerView Adapter.

Create a TypedViewHolder so that the RecyclerView knows how to handle your views:

class TypeOne : TypedViewHolder {
    override fun onCreateViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val viewOne = YourView(parent.context)
        viewOne.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        viewOne.id = ViewCompat.generateViewId()
        return ViewHolder(viewOne)
    }

    override fun onBindViewHolder(
        typedItem: TypedRecyclerViewItem?,
        holder: RecyclerView.ViewHolder
    ) {
        (holder as? ViewHolder)?.let { currentViewHolder ->
            (typedItem as? Item)?.let { currentItem ->
                currentViewHolder.view.text = currentItem.text
            }
        }
    }

    class ViewHolder(val view: YourView) : RecyclerView.ViewHolder(view)
}

Finally you have to register the different types of views:

adapter.registerViewHolderType(0, TypeOne())

Thats it, you can now use your RecyclerView as you are used to and use the function dispatchItems(items) on your adpter to update its data.

A full example can be found inside the repository under /sample.

Версии библиотеки

Версия
0.0.22