Slide 40
Slide 40 text
FIXED 8. User events in RecyclerViews
40
Optional
data class SampleItem(
val text: String,
val bgColor: Int,
val isFavorite: Boolean = false,
//
FIXED 8. add field action
val onFavorite: (String)
->
Unit
)
class SampleViewHolder(
private val binding: ItemSampleBinding
) : RecyclerView.ViewHolder(binding.root) {
private var item: SampleItem? = null
init {
binding.btnButton.setOnClickListener {
item
?.
let { safeItem
->
//
FIXED 8. use action
safeItem.onFavorite(safeItem.text)
}
}
class SearchViewModel(
.
..
) {
fun generate() {
val item = SampleItem(
text =
.
..
,
bgColor =
..
.
,
isFavorite = …,
//
FIXED 8. pending favorite function
onFavorite =
::
updateFavorite
)
...
}
/
/
FIXED 8. change visibility modifier
private fun updateFavorite(text: String) {
...
}
}
UI events ~ Handle user events
h
tt
ps://developer.android.com/topic/architecture/ui-layer/
events#recyclerview-events