Skip to content

Commit 68eb992

Browse files
committed
Recycler view multi items example added
1 parent b06bd0b commit 68eb992

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

features/example1/src/main/java/ru/bis/example1/view_models/Example1ViewModel.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ class Example1ViewModel @Inject constructor(
1616
private val getDataFromCache: GetDataFromCache
1717
) : BaseViewModel(application) {
1818

19+
companion object {
20+
private const val RV_TITLE = 1
21+
private const val RV_ITEM = 2
22+
}
23+
1924
val result = ObservableField<String>()
2025
val items = mutableListOf(
21-
RecyclerViewBaseDataModel("Label 1", 1),
22-
RecyclerViewBaseDataModel("Label 2", 1),
23-
RecyclerViewBaseDataModel("Label 3", 1),
24-
RecyclerViewBaseDataModel("Label 4", 1),
25-
RecyclerViewBaseDataModel("Label 5", 1),
26-
RecyclerViewBaseDataModel("Label 6", 1)
26+
RecyclerViewBaseDataModel("Title 1", RV_TITLE),
27+
RecyclerViewBaseDataModel("Label 1", RV_ITEM),
28+
RecyclerViewBaseDataModel("Title 2", RV_TITLE),
29+
RecyclerViewBaseDataModel("Label 1", RV_ITEM),
30+
RecyclerViewBaseDataModel("Label 2", RV_ITEM),
31+
RecyclerViewBaseDataModel("Label 3", RV_ITEM),
32+
RecyclerViewBaseDataModel("Title 3", RV_TITLE),
33+
RecyclerViewBaseDataModel("Label 1", RV_ITEM),
34+
RecyclerViewBaseDataModel("Label 2", RV_ITEM),
35+
RecyclerViewBaseDataModel("Label 3", RV_ITEM),
36+
RecyclerViewBaseDataModel("Label 4", RV_ITEM),
2737
)
2838

2939
override fun init() {
@@ -39,6 +49,7 @@ class Example1ViewModel @Inject constructor(
3949
}
4050

4151
fun recyclerViewAdapter() = RecyclerViewAdapter.Builder(this, BR.viewModel)
42-
.addProducer(1, R.layout.item_example1, String::class.java, ItemViewModel::class.java)
52+
.addProducer(RV_TITLE, R.layout.item_title, String::class.java, TitleViewModel::class.java)
53+
.addProducer(RV_ITEM, R.layout.item_example1, String::class.java, ItemViewModel::class.java)
4354
.build(items)
4455
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.bis.example1.view_models
2+
3+
import androidx.databinding.ObservableField
4+
import ru.sir.presentation.base.recycler_view.RecyclerViewBaseItem
5+
6+
class TitleViewModel : RecyclerViewBaseItem<String, Example1ViewModel>() {
7+
val title = ObservableField("")
8+
9+
override fun bindData(data: String) {
10+
title.set(data)
11+
}
12+
}

features/example1/src/main/res/layout/item_example1.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<androidx.appcompat.widget.AppCompatTextView
1717
android:layout_width="match_parent"
1818
android:layout_height="match_parent"
19-
android:gravity="center"
2019
app:layout_constraintTop_toTopOf="parent"
2120
app:layout_constraintEnd_toEndOf="parent"
2221
app:layout_constraintStart_toStartOf="parent"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<data>
6+
<variable
7+
name="viewModel"
8+
type="ru.bis.example1.view_models.TitleViewModel" />
9+
</data>
10+
11+
<androidx.constraintlayout.widget.ConstraintLayout
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content"
14+
xmlns:app="http://schemas.android.com/apk/res-auto">
15+
16+
<androidx.appcompat.widget.AppCompatTextView
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
app:layout_constraintTop_toTopOf="parent"
20+
app:layout_constraintEnd_toEndOf="parent"
21+
app:layout_constraintStart_toStartOf="parent"
22+
android:text="@{viewModel.title}"
23+
tools:text="Item"
24+
android:textSize="15sp"
25+
android:textColor="#000000"
26+
android:padding="10dp" />
27+
28+
</androidx.constraintlayout.widget.ConstraintLayout>
29+
30+
</layout>

0 commit comments

Comments
 (0)