Android Compose AdapterList异步更新数据

Android Compose AdapterList update data Asynchronously

您好,我正在尝试异步更新 AdapterList 可组合项。我把图像作为列表项之一。图像数据使用协程从服务器下载并使用状态更新值。当我抛出列表项时,出现以下错误

    java.lang.IllegalStateException: Asking for measurement result of unmeasured layout modifier
        at androidx.ui.core.LayoutNodeWrapper.getMeasureResult(LayoutNodeWrapper.kt:58)
        at androidx.ui.core.LayoutNodeWrapper.getMeasuredSize(LayoutNodeWrapper.kt:48)
        at androidx.ui.core.Placeable.getWidth(Placeable.kt:40)
        at androidx.ui.core.LayoutNode.getWidth(ComponentNodes.kt:841)
        at androidx.ui.foundation.ListState.composeAndMeasureNextItem-BTEqjtU(AdapterList.kt:222)
        at androidx.ui.foundation.ListState.consumePendingScroll(AdapterList.kt:151)
        at androidx.ui.foundation.ListState.access$consumePendingScroll(Unknown Source:0)
        at androidx.ui.foundation.ListState$ListMeasureBlocks.measure(AdapterList.kt:277)
        at androidx.ui.core.InnerPlaceable.performMeasure(InnerPlaceable.kt:43)
        at androidx.ui.core.LayoutNodeWrapper.measure(LayoutNodeWrapper.kt:99)
        at androidx.ui.core.DelegatingLayoutNodeWrapper.performMeasure(DelegatingLayoutNodeWrapper.kt:79)
        at androidx.ui.core.LayerWrapper.performMeasure(LayerWrapper.kt:52)
        at androidx.ui.core.LayoutNodeWrapper.measure(LayoutNodeWrapper.kt:99)
        at androidx.ui.core.DelegatingLayoutNodeWrapper.performMeasure(DelegatingLayoutNodeWrapper.kt:79)
        at androidx.ui.core.LayoutNodeWrapper.measure(LayoutNodeWrapper.kt:99)
        at androidx.ui.core.DelegatingLayoutNodeWrapper.performMeasure(DelegatingLayoutNodeWrapper.kt:79)
        at androidx.ui.core.LayoutNodeWrapper.measure(LayoutNodeWrapper.kt:99)
        at androidx.ui.core.DelegatingLayoutNodeWrapper.performMeasure(DelegatingLayoutNodeWrapper.kt:79)
        at androidx.ui.core.LayoutNodeWrapper.measure(LayoutNodeWrapper.kt:99)
        at androidx.ui.core.LayoutNode$measure.invoke(ComponentNodes.kt:1177)
        at androidx.ui.core.LayoutNode$measure.invoke(Unknown Source:0)
        at androidx.ui.core.ModelObserver.observeReads(ModelObserver.kt:151)
        at androidx.ui.core.AndroidComposeView.observeMeasureModelReads(AndroidOwner.kt:487)
        at androidx.ui.core.LayoutNode.measure(ComponentNodes.kt:1176)

我自己已经看到过几次,既有像您描述的那样下载图像的列表,也有没有完成任何异步工作的列表,但我认为这不是由我们专门做的任何事情引起的。我的印象是,这只是 Compose 当前状态的一个错误。

话虽如此,AndroidComposeViewAccessibilityDelegateCompat 至少有一个 class 处理此错误并引用内部问题跟踪器票证,该票据表明它将至少在 Android R 中修复对于那个例子。

} catch (e: IllegalStateException) {
    // We may get "Asking for measurement result of unmeasured layout modifier" error.
    // TODO(b/153198816): check whether we still get this exception when R is in.
    info.setBoundsInScreen(android.graphics.Rect())
}

dev11 也即将发生变化,更新 AdapterList to dispose of compositions scrolled off screen,我很想知道这会产生怎样的影响。

如果您只是想知道错误发生在何处,您可以查看 LayoutNodeWrapper._measureResult

此问题已在 Compose 版本 0.1.0-dev14 中修复

您可以将 compose 依赖项更新为

 composeOptions {
    kotlinCompilerExtensionVersion '0.1.0-dev14'
    kotlinCompilerVersion '1.3.70-dev-withExperimentalGoogleExtensions-20200424'
}