16
16
package com.google.firebase.dataconnect.minimaldemo
17
17
18
18
import android.os.Bundle
19
+ import android.view.LayoutInflater
19
20
import android.view.View
21
+ import android.view.ViewGroup
20
22
import androidx.activity.viewModels
21
23
import androidx.appcompat.app.AppCompatActivity
22
24
import androidx.lifecycle.flowWithLifecycle
23
25
import androidx.lifecycle.lifecycleScope
26
+ import androidx.recyclerview.widget.LinearLayoutManager
27
+ import androidx.recyclerview.widget.RecyclerView
28
+ import com.google.firebase.dataconnect.minimaldemo.connector.GetAllItemsQuery
24
29
import com.google.firebase.dataconnect.minimaldemo.databinding.ActivityListItemsBinding
30
+ import com.google.firebase.dataconnect.minimaldemo.databinding.ListItemBinding
25
31
import kotlinx.coroutines.flow.collectLatest
26
32
import kotlinx.coroutines.launch
27
33
@@ -36,6 +42,7 @@ class ListItemsActivity : AppCompatActivity() {
36
42
myApplication = application as MyApplication
37
43
38
44
viewBinding = ActivityListItemsBinding .inflate(layoutInflater)
45
+ viewBinding.recyclerView.layoutManager = LinearLayoutManager (this )
39
46
setContentView(viewBinding.root)
40
47
41
48
lifecycleScope.launch {
@@ -57,18 +64,48 @@ class ListItemsActivity : AppCompatActivity() {
57
64
viewBinding.statusText.text = " Loading Items..."
58
65
viewBinding.statusText.visibility = View .VISIBLE
59
66
viewBinding.recyclerView.visibility = View .GONE
67
+ viewBinding.recyclerView.adapter = null
60
68
} else if (items != = null ) {
61
- viewBinding.statusText.text = " Items: $items "
62
- viewBinding.statusText.visibility = View .VISIBLE
63
- viewBinding.recyclerView.visibility = View .GONE
69
+ viewBinding.statusText.text = null
70
+ viewBinding.statusText.visibility = View .GONE
71
+ viewBinding.recyclerView.visibility = View .VISIBLE
72
+ val oldAdapter = viewBinding.recyclerView.adapter as ? RecyclerViewAdapterImpl
73
+ if (oldAdapter == = null || oldAdapter.items != = items) {
74
+ viewBinding.recyclerView.adapter = RecyclerViewAdapterImpl (items)
75
+ }
64
76
} else if (exception != = null ) {
65
77
viewBinding.statusText.text = " Loading items FAILED: $exception "
66
78
viewBinding.statusText.visibility = View .VISIBLE
67
79
viewBinding.recyclerView.visibility = View .GONE
80
+ viewBinding.recyclerView.adapter = null
68
81
} else {
69
82
viewBinding.statusText.text = null
70
83
viewBinding.statusText.visibility = View .GONE
71
84
viewBinding.recyclerView.visibility = View .GONE
72
85
}
73
86
}
87
+
88
+ private class RecyclerViewAdapterImpl (val items : List <GetAllItemsQuery .Data .ItemsItem >) :
89
+ RecyclerView .Adapter <RecyclerViewViewHolderImpl >() {
90
+
91
+ override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): RecyclerViewViewHolderImpl {
92
+ val binding = ListItemBinding .inflate(LayoutInflater .from(parent.context), parent, false )
93
+ return RecyclerViewViewHolderImpl (binding)
94
+ }
95
+
96
+ override fun getItemCount () = items.size
97
+
98
+ override fun onBindViewHolder (holder : RecyclerViewViewHolderImpl , position : Int ) {
99
+ holder.bindTo(items[position])
100
+ }
101
+ }
102
+
103
+ private class RecyclerViewViewHolderImpl (private val binding : ListItemBinding ) :
104
+ RecyclerView .ViewHolder (binding.root) {
105
+
106
+ fun bindTo (item : GetAllItemsQuery .Data .ItemsItem ) {
107
+ binding.id.text = item.id.toString()
108
+ binding.name.text = item.string
109
+ }
110
+ }
74
111
}
0 commit comments