I found an article where it describes how to make implementation of parallax view in LazyColumn and there is such a method:
@Composable
fun ImageParallaxScroll() {
val lazyListState = rememberLazyListState()
val list = (0..1_000).map{ "Item $it" }.toList()
val firstItemTranslationY: LazyListState by remember {
derivedStateOf {
when {
lazyListState.layoutInfo.visibleItemsInfo.isNotEmpty() && lazyListState.firstVisibleItemIndex == 0 -> lazyListState.firstVisibleItemScrollOffset * .6f
else -> 0f
}
}
}
...
}
The problem is that the entire block of remember
underline with a red line and such error comes:
Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
Can't understand what is problem here?
Couple things I can see here...
Firstly :
You need to import :
import androidx.compose.runtime.getValue
Or import everything using :
import androidx.compose.runtime.*
This will import the extension operator function on State<T>
which you are missing (from the SnapshotState.kt file).
The IDE seems to have a difficult time auto importing top level extension functions for some reason.
Not sure why its inlined
but thats probably the reason for not just adding it to the State<T>
interface and instead having the loose top level function, requiring the extra import.
Secondly :
I believe the return type by will Float
not LazyListState
.
So the function with imports would be (also remembering the list itself so its not recalculated in recomposition) :
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
@Composable
fun ImageParallaxScroll() {
val lazyListState = rememberLazyListState()
val list = remember { (0..1_000).map{ "Item $it" }.toList() }
val firstItemTranslationY: Float by remember {
derivedStateOf {
when {
lazyListState.layoutInfo.visibleItemsInfo.isNotEmpty() && lazyListState.firstVisibleItemIndex == 0 -> lazyListState.firstVisibleItemScrollOffset * .6f
else -> 0f
}
}
}
...
}