KotlinLeaner
01/05/2023, 9:21 AMColumn
inside that there are few items. Some of the item are hidden and visible on some condition. There is one Nested Column
which is adding item and auto scrolling which is done with the help of this post. Item adding will done through Add Me
CTA. Main problem comes here, when I was added more than 10 item and then we click on Show Banner
CTA, AnimatedVisibility
condition will true and display the view and Nested Column
will shifted the few items downward. I don't won't like this, instead I wan't to calculate the whole column height and always show the last item. Same as done through in the answer gif.KotlinLeaner
01/05/2023, 9:21 AM@Preview(showBackground = true, widthDp = 250, heightDp = 320)
@Composable
fun addItem() {
val scrollState = rememberScrollState()
var uiEvent by remember { mutableStateOf(Number.Initial) }
var itemFalse by remember {mutableStateOf(false)}
var itemClicked by remember { mutableStateOf(0) }
val favourites = remember { mutableStateListOf<String>() }
LaunchedEffect(itemFalse) {
for (i in 0..8) {
itemClicked++
favourites.add("item clicked $itemClicked")
}
}
LaunchedEffect(
favourites.size,
uiEvent
) {
scrollState.animateScrollTo(scrollState.maxValue)
}
Column { // 1st Column
Column(
modifier = Modifier
.verticalScroll(scrollState)
.weight(1f),
horizontalAlignment = Alignment.CenterHorizontally,
) { // 2nd Column
Text(text = "Hello world", fontSize = 20.sp)
Text(text = "Hello How are you?", fontSize = 20.sp)
AnimatedVisibility(visible = uiEvent == Number.One) {
Text(text = "Write it on your heart that every day is the best day in the year.", fontSize = 20.sp)
}
favourites.forEach { text -> // nested Column
Text(
text = text,
fontSize = 30.sp,
color = Red
)
}
}
Column { // 3rd Column
Row {
Button(onClick = {
uiEvent = Number.One
}) {
Text(text = "Show Banner")
}
Button(onClick = {
itemClicked++
favourites.add("item clicked $itemClicked")
}) {
Text(text = "Add Me")
}
}
}
}
}
enum class Number(val type: Int) {
Initial(0),
One(1);
}
KotlinLeaner
01/05/2023, 9:21 AMAdd Me
it added item on Nested Column
and autoscroll. When we click on Show Banner
, few items of Nested Column
will go downwards. I don't want it like that. It always want to make last item visible and auto scroll.
Nested Column meansthis piece of code.favourites.forEach { text ->