https://kotlinlang.org logo
#compose
Title
# compose
b

Billy Newman

06/23/2022, 3:06 PM
Hello all, wondering if someone can help me understand LayColumn’s contentPadding and its relation to ListState.layoutInfo.viewportStartOffset. Example code in reply
I am setting up a LazyColumn with some top padding.
Copy code
LazyColumn(
   state = listState,
   contentPadding = PaddingValues(top = 16.dp),
   verticalArrangement = Arrangement.spacedBy(8.dp),
) {
I am also calculating when the top of a list item is in view:
Copy code
val offset = firstItem.offset - state.layoutInfo.viewportStartOffset
offset >= 0 // item is in view
I noticed when logging the viewportStartOffset that it didn’t match the padding value. In this case my padding value is 16, viewportStartOffset is 44. Is this potentially density dependent vs not values. Whats throwing me is that I am setting a starting index for my list state, however with content padding that item is “off” by 44. Initial code was setting up listState as:
Copy code
val listState = rememberLazyListState(index, 16)
If this is indeed a screen density issue, should I be calculating the correct value for 16.dp to give to the list state based on my screens density? Or is there something else I am missing?
Confirmed, this works:
Copy code
val contentPadding = PaddingValues(top = 16.dp)
val listState = rememberLazyListState(index, with(LocalDensity.current) { contentPadding.calculateTopPadding().roundToPx() })
I guess I wasn’t expecting initialFirstVisibleItemScrollOffset: Int = 0 to be density dependent. Suppose that makes sense, its an Int, not Dp
4 Views