Fixed
Status Update
Comments
ra...@google.com <ra...@google.com>
ap...@google.com <ap...@google.com> #2
Hey thank you for filing this issue. Could you provide a sample code that we can use to verify the described behavior?
Thank you!
ap...@google.com <ap...@google.com> #3
in this code i use NestedScrollConnection on LazyColumn and when offset change the given velocity in onPreFling method is incorrect.
for example i move my finger up on screen and you can see in following logcat velocity in y axis is positive
TAG: velocity:(0.0, 388.90985) px/sec offset:780.03516
TAG: velocity:(0.0, 743.0752) px/sec offset:667.8938
TAG: velocity:(0.0, 292.08224) px/sec offset:583.4952
TAG: velocity:(0.0, 993.74994) px/sec offset:471.7256
TAG: velocity:(0.0, 638.26825) px/sec offset:404.34738
TAG: velocity:(0.0, 482.3507) px/sec offset:329.2629
TAG: velocity:(0.0, 233.61378) px/sec offset:247.59741
TAG: velocity:(0.0, 758.8499) px/sec offset:158.94847
TAG: velocity:(0.0, 679.20667) px/sec offset:87.78984
TAG: velocity:(0.0, 725.84314) px/sec offset:2.3688889
and when offset reach to zero ,
i don't decrease offset. so
layout is fixed,but now when i move my finger up the velocity in y axis is negative
TAG: velocity:(0.0, -929.37604) px/sec offset:0.0
TAG: velocity:(0.0, -2.2106879) px/sec offset:0.0
TAG: velocity:(0.0, -472.8846) px/sec offset:0.0
TAG: velocity:(0.0, -1433.1675) px/sec offset:0.0
TAG: velocity:(0.0, -1240.49) px/sec offset:0.0
const val max = 900f
@Composable
fun NestedScrollTest() {
var offset by remember {
mutableStateOf(max)
}
fun scrollBy(ds: Float): Float {
if (offset == 0f && ds < 0f) return 0.0f
if (offset == max && ds > 0f) return 0.0f
val pv = offset
offset = (ds + offset).coerceIn(0f, max)
return offset - pv
}
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
return if (available.y < 0) {
val consumed = scrollBy(available.y)
Offset(x = 0f, y = consumed)
} else Offset.Zero
}
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset {
return if (abs(available.y) > 0f &&
available.y > 0f
) {
Offset(0f, scrollBy(available.y))
} else
super.onPostScroll(consumed, available, source)
}
override suspend fun onPreFling(available: Velocity): Velocity {
Log.i(TAG, "velocity:$available offset:$offset")
return super.onPreFling(available)
}
}
}
Column(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(80.dp)
)
LazyColumn(
modifier = Modifier
.graphicsLayer {
translationY = offset
}
.nestedScroll(connection = nestedScrollConnection)
.fillMaxWidth()
.weight(1f)
) {
items(100) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(60.dp)
.background(Color.Blue)
)
Spacer(modifier = Modifier.height(8.dp))
}
}
}
}
for example i move my finger up on screen and you can see in following logcat velocity in y axis is positive
TAG: velocity:(0.0, 388.90985) px/sec offset:780.03516
TAG: velocity:(0.0, 743.0752) px/sec offset:667.8938
TAG: velocity:(0.0, 292.08224) px/sec offset:583.4952
TAG: velocity:(0.0, 993.74994) px/sec offset:471.7256
TAG: velocity:(0.0, 638.26825) px/sec offset:404.34738
TAG: velocity:(0.0, 482.3507) px/sec offset:329.2629
TAG: velocity:(0.0, 233.61378) px/sec offset:247.59741
TAG: velocity:(0.0, 758.8499) px/sec offset:158.94847
TAG: velocity:(0.0, 679.20667) px/sec offset:87.78984
TAG: velocity:(0.0, 725.84314) px/sec offset:2.3688889
and when offset reach to zero ,
i don't decrease offset. so
layout is fixed,but now when i move my finger up the velocity in y axis is negative
TAG: velocity:(0.0, -929.37604) px/sec offset:0.0
TAG: velocity:(0.0, -2.2106879) px/sec offset:0.0
TAG: velocity:(0.0, -472.8846) px/sec offset:0.0
TAG: velocity:(0.0, -1433.1675) px/sec offset:0.0
TAG: velocity:(0.0, -1240.49) px/sec offset:0.0
const val max = 900f
@Composable
fun NestedScrollTest() {
var offset by remember {
mutableStateOf(max)
}
fun scrollBy(ds: Float): Float {
if (offset == 0f && ds < 0f) return 0.0f
if (offset == max && ds > 0f) return 0.0f
val pv = offset
offset = (ds + offset).coerceIn(0f, max)
return offset - pv
}
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
return if (available.y < 0) {
val consumed = scrollBy(available.y)
Offset(x = 0f, y = consumed)
} else Offset.Zero
}
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset {
return if (abs(available.y) > 0f &&
available.y > 0f
) {
Offset(0f, scrollBy(available.y))
} else
super.onPostScroll(consumed, available, source)
}
override suspend fun onPreFling(available: Velocity): Velocity {
Log.i(TAG, "velocity:$available offset:$offset")
return super.onPreFling(available)
}
}
}
Column(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(80.dp)
)
LazyColumn(
modifier = Modifier
.graphicsLayer {
translationY = offset
}
.nestedScroll(connection = nestedScrollConnection)
.fillMaxWidth()
.weight(1f)
) {
items(100) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(60.dp)
.background(Color.Blue)
)
Spacer(modifier = Modifier.height(8.dp))
}
}
}
}
Description