Recyclical
recyclical, an easy-to-use Kotlin DSL API for setting up and manipulating RecyclerViews.
Gradle Dependency
Add this to your module's build.gradle
file:
The Basics
First, declare an Item class:
Second, a layout and a View Holder:
Finally, you can begin using the DSL API:
More Options
There are other things you can give to the setup extension:
Multiple Item Types
You can mix different types of items - but you need to specify view holders and layouts for them too:
DataSource
DataSource
is an interface which provides data and allows manipulation of the data, to display in a RecyclerView.
Being an interface means you make your own implementations of it, you can mock it in tests, you could even provide it
via Dagger to a presenter and manipulate the RecyclerView outside of your UI layer.
Construction
The included implementation of data source operates on a List of objects (of any type).
Manipulation
Diffing
When performing a set
on the data set, you can opt to use diff utils:
This will automatically coordinate notifying of adds, moves, and insertions so that
update of the data set is pretty and animated by the RecyclerView.
SelectableDataSource
A SelectableDataSource
is built on top of a regular [DataSource]. It provides additional APIs
to manage the selection state of items in your list.
Construction
Construction methods for SelectableDataSource
are the same as the DataSource
ones, they just
include selectable
in their names.
Manipulation
There are some additional methods added on top of the DataSource
methods:
Use in Binding
During binding of your items, you can access selection states even if you don't have a direct
reference to your DataSource
.
In onBind
blocks, this is done with extensions in ViewHolder
which provide functions to check
selection state and select/deselect the current item that is being bound.
In onClick
and onLongClick
blocks, this is done using a type that is passed as this
which provides the same set
of functions.