AnchoredDraggableState wird auf „True“ gesetzt, auch wenn der Finger die Wischsteuerung nicht loslässtAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 AnchoredDraggableState wird auf „True“ gesetzt, auch wenn der Finger die Wischsteuerung nicht loslässt

Post by Anonymous »

Ich versuche, die Wischsteuerung in meinem App-Build auf Jet Pack Compose zu implementieren. Ich habe bisher folgenden Code verwendet. Ich verwende Compose Bom

Code: Select all

androidx.compose:compose-bom:2024.09.00
Derzeit springt die Wischsteuerung beim Wischen nach rechts ganz nach rechts, sobald die Schwellenwerte erreicht sind, d. h. wenn die Hälfte des Wischens abgeschlossen ist, wenn der Zustand wahr wird. Ich möchte, dass der Swipe-Status nicht wahr ist, bis der Benutzer die Finger loslässt, selbst wenn Schwellenwerte überschritten werden, z. B. 0,5f.

Code: Select all

'androidx.compose:compose-bom:2024.09.00'

Code: Select all

val maxWidthPx = constraints.maxWidth.toFloat()
val endPx = maxWidthPx - handleSizePx
val swipeState = remember {
AnchoredDraggableState(
initialValue = false
)
}

LaunchedEffect(endPx, swipeState.currentValue) {
swipeState.updateAnchors(
DraggableAnchors {
if (!swipeState.currentValue) {
false at 0f
}
true at endPx
}
)
}

// Trigger action when the state becomes 'true' (Arrived)
LaunchedEffect(swipeState.currentValue) {
if (swipeState.currentValue) {
onArrived()
Toast.makeText(context, "Arrived", Toast.LENGTH_SHORT).show()
}
}

val flingBehavior = AnchoredDraggableDefaults.flingBehavior(
state = swipeState,
positionalThreshold =  { distance -> distance * 0.5f },
animationSpec = spring(
dampingRatio = Spring.DampingRatioLowBouncy,
stiffness = Spring.StiffnessLow
)
)

Box(
modifier = Modifier
.offset {
// Safety check to ensure offset is read only after layout/initialization
val offset = try {
swipeState.requireOffset()
} catch (e: Exception) {
0f
}
IntOffset(offset.roundToInt(), 0)
}
.size(handleSize)
.padding(4.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary)
.anchoredDraggable(
state = swipeState,
orientation = Orientation.Horizontal,
flingBehavior = flingBehavior,
),
contentAlignment = Alignment.Center
) {
// My controls
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post