通过代码动态创建UI时RecyclerView条目无法显示的问题
RecyclerView条目无法显示的问题修复
因需求原因,我希望完全通过代码而非XML动态创建UI,但当前遇到RecyclerView条目无法显示的问题——无论提供多少数据条目,都无法在屏幕上呈现。我已使用ConstraintLayout添加视图并设置固定高度,且借助AndroidView在Jetpack Compose中集成该RecyclerView,暂认为此集成方式并非问题诱因。
问题根源
ChatCell实现错误:
ChatCell继承自View(非ViewGroup),无法容纳子View,但构造函数中创建的ConstraintLayout未被关联到自身,导致item内部无实际内容。- 未给
ChatCell设置有效布局参数,RecyclerView无法正确测量item宽高,最终导致条目不可见。
冗余视图层级干扰:
createRecyclerView中创建的parent是普通View,属于多余层级,且错误地将RecyclerView约束到该View上,干扰了RecyclerView的正常布局逻辑。
修复后的代码
1. 修正ChatCell实现
将ChatCell改为继承ConstraintLayout,使其能正确容纳子View并设置布局:
class ChatCell(context: Context) : ConstraintLayout(context) { private val textView: TextView init { println("Created") id = View.generateViewId() textView = TextView(context).apply { id = View.generateViewId() text = "test" setBackgroundColor(context.getColor(R.color.red)) layoutParams = LayoutParams(MATCH_PARENT, 100) } addView(textView) val constraintSet = ConstraintSet() constraintSet.clone(this) constraintSet.constrainHeight(textView.id, 100) constraintSet.connect(textView.id, ConstraintSet.LEFT, this.id, ConstraintSet.LEFT) constraintSet.connect(textView.id, ConstraintSet.TOP, this.id, ConstraintSet.TOP) constraintSet.connect(textView.id, ConstraintSet.RIGHT, this.id, ConstraintSet.RIGHT) constraintSet.applyTo(this) setBackgroundColor(context.getColor(R.color.red)) } fun x() {} }
2. 简化RecyclerView创建逻辑
移除冗余的parent View,直接将RecyclerView绑定到根ConstraintLayout:
fun createRecyclerView(context: Context): ConstraintLayout { val constraintLayout = createConstraintLayout(context) val recyclerView = RecyclerView(context).apply { id = View.generateViewId() layoutParams = ConstraintLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT) setBackgroundColor(context.getColor(R.color.purple_500)) val shapeDrawable = MaterialShapeDrawable() shapeDrawable.fillColor = ColorStateList.valueOf(context.getColor(R.color.teal_200)) shapeDrawable.setStroke(5f, context.getColor(R.color.red)) background = shapeDrawable adapter = ChatAdapter() layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false) } constraintLayout.addView(recyclerView) val constraintSet = ConstraintSet() constraintSet.clone(constraintLayout) constraintSet.connect(recyclerView.id, ConstraintSet.LEFT, constraintLayout.id, ConstraintSet.LEFT) constraintSet.connect(recyclerView.id, ConstraintSet.TOP, constraintLayout.id, ConstraintSet.TOP) constraintSet.connect(recyclerView.id, ConstraintSet.RIGHT, constraintLayout.id, ConstraintSet.RIGHT) constraintSet.connect(recyclerView.id, ConstraintSet.BOTTOM, constraintLayout.id, ConstraintSet.BOTTOM) constraintSet.applyTo(constraintLayout) return constraintLayout } fun createConstraintLayout(context: Context): ConstraintLayout { return ConstraintLayout(context).apply { id = View.generateViewId() layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) } }
3. 其他辅助函数调整(可选)
保留简化视图创建的工具函数:
fun <T: View> applyDefaults(parent: ViewGroup, view: T, width: Int, height: Int): T { view.id = View.generateViewId() view.layoutParams = ConstraintLayout.LayoutParams(width, height) parent.addView(view) return view }
内容的提问来源于stack exchange,提问作者J. Doe
相关产品推荐
相关产品推荐

