Issue
When creating a LazyColumn
layout, is there a way to modify the items source (i.e. Array<Starters>
) to something else so that I can reuse my LazyColumn
for a different set of items from a different array?
@Composable
fun MyLazyColumn(lazyItems: Array<Starters>,
onClickItem: (Starters) -> Unit
) {
LazyColumn() {
items(lazyItems) { choice -> Row() { Text(text = stringResource(id = choice.textResId)) } }
}
}
Scaffold(
content = {
MyLazyColumn(lazyItems = arrayOf(Starters.Canapes,...), onClickItem = startersClickListner)
}
)
Solution
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) )
}
}
)
Answered By - Dinamots
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.