Closed Diy2210 closed 4 years ago
You should to add clickable
widget to the ServerUnitItem
to handle clicks. And pass a callback to constructor of the unit class:
class ServerUnitItem(
// ...
private val clickListener: (ServerModel) -> Unit
) : WidgetsTableUnitItem<ServerModel>(itemId, server) {
// ...
override fun createWidget(data: LiveData<ServerModel>): UnitItemRoot {
return with(theme) {
clickable( // Wrap some widget into clickable widget
onClick = { // Add onClick action
clickListener(data.value)
},
child = constraint(size = WidgetSize.WidthAsParentHeightWrapContent) {
// ...
}
).let { UnitItemRoot.from(it) }
}
}
}
And then in the function of unit creation in the screen class you can handle clicks:
fun serversToTableUnits(servers: List<ServerModel>, viewModel: ServerViewModel): List<TableUnitItem> {
return servers.map { server ->
ServerUnitItem(
// ...
clickListener = { // here you can handle click to an unit
viewModel.onClickToItem(it)
}
)
}
}
Tnx!!!
How implement item list click listener? I create list on widgets like codelabs(MOKO Widgets #7 - lists on widgets), all works, but i dont understand how add listener from item click.
My code: My ViewModel:
UnitItem: