fix: 列表滑动

This commit is contained in:
HuanCheng65 2023-07-22 08:16:02 +08:00
parent 063806e6a6
commit b2edec39eb
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
1 changed files with 10 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -49,11 +50,12 @@ fun LoadMoreLayout(
) { ) {
val loadDistance = with(LocalDensity.current) { LoadDistance.toPx() } val loadDistance = with(LocalDensity.current) { LoadDistance.toPx() }
val canLoadMore = remember(enableLoadMore, loadEnd) { enableLoadMore && !loadEnd }
val curIsLoading by rememberUpdatedState(newValue = isLoading)
val curCanLoadMore by rememberUpdatedState(newValue = canLoadMore)
var waitingStateReset by remember { mutableStateOf(false) } var waitingStateReset by remember { mutableStateOf(false) }
val curIsLoading by rememberUpdatedState(newValue = isLoading)
val canLoadMore = remember(enableLoadMore, loadEnd) { enableLoadMore && !loadEnd }
val curCanLoadMore by rememberUpdatedState(newValue = canLoadMore)
val swipeableState = rememberSwipeableState(false) { newValue -> val swipeableState = rememberSwipeableState(false) { newValue ->
if (newValue && !curIsLoading && curCanLoadMore) { if (newValue && !curIsLoading && curCanLoadMore) {
onLoadMore() onLoadMore()
@ -61,14 +63,14 @@ fun LoadMoreLayout(
true true
} else !newValue } else !newValue
} }
val stateOffset by swipeableState.offset
LaunchedEffect(stateOffset) { val isStateReset by remember { derivedStateOf { abs(swipeableState.offset.value - loadDistance) < 1f } }
if (waitingStateReset && abs(stateOffset - loadDistance) < 1f) { LaunchedEffect(waitingStateReset, isStateReset) {
if (waitingStateReset && isStateReset) {
waitingStateReset = false waitingStateReset = false
} }
} }
Box( Box(
modifier = Modifier modifier = Modifier
.nestedScroll(swipeableState.LoadPreUpPostDownNestedScrollConnection) .nestedScroll(swipeableState.LoadPreUpPostDownNestedScrollConnection)
@ -80,7 +82,7 @@ fun LoadMoreLayout(
), ),
thresholds = { _, _ -> FractionalThreshold(0.5f) }, thresholds = { _, _ -> FractionalThreshold(0.5f) },
orientation = Orientation.Vertical, orientation = Orientation.Vertical,
enabled = enableLoadMore && !swipeableState.isAnimationRunning && !waitingStateReset, enabled = enableLoadMore && !waitingStateReset,
) )
.fillMaxSize() .fillMaxSize()
) { ) {