android-jetpack-compose - 不能在同一个 Measurable 上多次调用 measure()
问题描述
我正在创建一个应用程序,它使用 Flickr api 使用 Pager 3 库逐页获取图像,并使用 Jetpack Compose 在屏幕上显示它。一切正常,直到它加载 4-5 页(看起来它是浮动的)。
我收到一个错误:
Process: com.peterstaranchuk.flickrclient, PID: 9843
java.lang.IllegalStateException: measure() may not be called multiple times on the same Measurable
at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:86)
at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release(LayoutNode.kt:1236)
at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release$default(LayoutNode.kt:1232)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.doRemeasure-0kLqBqw(MeasureAndLayoutDelegate.kt:171)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$doRemeasure-0kLqBqw(MeasureAndLayoutDelegate.kt:38)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:207)
at androidx.compose.ui.platform.AndroidComposeView.onMeasure(AndroidComposeView.android.kt:547)
at android.view.View.measure(View.java:17430)
它没有指向我的代码中的某个地方,看起来它与 Jetpack compose 测量有关。
我检查了所有可用信息,但没有找到答案。可能有人有同样的问题?任何线索将不胜感激。
我的视图模型:
class SearchViewModel(
private val interactor: SearchInteractor,
private val photosPresentationMapper: PhotoPresentationMapper,
eventSender: ScreenEventSender<SearchContract.Event>,
dispatchers: DispatchersProvider
) : BaseViewModel<SearchContract.Event>(eventSender, dispatchers) {
fun observePhotos(): Flow<PagingData<PhotoPresentation>> {
return interactor.getPagedPhotos("nature")
.flowOn(dispatchers.io)
.catch { /*todo handle error state */
Log.d("", it.message.toString())
}
.map {
it.map {
photosPresentationMapper.map(it)
}
}
}
}
屏幕可组合:
@Composable
fun ScreenContent() {
val lazyPhotoItems: LazyPagingItems<PhotoPresentation> = vm.observePhotos().collectAsLazyPagingItems()
Column {
Box(
modifier = Modifier
.wrapContentSize(Alignment.Center)
.background(Color(ContextCompat.getColor(LocalContext.current, R.color.black60transparency)))
.padding(8.dp)
) {
Row(
modifier = Modifier
.wrapContentSize()
.fillMaxWidth()
.clip(RoundedCornerShape(50))
.background(colorResource(id = R.color.search_field_background_color)),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
painterResource(id = R.drawable.ic_search_image),
contentDescription = stringResource(R.string.search_screen_magnifier_icon_content_description),
tint = Color.White
)
Text(
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp, start = 4.dp),
text = stringResource(R.string.search_screen_search_field_text),
fontSize = 12.sp,
color = colorResource(id = R.color.search_field_text_color),
fontFamily = getFont(R.font.nunito_sans_extra_bold)
)
}
}
LazyColumn (content = {
items(lazyPhotoItems.itemCount, {
lazyPhotoItems[it]!!.id
}) { index ->
ImageItem(lazyPhotoItems[index]!!)
}
}, modifier = Modifier.fillMaxSize())
}
}
我用:
//paging
implementation "androidx.paging:paging-runtime:$paging_version"
implementation "androidx.paging:paging-compose:$paging_compose_version"
与版本:
compose_version = '1.0.1'
paging_compose_version = "1.0.0-alpha12"
解决方案
我建议你添加.cachedIn(viewModelScope)
看评论// Here
fun observePhotos(): Flow<PagingData<PhotoPresentation>> {
return interactor.getPagedPhotos("nature")
.cachedIn(viewModelScope) // Here
.flowOn(dispatchers.io)
.catch { /*todo handle error state */
Log.d("", it.message.toString())
}
.map {
it.map {
photosPresentationMapper.map(it)
}
}
}
推荐阅读
- amazon-web-services - 适用于 IaC 的 AWS CDK VS 开发工具包
- java - Java - 使用 .class 文件编译
- reactjs - 如何在匹配减速器中的项目时使用新键和值修改减速器对象的值
- java - 如何使用 Jsoup 获取 html 数据的特定子元素
- tensorflow - 如何直接从 GitHub 或本地下载安装联合的 Tensorflow?
- php - 无法使用 ajax、codeignier 获取输出购物车
- java - 在 ojAlgo 中检测导致无界优化问题的成本/变量
- python - 通过 Python 中的 IP 地址和端口进行 TCP 连接
- c++ - 如何在不将数字四舍五入的情况下将字符数组转换为浮点数?
- debugging - 是否有适用于 Cortex M7 的 FreeRTOS howto,关于如何监督/跟踪具有少量任务的系统(要使用内核的哪些功能)