ComposeView 未附加到窗口 - Jetpack Compose

问题描述 投票:0回答:1

我尝试将

FlexBox
布局与 JetpackCompose 结合使用。我假设我可以使用普通
RecyclerView
并使用
ComposeView
来渲染我的视图。

因此,我从文档开始,并尝试了 Compose 文档中给出的所有步骤,以将其与 RecyclerView 一起使用。

但是一旦我这样做了,我就收到以下错误:

java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{83fefc1 V.E...... ......I. 0,0-0,0} is not attached to a window

[1] 适配器代码

class SelectAisleAdapter : BaseRecyclerViewAdapter<Any, SelectAisleAdapter.SelectAisleViewHolder>() {

    override fun getViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): SelectAisleViewHolder {
        return SelectAisleViewHolder(ComposeView(parent.context))
    }

    override fun onViewRecycled(holder: BaseViewHolder<Any>) {
        super.onViewRecycled(holder)
        if (holder is SelectAisleViewHolder) {
            holder.composeView.disposeComposition()
        }
    }

    inner class SelectAisleViewHolder(val composeView: ComposeView) : BaseViewHolder<Any>(composeView) {

        init {
            composeView.setViewCompositionStrategy(
                ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
            )
        }

        override fun bind(item: Any) {
            composeView.setContent {
                AisleChip(text = "Milk", /*modifier = Modifier.padding(8.dp),*/ selected = false)
            }
        }
    }
}

[2] 编写代码

@Composable
fun AisleChip(
    modifier: Modifier = Modifier,
    text: String,
    selected: Boolean = false,
) {
    val backgroundColor = if (selected) colorResource(id = R.color.colorPrimary) else colorResource(id = R.color.colorBackground)
    val borderColor = if (selected) colorResource(id = R.color.colorPrimary) else colorResource(id = R.color.colorBorder)
    val textColor = if (selected) colorResource(id = R.color.white) else colorResource(id = R.color.colorBorder)

    Text(
        text = text,
        fontSize = 18.sp,
        fontWeight = FontWeight.Regular,
        fontFamily = appFontFamily,
        color = textColor,
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .border(
                width = 1.dp,
                color = borderColor,
                shape = RoundedCornerShape(8.dp)
            )
            .background(backgroundColor)
            .padding(16.dp)
    )
}

[3]活动代码

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rv = findViewById<RecyclerView>(R.id.rvMain)
        val adapter = SelectAisleAdapter()
        rv.layoutManager = FlexboxLayoutManager(this)
        rv.adapter = adapter
        adapter.replaceItems(listOf("1", "2", "3", "4", "5"))
    }

}

完整的堆栈跟踪

    java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{83fefc1 V.E...... ......I. 0,0-0,0} is not attached to a window
        at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:225)
        at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:220)
        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:227)
        at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:264)
        at android.view.View.measure(View.java:24530)
        at com.google.android.flexbox.FlexboxHelper.calculateFlexLines(FlexboxHelper.java:477)
        at com.google.android.flexbox.FlexboxHelper.calculateHorizontalFlexLines(FlexboxHelper.java:249)
        at com.google.android.flexbox.FlexboxLayoutManager.updateFlexLines(FlexboxLayoutManager.java:960)
        at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:737)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:725)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2021-09-28 16:14:51.363 21103-21103/com.alphastack.shoppy I/Process: Sending signal. PID: 21103 SIG: 9

android-recyclerview android-jetpack-compose
1个回答
0
投票

您因这一行而遇到该错误:

rv.layoutManager = FlexboxLayoutManager(this)

这是一个已知问题,可能是由于

FlexboxLayoutManager
实现
onMeasure
的方式造成的。

尝试不同的 LayoutManager 并查看它是否有效。”

© www.soinside.com 2019 - 2024. All rights reserved.