我试图在最近展示的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)
有人知道使用此组件处理动态数据的任何方法吗?
答案 0 :(得分:0)
如果您了解最新的 dev06 Jetpack Compose ,则有两种方法可以做到:
List<Any>
必须“知道”,并且当前可用的是ModelList<Any>
@Model
标签创建模型,因为它们会改变状态,并且@Compososable
函数会知道(使用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
删除了值,但添加时没有问题。收到回复后,我将更新我的答案。
答案 1 :(得分:0)
这是一个错误(issue 153195921),它将由this patch在即将发布的版本中修复。
快速浏览一下,您的代码应该可以使用该补丁,尽管我无法从示例中对其进行测试。不过,它与补丁中的测试用例非常相似。