You can create a generic lazyColumn by giving the opportunity for the caller to use the item composable that he want with a @Composable function callback.
Exemple :
@Composable
fun <T> MyLazyColumn(
lazyItems: Array<T>,
onClickItem: (T) -> Unit,
item: @Composable RowScope.(item: T) -> Unit,
) {
LazyColumn() {
items(lazyItems) { choice ->
Row(
modifier = Modifier.clickable(onClick = { onClickItem(choice) })
) {
item(choice)
}
}
}
}
In your scaffold :
Scaffold(
content = {
MyLazyColumn<Starters>(
lazyItems = arrayOf(Starters.Canapes, ...),
onClickItem = startersClickListner
) {
Text(text = stringResource(it.textResId) )
}
}
)
If you want to use a custom key you can add another function parameter
@Composable
fun <T> MyLazyColumn(
lazyItems: Array<T>,
onClickItem: (T) -> Unit,
key: ((item: T) -> Any)? = null,
item: @Composable RowScope.(item: T) -> Unit
) {
LazyColumn() {
items(
items = lazyItems,
key = key
) { choice ->
Row(
modifier = Modifier.clickable(onClick = { onClickItem(choice) })
) {
item(choice)
}
}
}
}
And on the caller side, you can use it like this
data class Foo(
val id: Int
)
MyLazyColumn(
lazyItems = arrayOf(
Foo(1), Foo(2)
),
key = {
it.id
},
onClickItem = {
// ...
}
) {
// ...
}