Skip to content

Commit 74f5948

Browse files
Update README.md
1 parent ea1e4a5 commit 74f5948

File tree

1 file changed

+67
-30
lines changed

1 file changed

+67
-30
lines changed

README.md

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
[MultiType](https://github.com/drakeet/MultiType) library makes it easier and more flexible to create multiple types for Android RecyclerView, then this library can help you make it easier to use MultiType library in Kotlin.
66

7+
If you want to use `ViewBinding` more easily in the MultiType, you can use my other [ViewBindingKTX](https://github.com/DylanCaiCoding/ViewBindingKTX) library.
8+
79
## Getting started
810

911
Add it in your root `build.gradle` at the end of repositories:
@@ -22,17 +24,21 @@ Add dependencies:
2224
```groovy
2325
dependencies {
2426
implementation 'com.drakeet.multitype:multitype:4.3.0'
25-
implementation 'com.github.DylanCaiCoding:MultiTypeKTX:0.1.0'
27+
implementation 'com.github.DylanCaiCoding:MultiTypeKTX:0.2.0'
2628
}
2729
```
2830

2931
## Usage
3032

31-
### Jetpack MVVM + MultiType + DiffUtil
33+
### Initialize the MultiTypeAdapter
3234

33-
#### The usage of a multi-type list:
35+
Register a single `ViewDelegate` , for example:
36+
37+
```kotlin
38+
private val adapter = MultiTypeAdapter(TextViewDelegate())
39+
```
3440

35-
You can register multiple `ViewDelegate` with cleaner code, for example:
41+
Register multiple `ViewDelegate` , for example:
3642

3743
```kotlin
3844
private val adapter = MultiTypeAdapter {
@@ -41,15 +47,38 @@ private val adapter = MultiTypeAdapter {
4147
}
4248
```
4349

44-
Add a variable of the `ItemsLiveData<Any>` type in `ViewModel`, for example:
50+
Register one-to-many `ViewDelegate` , for example:
51+
52+
```kotlin
53+
private val adapter = MultiTypeAdapter {
54+
register(RadioOptionViewDelegate(), MultipleOptionViewDelegate())
55+
.withKotlinClassLinker { _, item ->
56+
if (item.isSingleChoice) {
57+
RadioOptionViewDelegate::class
58+
} else {
59+
MultipleOptionViewDelegate::class
60+
}
61+
}
62+
}
63+
```
64+
65+
### Jetpack MVVM + MultiType + DiffUtil
66+
67+
#### Step 1. Add a variable of the `ItemsLiveData<T>` type in `ViewModel`, for example:
4568

4669
```kotlin
47-
class MainViewModel : ViewModel() {
70+
class MessageListViewModel : ViewModel() {
4871
val items = ItemsLiveData<Any>()
4972
}
5073
```
5174

52-
Observe items changes. The code block returns whether the old and new items are the same, for example:
75+
##### Or if you register a single `ViewDelegate`, you can use specific types, for example:
76+
77+
```kotlin
78+
val items = ItemsLiveData<Message>()
79+
```
80+
81+
#### Step 2. Observe items changes. The code block returns whether the old and new items are the same, for example:
5382

5483
```kotlin
5584
adapter.observeItemsChanged(this, viewModel.items) { oldItem, newItem ->
@@ -58,50 +87,58 @@ adapter.observeItemsChanged(this, viewModel.items) { oldItem, newItem ->
5887
}
5988
```
6089

61-
After changing the list data, you can set the list data to the `ItemsLiveData<Any>` variable, for example:
90+
#### Step 3. After changing the list data, you can set the list data to the `ItemsLiveData<T>` variable, for example:
6291

6392
```kotlin
6493
items.value = list
6594
```
6695

67-
#### The usage of a single-type list:
68-
69-
You can register a single `ViewDelegate` with cleaner code, for example:
70-
71-
```kotlin
72-
private val adapter = MultiTypeAdapter(TextViewDelegate())
73-
```
96+
### Check one or more
7497

75-
Add a variable of the `ItemsLiveData<T>` type in `ViewModel`, for example:
98+
#### Step 1. The entity class implement `ICheckable` interface, for example:
7699

77100
```kotlin
78-
class MainViewModel : ViewModel() {
79-
val items = ItemsLiveData<Message>()
80-
}
101+
data class Option(
102+
val id : Int,
103+
val name: String,
104+
override val groupId: Int, // Add it when you need to check it separately
105+
override var isChecked: Boolean = false
106+
) : ICheckable
81107
```
82108

83-
Observe items changes. The code block returns whether the old and new items are the same, for example:
109+
#### Step 2. Create a class extends `CheckableItemViewDelegate<T : ICheckable, VH : ViewHolder>`, for example:
84110

85111
```kotlin
86-
adapter.observeItemsChanged(this, viewModel.items) { oldItem, newItem ->
87-
oldItem.id == newItem.id
112+
class RadioOptionViewDelegate :
113+
CheckableItemViewDelegate<Option, BindingViewHolder<ItemOptionBinding>>(CheckType.SINGLE) {
114+
115+
override fun onCreateViewHolder(context: Context, parent: ViewGroup) =
116+
BindingViewHolder<ItemOptionBinding>(parent)
117+
.onItemClick { position ->
118+
checkItem(position) // When you need to change the checked state
119+
}
120+
121+
override fun onBindViewHolder(holder: BindingViewHolder<ItemOptionBinding>, item: Option) {
122+
with(holder.binding) {
123+
checkBox.text = item.name
124+
checkBox.isChecked = item.isChecked
125+
}
126+
}
88127
}
89128
```
90129

91-
After changing the list data, you can set the list data to the `ItemsLiveData<T>` variable, for example:
130+
The constructor of `CheckableItemViewDelegate` has a `CheckType` argument. There are the following options.
92131

93-
```kotlin
94-
items.value = list
95-
```
132+
| type | function |
133+
| ----------------------- | --------------------------- |
134+
| CheckType.SINGLE | Single choice |
135+
| CheckType.MULTIPLE | Multiple choice |
136+
| CheckType.limit(number) | Limit the number of checked |
96137

97138
## Change log
98139

99140
[Releases](https://github.com/DylanCaiCoding/MultiTypeKTX/releases)
100141

101-
## TODO
102-
103-
- CheckableItemViewDelegate
104-
105142
## License
106143

107144
```

0 commit comments

Comments
 (0)