KotlinLeaner
12/06/2022, 8:30 PMKotlinLeaner
12/06/2022, 8:31 PM@Composable
fun Xyz(){
Theme {
val scrollState = rememberScrollState()
LaunchedEffect(
keyOneIsTrue,
keyTwoIsTrue
) {
val newValue = scrollState.maxValue
scrollState.animateScrollTo(newValue)
}
Column(
modifier = Modifier
.padding(dimensionResource(R.dimen.margin_screen_edge_sides))
.fillMaxSize()
.verticalScroll(rememberScrollState()),
// verticalArrangement = <http://Arrangement.Top|Arrangement.Top>
or
// verticalArrangement = Arrangement.Arrangement.SpaceBetween
) {
Column(
modifier = Modifier
.verticalScroll(scrollState)
.weight(1f),
horizontalAlignment = Alignment.CenterHorizontally,
) {
// so many item in here.
// If here items is behind of 3rd column then automatically scroll the item when user land of this this screen
}
Column {
Button()
// may be more item in here
}
}
}
}KotlinLeaner
12/06/2022, 8:34 PMKotlinLeaner
12/06/2022, 8:35 PMKotlinLeaner
12/06/2022, 8:35 PMNote:- Item will be increase in 2nd Column i.e. I added logic in AnimatedVisibility so when recompose it will added the item.
Kirill Grouchnikov
12/06/2022, 8:52 PMKotlinLeaner
12/07/2022, 4:27 AMmattinger
12/07/2022, 4:59 AMval scrollState = rememberScrollState()
and pass that to the verticalScroll
modifier function you have above. Once you do that, you can ask the scrollState to scroll to a specific pixel offset within the measured size of the column. So, if the entire column is measured 1500 at pixel tall, you can ask it to scroll to any particular location:
val scope = rememberCoroutineScope()
...
scope.launch {
scrollState.animateScrollTo(1000)
}
The problem with this, of course is that it’s entirely pixel based, not based on any child index or anything like that.
If you want to scroll to specific items, i think that LazyColumn might be a better choice for you, as LazyListState
(the equivalent of ScrollState) has an animateScrollToItem
function where you can give it the index of the item.KotlinLeaner
12/07/2022, 5:09 AMJonas
12/07/2022, 10:55 AMonGloballyPositioned
:
https://git.fairkom.net/jonas/emoji-scratcher/-/blob/master/app/src/main/java/app/jonas/emojiscratcher/ui/calendar/Calendar.kt#L292KotlinLeaner
12/07/2022, 11:24 AM@Preview(showBackground = true, widthDp = 250, heightDp = 320)
@Composable
fun Xyz() {
Theme {
var itemClicked by remember { mutableStateOf(0) }
val favourites = remember { mutableStateListOf<String>() }
val scrollState = rememberScrollState()
var columnHeightPx by remember { mutableStateOf(0) }
LaunchedEffect(favourites.size > 0) {
scrollState.animateScrollTo(columnHeightPx)
}
Column(
modifier = Modifier
.padding(dimensionResource(R.dimen.margin_screen_edge_sides))
.fillMaxSize()
.verticalScroll(rememberScrollState()),
) {
Column(
modifier = Modifier
.onGloballyPositioned { coordinates ->
// Set column height using the LayoutCoordinates
columnHeightPx = coordinates.size.height
}
.verticalScroll(scrollState)
.weight(1f),
horizontalAlignment = Alignment.CenterHorizontally,
) {
favourites.forEach { text ->
Text(
text = "$text -> ${columnHeightPx}",
fontSize = 20.sp,
color = Red
)
}
}
Column {
Button(onClick = {
itemClicked++
favourites.add("item clicked $itemClicked")
}) {
Text(text = "Add me")
}
}
}
}
}
Jonas
12/07/2022, 12:49 PMKotlinLeaner
12/07/2022, 1:02 PMJonas
12/07/2022, 1:02 PMKotlinLeaner
12/07/2022, 1:03 PMJonas
12/07/2022, 1:05 PMKotlinLeaner
12/07/2022, 1:06 PMKirill Grouchnikov
12/07/2022, 1:13 PMKotlinLeaner
12/07/2022, 1:17 PM2nd column
inside there are so many items i.e. like Row
, Text
, Image
. So how can I switch all this stuff in LazyColumn
?Kirill Grouchnikov
12/07/2022, 1:19 PMKotlinLeaner
12/07/2022, 1:28 PMLazyColumn
.KotlinLeaner
12/07/2022, 1:30 PMText
, Image
.. etc to LazyColumn
?Kirill Grouchnikov
12/07/2022, 1:32 PMmattinger
12/07/2022, 1:34 PMKotlinLeaner
12/07/2022, 1:37 PMbutton
at bottom of screen which is unscrollable and other item which is above will be scrollable. I found out on this example and similar to this examplemattinger
12/07/2022, 1:37 PMval state = rememberLazyListState()
LazyColumn(state=state) {
item {
...
}
item {
...
}
}
And when you want to scroll:
scope.launch {
state.animateScrollToItem(index)
}
mattinger
12/07/2022, 1:37 PMKotlinLeaner
12/07/2022, 1:37 PMmattinger
12/07/2022, 1:39 PMColumn(modifier=Modifier.fillMaxSize()) {
Column(
modifier = Modifier
.verticalScroll(scrollState)
.weight(1.0f, true)
) {
// put your scrollable content here
}
Column {
// put your buttons in here.
}
}
mattinger
12/07/2022, 1:41 PMmattinger
12/07/2022, 1:43 PMKotlinLeaner
12/07/2022, 1:45 PMweight
modifier what is the use of 2nd parameter true
means?mattinger
12/10/2022, 5:43 PMmattinger
12/10/2022, 5:44 PMweight: Float
The proportional width to give to this element, as related to the total of all weighted siblings. Must be positive.
fill: Boolean = true
When true, the element will occupy the whole width allocated.