0

我尝试在最近展示的AdapterList组件中组织使用动态数据的工作。作为测试,我将数据列表置于父组件的状态,并添加了一个按钮,每次单击都会将初始列表的长度减少一个。

val (data, setData) = state { testFilms }

Column {
    Button(onClick = {
        setData(testFilms.dropLast(1))
    }) {
        Text("Change")
    }

    AdapterList(
        data,
        modifier = LayoutPadding(5.dp) + LayoutHeight.Fill + LayoutWidth.Fill
    ) { film ->
        FilmItemView(
            film = film,
            selectFilmAction = selectFilmAction,
            logger = logger
        )
    }
}

但是,在重新组合时,我得到了以下错误,而不是预期的结果AdapterList

java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.get(ArrayList.java:437)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3$1.invoke(AdapterList.kt:448)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3$1.invoke(Unknown Source:0)
    at androidx.compose.ObserveKt.Observe(Observe.kt:37)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3.invoke(Unknown Source:11)
    at androidx.ui.foundation.ListState$composeChildForDataIndex$3.invoke(Unknown Source:0)
    at androidx.compose.Recomposer$recompose$1.invoke(Recomposer.kt:61)
    at androidx.compose.Recomposer$recompose$1.invoke(Recomposer.kt:19)
    at androidx.compose.ViewComposerKt.runWithCurrent(ViewComposer.kt:387)
    at androidx.compose.Recomposer.recompose(Recomposer.kt:51)
    at androidx.compose.Recomposer.access$recompose(Recomposer.kt:19)
    at androidx.compose.Recomposer$Companion.recompose$compose_runtime_release(Recomposer.kt:42)
    at androidx.compose.Composition.compose(Composition.kt:67)
    at androidx.compose.Composition.compose(Composition.kt:59)
    at androidx.compose.Compose$subcomposeInto$1.invoke(Compose.kt:253)
    at androidx.compose.Compose$subcomposeInto$1.invoke(Compose.kt:23)
    at androidx.compose.ViewComposerKt.runWithComposing(ViewComposer.kt:397)
    at androidx.compose.Compose.subcomposeInto(Compose.kt:252)
    at androidx.ui.foundation.ListState.composeChildForDataIndex-N7Qnm20(AdapterList.kt:447)
    at androidx.ui.foundation.ListState.recomposeAllChildren(AdapterList.kt:358)
    at androidx.ui.foundation.ListState.recomposeIfAttached(AdapterList.kt:352)
    at androidx.ui.foundation.AdapterListKt.AdapterList(AdapterList.kt:486)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2$invoke$1.invoke(FilmListView.kt:174)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2$invoke$1.invoke(Unknown Source:0)
    at androidx.compose.ObserveKt.Observe(Observe.kt:37)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2.invoke(Unknown Source:20)
    at com.gitlab.andrewkuryan.lab1.view.FilmListViewKt$FilmList$1$2.invoke(Unknown Source:3)

有谁知道使用此组件处理动态数据的任何方法?

4

2 回答 2

0

如果你是最新的dev06 Jetpack Compose,你有两种方法可以做到:

  • List<Any>必须具备“撰写意识”,而当前可用的是ModelList<Any>
  • @Model您可以使用标签创建模型,因为它们会更改状态,并且您的@Compososable函数将知道(使用 a MutableList

示例代码:

fun addLogic(modelList: ModelList<MyModel>) {
  modelList.add(MyModel("Smith John", 10))
}

class MyModel(var name: String, var index: Int)

@Composable
fun RecycledList() { // Any name you want
  val modelList<MyModel> = modelListOf()
  var counter = 0

  addLogic(modelList)

  modelList.add(MyModel("John Doe", 99))

  MaterialTheme {
    Column {
      Container(height = 70.dp) {
         Align(alignment = Alignment.Center) {
            Button(onClick = { 
               modelList.add(MyModel("John Smith", counter++))
               // Any other logic you want
            }) {
               Text("ADD ITEM")
            }
         }
      }
      AdapterList(data = modelList) { item ->
         Center {
            Text("Hello ${item.name} - Index: ${item.index}")
         }
      },
    }
  }
}

结果:

                                            适配器列表

在测试AdapterList时,我注意到IndexOutOfBoundsException删除值但添加时没有问题。当我收到回复时,我会更新我的答案。

于 2020-03-11T16:10:13.083 回答
0

这是一个错误(问题 153195921 ),它将在此补丁的即将发布的版本中修复。

快速浏览一下,您的代码应该可以与该补丁一起使用,尽管我无法从示例中对其进行测试。不过,它与补丁中的测试用例非常相似。

于 2020-04-08T12:06:21.183 回答