Lazyrow因为’IndexOutOfBoundsException’在Android Compose中崩溃。

huangapple go评论64阅读模式
英文:

Lazyrow crashes because of 'IndexOutOfBoundsException' android compsoe

问题

I used a lazycolumn to display a list of photos. Photos are displayed with their paths. Problem occurred when I tried to delete an image. I have a button on each image, and when clicked, its path is removed from the photoList. But when I click the delete button, the photoList is modified, but the UI didn't change. And if I scroll the row at this time, the app will crash and show an error message as 'IndexOutOfBoundsException.' I think that was due to the row not being recomposed. What is the reason? How do I fix?

Composable which calls photoList

val photoList: MutableList

onDeleteClick = { path ->
    photoList.remove(path)
}
Photo row composable
LazyRow(
    modifier = Modifier
        .padding(vertical = 16.dp)
        .wrapContentHeight()
        .fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    state = rememberLazyListState(),
    contentPadding = PaddingValues(horizontal = 16.dp)
) {
    items(photoList) { path ->
        ImageCard(path) {
            onDeleteClick(path)
        }
    }
}
ImageCard
IconButton(
    onClick = { onDeleteClick(path) },
    modifier = Modifier.align(Alignment.TopEnd)
) {
    Icon(
        imageVector = Icons.Default.Delete,
        contentDescription = "Delete",
        tint = Color.White
    )
}
英文:

I used a lazycolumn to dispaly a list of photos. Photos are displayed with its path. Problem occured when I try to delete an image. I have a button on each image and when click it, its path is removed from the photoList. But when I click the delete button, the photoList is modiifed but the UI didn't change. And if I scroll the row at this time, the app will crash and show error message as 'IndexOutOfBoundsException'. I think that was due to the row is not recomposed. What is the reason. How do I fix?

Composable which call photoList

val photoList MutableList

onDeleteClick = { path ->
                photoList.remove(path)

            }
Photo row compsoable
LazyRow(
    modifier = Modifier
        .padding(vertical = 16.dp)
        .wrapContentHeight()
        .fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    state = rememberLazyListState(),
    contentPadding = PaddingValues(horizontal = 16.dp)
    ) {
        items(photoList) { path ->
        ImageCard(path) {
        onDeleteClick(path)
    }

ImageCard
IconButton(
            onClick = { onDeleteClick(path) },
            modifier = Modifier.align(Alignment.TopEnd)
        ) {
            Icon(
                imageVector = Icons.Default.Delete,
                contentDescription = "Delete",
                tint = Color.White
            )
        }


答案1

得分: 2

The problem is probably that your photoList is not wrapped in a state which would trigger Compose recomposition on change Lazyrow因为’IndexOutOfBoundsException’在Android Compose中崩溃。

Below is a sample code where I wrapped the photoList in a remember construct. It is not exactly the same code as you have not really provided the whole code, so I had to improvise but hopefully, you will get the point Lazyrow因为’IndexOutOfBoundsException’在Android Compose中崩溃。

val photoList = remember {
    // Wrap your photo list data in a state so it can trigger recomposition
    mutableStateOf(List(20) { index -> "Index $index" })
}

LazyRow(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    contentPadding = PaddingValues(horizontal = 16.dp)
) {
    items(photoList.value) { text ->
        Text(
            text = text,
            modifier = Modifier
                .clickable {
                    // Modify the state value, this will trigger recomposition
                    photoList.value = photoList.value.minus(text)
                }
        )
    }
}
英文:

The problem is probably that your photoList is not wrapped in a state which would trigger Compose recomponsition on change Lazyrow因为’IndexOutOfBoundsException’在Android Compose中崩溃。

Below is a sample code where I wrapped the photoList in a remember construct. It is not exactly the same code as you have not really provided the whole code, so I had to improvise but hopefully, you will get the point Lazyrow因为’IndexOutOfBoundsException’在Android Compose中崩溃。

val photoList = remember {
    // Wrap your photo list data in a state so it can trigger recomposition
    mutableStateOf(List(20) { index -> "Index $index" })
}

LazyRow(
    modifier = Modifier.fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    contentPadding = PaddingValues(horizontal = 16.dp)
) {
    items(photoList.value) { text ->
        Text(
            text = text,
            modifier = Modifier
                .clickable {
                    // Modify the state value, this will trigger recomposition
                    photoList.value = photoList.value.minus(text)
                }
        )
    }
}

答案2

得分: 2

LazyColumn/LazyRow中,您必须根据唯一键在项目中添加唯一键,只有基于唯一键的项目才会得到更新。

LazyRow(
    modifier = Modifier
        .padding(vertical = 16.dp)
        .wrapContentHeight()
        .fillMaxWidth(),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    state = rememberLazyListState(),
    contentPadding = PaddingValues(horizontal = 16.dp)
) {
    items(photoList, key = { it.yourUniqueId }) { path ->
        ImageCard(path) {
            onDeleteClick(path)
        }
    }
}
英文:

In LazyColumn/LazyRow you have to add Unique key in items, based on the unique key only particular item got update.

LazyRow(
modifier = Modifier
    .padding(vertical = 16.dp)
    .wrapContentHeight()
    .fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(16.dp),
state = rememberLazyListState(),
contentPadding = PaddingValues(horizontal = 16.dp)
) {
    items(photoList,key = { it -> it.yourUniqueId ) { path ->
    ImageCard(path) {
    onDeleteClick(path)
}

huangapple
  • 本文由 发表于 2023年5月15日 08:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定