core

Core of TypedRecyclerview to create multi-typed RecyclerViews

License

License

GroupId

GroupId

io.gierla.typedrecyclerview
ArtifactId

ArtifactId

core
Last Version

Last Version

0.0.22
Release Date

Release Date

Type

Type

aar
Description

Description

core
Core of TypedRecyclerview to create multi-typed RecyclerViews
Project URL

Project URL

https://github.com/MaxGierlachowski/TypedRecyclerView
Source Code Management

Source Code Management

https://github.com/MaxGierlachowski/TypedRecyclerView

Download core

How to add to project

<!-- 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"]

Dependencies

runtime (4)

Group / Artifact Type Version
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

Project Modules

There are no modules declared in this project.

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.

Versions

Version
0.0.22