本文介绍了LazyColumn With SwipeToDismisse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Android Compose中使用SwipeToDismiss
和LazyColumn
的正确方式是什么?
我的方法:
LazyColumn(
modifier = Modifier.padding(6.dp),
verticalArrangement = Arrangement.spacedBy(6.dp),
) {
items(items = items) {
TrackedActivityRecord(it.activity, it.record, scaffoldState)
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TrackedActivityRecord(
activity: TrackedActivity,
record: TrackedActivityRecord,
scaffoldState: ScaffoldState,
vm: TimelineVM = viewModel()
){
val dismissState = rememberDismissState()
if (dismissState.value != DismissValue.Default){
LaunchedEffect(subject = activity){
val deleted = scaffoldState.snackbarHostState.showSnackbar("Awesome", "do it")
if (deleted == SnackbarResult.Dismissed){
vm.rep.deleteRecordById(activity.id, record.id)
}
dismissState.snapTo(DismissValue.Default)
}
}
SwipeToDismiss(
state = dismissState,
background = {
Box(Modifier.size(20.dp). background(Color.Red))
},
) {
Record(activity = activity, record = record)
}
}
LazyColumn
重新组合时出现问题删除位置上的项目Dismissed
-不可见。我用dismissState.snapTo(DismissValue.Default)
破解了它。但在一秒钟内,你可以看到旧的物品。如果我没有使用REMERY但DismissState,我会得到:java.lang.IllegalArgumentException: Cannot round NaN value.
由androidx.compose.material.SwipeToDismissKt$SwipeToDismiss$2$1$1$1.invoke-nOcc-ac(SwipeToDismiss.kt:244)
引起推荐答案
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.draw.scale
import androidx.compose.material.DismissValue.*
import androidx.compose.material.DismissDirection.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
// This is an example of a list of dismissible items, similar to what you would see in an
// email app. Swiping left reveals a 'delete' icon and swiping right reveals a 'done' icon.
// The background will start as grey, but once the dismiss threshold is reached, the colour
// will animate to red if you're swiping left or green if you're swiping right. When you let
// go, the item will animate out of the way if you're swiping left (like deleting an email) or
// back to its default position if you're swiping right (like marking an email as read/unread).
@ExperimentalMaterialApi
@Composable
fun MyContent(
items: List<ListItem>,
dismissed: (listItem: ListItem) -> Unit
) {
val context = LocalContext.current
LazyColumn {
items(items, {listItem: ListItem -> listItem.id}) { item ->
val dismissState = rememberDismissState()
if (dismissState.isDismissed(EndToStart)){
dismissed(item)
}
SwipeToDismiss(
state = dismissState,
modifier = Modifier.padding(vertical = 1.dp),
directions = setOf(StartToEnd, EndToStart),
dismissThresholds = { direction ->
FractionalThreshold(if (direction == StartToEnd) 0.25f else 0.5f)
},
background = {
val direction = dismissState.dismissDirection ?: return@SwipeToDismiss
val color by animateColorAsState(
when (dismissState.targetValue) {
Default -> Color.LightGray
DismissedToEnd -> Color.Green
DismissedToStart -> Color.Red
}
)
val alignment = when (direction) {
StartToEnd -> Alignment.CenterStart
EndToStart -> Alignment.CenterEnd
}
val icon = when (direction) {
StartToEnd -> Icons.Default.Done
EndToStart -> Icons.Default.Delete
}
val scale by animateFloatAsState(
if (dismissState.targetValue == Default) 0.75f else 1f
)
Box(
Modifier
.fillMaxSize()
.background(color)
.padding(horizontal = 20.dp),
contentAlignment = alignment
) {
Icon(
icon,
contentDescription = "Localized description",
modifier = Modifier.scale(scale)
)
}
},
dismissContent = {
Card(
elevation = animateDpAsState(
if (dismissState.dismissDirection != null) 4.dp else 0.dp
).value
) {
Text(item.text)
}
}
)
}
}
}
data class ListItem(val id:String, val text:String)
原始版本中的主要问题是,根据物品的位置来记住解除状态。当列表发生变化时(这在删除项目时非常明显),记住的disdissState将应用于下一个项目(当然这是错误的)。要解决此问题,请使用Items(Items,{listItem:MyRoutesViewModel.ListItem->;listItem.id}),而不仅仅是Items(Items)
这篇关于LazyColumn With SwipeToDismisse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!