我尝试在最近展示的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)
有谁知道使用此组件处理动态数据的任何方法?