android-recyclerview - ComposeView 未附加到窗口 - Jetpack Compose
问题描述
我试图在FlexBox
JetpackCompose 中使用布局。我假设我可以使用 normalRecyclerView
和 useComposeView
来渲染我的视图。
所以我从文档开始,并尝试了 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"))
}
}
完成 StackTrace
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
解决方案
推荐阅读
- jenkins - 基于环境变量的 Jenkins 流水线条件阶段
- angular - ngOnDestroy 的 Angular 单元测试
- powershell - 在各种数据库之间分配新邮箱
- java - firestoreoptions.getService 上的 Firestore 空指针异常
- r - 使用 Xptr 和 Function 调用 Rcpp 函数 - 只有 xptr 案例有效
- jquery - 如何使用 jquery 根据子 div 的特定文本对打印结果进行排序?
- python - 逆转录
- c# - 使用 SqlDependency OnChange 采取的路线
- git - 如何在裸仓库中进行 lfs 跟踪?
- qt - 当窗口最大化时,QLayout 中的 QWidget 被移动和剪切