|
| 1 | +[](https://search.maven.org/artifact/com.matbadev.dabirva/dabirva) |
| 2 | +[](https://github.com/matbadev/dabirva/actions/workflows/publish.yml) |
| 3 | +[](https://codecov.io/gh/matbadev/dabirva) |
| 4 | +[](https://www.apache.org/licenses/LICENSE-2.0.html) |
| 5 | + |
1 | 6 | # Dabirva (Data Binding RecyclerView Adapter) |
2 | 7 |
|
3 | | -[](https://github.com/matbadev/dabirva/actions/workflows/publish.yml) |
| 8 | +Dabirva is a simple and extensible adapter for Android's RecyclerView to easily build lists using the [Data Binding Library](https://developer.android.com/topic/libraries/data-binding/). |
| 9 | + |
| 10 | +Features: |
| 11 | + |
| 12 | +- Seamless integration into the MVVM pattern ([recommended by Google](https://developer.android.com/jetpack/guide)) |
| 13 | +- Asynchronous item diffing by default for better performance |
| 14 | +- Decorations binding |
| 15 | +- Sticky headers (for horizontal and vertical linear layouts) |
| 16 | +- Extensible classes |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Dabirva is available via the [Central Repository](https://central.sonatype.org/): |
| 21 | + |
| 22 | +```groovy |
| 23 | +dependencies { |
| 24 | + implementation "com.matbadev.dabirva:dabirva:1.0.0" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +This library requires at least Java 8: |
| 29 | + |
| 30 | +```groovy |
| 31 | +android { |
| 32 | + compileOptions { |
| 33 | + sourceCompatibility = JavaVersion.VERSION_1_8 |
| 34 | + targetCompatibility = JavaVersion.VERSION_1_8 |
| 35 | + } |
| 36 | +
|
| 37 | + kotlinOptions { |
| 38 | + jvmTarget = JavaVersion.VERSION_1_8 |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +### Defining item view models |
| 46 | + |
| 47 | +To define an item view model (which can later be bound to a RecyclerView) start by implementing [ItemViewModel](dabirva/src/main/java/com/matbadev/dabirva/ItemViewModel.kt). |
| 48 | +For a simple note this might look like this: |
| 49 | + |
| 50 | +```kotlin |
| 51 | +data class NoteViewModel( |
| 52 | + val id: Long, |
| 53 | + val text: String, |
| 54 | +) : ItemViewModel { |
| 55 | + |
| 56 | + override val bindingId: Int |
| 57 | + get() = BR.viewModel |
| 58 | + |
| 59 | + override val layoutId: Int |
| 60 | + get() = R.layout.item_note |
| 61 | + |
| 62 | + override fun entityEquals(other: Any?): Boolean { |
| 63 | + return other is NoteViewModel && id == other.id |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Item view models need to be **bindable** which requires them to define a **binding ID** (from the generated ` BR` class) and a **layout ID** (from the generated `R.layout` class). |
| 70 | + |
| 71 | +Furthermore they need to be **diffable** which requires a proper ` equals` and `hashCode` implementation (e.g. by using a Kotlin data class) and an implementation of `entityEquals` which defines when two item view models describe the same entity (e.g. when they have the same ID). |
| 72 | +Dabirva uses this information to detect item positions after list updates to display proper item animations. |
| 73 | + |
| 74 | +The second step is to add the referenced layout with the view model's class as a variable. |
| 75 | +For the previous note example this might look like this: |
| 76 | + |
| 77 | +```xml |
| 78 | +<?xml version="1.0" encoding="utf-8"?> |
| 79 | +<layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 80 | + xmlns:tools="http://schemas.android.com/tools"> |
| 81 | + |
| 82 | + <data> |
| 83 | + <variable |
| 84 | + name="viewModel" |
| 85 | + type="com.matbadev.dabirva.example.NoteViewModel" /> |
| 86 | + </data> |
| 87 | + |
| 88 | + <TextView |
| 89 | + android:layout_width="match_parent" |
| 90 | + android:layout_height="wrap_content" |
| 91 | + android:text="@{viewModel.text}" |
| 92 | + tools:text="Some text" /> |
| 93 | + |
| 94 | +</layout> |
| 95 | +``` |
| 96 | + |
| 97 | +### Binding item view models to a RecyclerView |
| 98 | + |
| 99 | +To bind item view models to a RecyclerView using Dabirva as an adapter, first declare a list of [ItemViewModel](dabirva/src/main/java/com/matbadev/dabirva/ItemViewModel.kt) in your screen's view model using an observable type: |
| 100 | + |
| 101 | +```kotlin |
| 102 | +// Using ObservableField |
| 103 | +val items = ObservableField<List<ItemViewModel>?>() |
| 104 | + |
| 105 | +// Using NonNullObservableField (provided by Dabirva) |
| 106 | +val items = NonNullObservableField<List<ItemViewModel>>(listOf()) |
| 107 | + |
| 108 | +// Using MutableLiveData |
| 109 | +val items = MutableLiveData<List<ItemViewModel>?>() |
| 110 | +``` |
| 111 | + |
| 112 | +Next bind the items to a RecyclerView in your XML layout: |
| 113 | + |
| 114 | +```xml |
| 115 | +<?xml version="1.0" encoding="utf-8"?> |
| 116 | +<layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 117 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 118 | + xmlns:tools="http://schemas.android.com/tools"> |
| 119 | + |
| 120 | + <data> |
| 121 | + <variable |
| 122 | + name="viewModel" |
| 123 | + type="com.matbadev.dabirva.example.ActivityViewModel" /> |
| 124 | + </data> |
| 125 | + |
| 126 | + <androidx.recyclerview.widget.RecyclerView |
| 127 | + android:layout_width="match_parent" |
| 128 | + android:layout_height="match_parent" |
| 129 | + android:orientation="vertical" |
| 130 | + app:dabirvaItems="@{viewModel.items}" |
| 131 | + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" |
| 132 | + tools:listitem="@layout/item_note" /> |
| 133 | + |
| 134 | +</layout> |
| 135 | +``` |
| 136 | + |
| 137 | +When the `dabirvaItems` binding adapter is executed it will create an instance of `Dabirva` and attach it to the RecyclerView. |
| 138 | +The displayed list will automatically be updated with proper item animations once the `items` field in the screen's view model is changed. |
| 139 | + |
| 140 | +### Add decorations to RecyclerView items |
| 141 | + |
| 142 | +Dabirva provides the `itemDecorations` binding adapter for adding decorations to RecyclerView items. |
| 143 | +First they need to be defined in the screen's view model: |
| 144 | + |
| 145 | +```kotlin |
| 146 | +val itemDecorations = listOf<RecyclerView.ItemDecoration>( |
| 147 | + /* some item decorations */ |
| 148 | +) |
| 149 | +``` |
| 150 | + |
| 151 | +After that they can easily be bound to a RecyclerView in the screen's XML layout: |
| 152 | + |
| 153 | +```xml |
| 154 | +<?xml version="1.0" encoding="utf-8"?> |
| 155 | +<layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 156 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 157 | + xmlns:tools="http://schemas.android.com/tools"> |
| 158 | + |
| 159 | + <data> |
| 160 | + <variable |
| 161 | + name="viewModel" |
| 162 | + type="com.matbadev.dabirva.example.ActivityViewModel" /> |
| 163 | + </data> |
| 164 | + |
| 165 | + <androidx.recyclerview.widget.RecyclerView |
| 166 | + android:layout_width="match_parent" |
| 167 | + android:layout_height="match_parent" |
| 168 | + android:orientation="vertical" |
| 169 | + app:itemDecorations="@{viewModel.itemDecorations}" |
| 170 | + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" |
| 171 | + tools:listitem="@layout/item_note" /> |
| 172 | + |
| 173 | +</layout> |
| 174 | +``` |
| 175 | + |
| 176 | +This can be done without using the `dabirvaItems` binding adapter. |
| 177 | + |
| 178 | +#### Sticky headers |
| 179 | + |
| 180 | +Dabirva supports sticky list headers for horizontal and vertical linear layouts. |
| 181 | +**This requires an instance of `Dabirva` to be used as RecyclerView adapter.** |
| 182 | + |
| 183 | +To use sticky headers simply add a corresponding instance to the `itemDecorations` field in the screen's view model: |
| 184 | + |
| 185 | +```kotlin |
| 186 | +val itemDecorations = listOf<RecyclerView.ItemDecoration>( |
| 187 | + HorizontalStickyHeaderDecoration( |
| 188 | + headerPositionProvider = ItemHeaderProvider { it is HeaderViewModel }, |
| 189 | + ), |
| 190 | +) |
| 191 | +``` |
| 192 | + |
| 193 | +As you can see you need to provide a [HeaderPositionProvider](dabirva/src/main/java/com/matbadev/dabirva/decoration/HeaderPositionProvider.kt) for defining the items to use as headers. |
| 194 | +The easiest way is to implement [ItemHeaderProvider](dabirva/src/main/java/com/matbadev/dabirva/decoration/ItemHeaderProvider.kt) which just requires a predicate to decide if a specific item should be used as a header. |
| 195 | + |
| 196 | +### Advanced: use custom `Executor` for item diffing |
| 197 | + |
| 198 | +To perform the item diffing process on a custom `Executor` provide a suitable factory to [DabirvaConfig](dabirva/src/main/java/com/matbadev/dabirva/DabirvaConfig.kt) which is used by the `dabirvaItems` binding adapter. |
| 199 | +This can also be used to execute the diffing synchronously on the main thread: |
| 200 | + |
| 201 | +```kotlin |
| 202 | +DabirvaConfig.factory = DabirvaFactory { Dabirva(diffExecutor = Runnable::run) } |
| 203 | +``` |
| 204 | + |
| 205 | +This might cause UI lags for complex lists and is therefore not recommended. |
| 206 | + |
| 207 | +### Advanced: extend the `Dabirva` class |
| 208 | + |
| 209 | +Dabirva supports subclassing the `Dabirva` class to add custom functionality: |
| 210 | + |
| 211 | +```kotlin |
| 212 | +class CustomDabirva : Dabirva() { |
| 213 | + // Add custom logic here by overwriting the correspondent methods. |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +To make the `dabirvaItems` binding adapter instantiate your custom class instead of the default `Dabirva` one you need to supply a suitable factory to [DabirvaConfig](dabirva/src/main/java/com/matbadev/dabirva/DabirvaConfig.kt): |
| 218 | + |
| 219 | +```kotlin |
| 220 | +DabirvaConfig.factory = DabirvaFactory { CustomDabirva() } |
| 221 | +``` |
| 222 | + |
| 223 | +## License |
| 224 | + |
| 225 | +``` |
| 226 | +Copyright 2021 Matthias Bäuerle |
| 227 | +
|
| 228 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 229 | +you may not use this file except in compliance with the License. |
| 230 | +You may obtain a copy of the License at |
| 231 | +
|
| 232 | + http://www.apache.org/licenses/LICENSE-2.0 |
4 | 233 |
|
5 | | -[](https://codecov.io/gh/matbadev/dabirva) |
| 234 | +Unless required by applicable law or agreed to in writing, software |
| 235 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 236 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 237 | +See the License for the specific language governing permissions and |
| 238 | +limitations under the License. |
| 239 | +``` |
0 commit comments