【问题标题】:Inflate and bind view at the same moment in android在android中同时膨胀和绑定视图
【发布时间】:2019-03-05 11:23:58
【问题描述】:

我正在尝试为列表中的每个对象膨胀一个视图,并同时将其与数据绑定,这就是我实现这一点的方式:

data.forEach { rule ->
        layoutInflater.inflate(
            R.layout.rule_view_holder,
            this.rulesContainer
        ).apply {
            setOnClickListener { content.flipVisibility() }
            title.text = rule.title
            contentText.text = rule.content.fromHtml()
            rule.images.map {
                createImageView(this.context, it)
            }.forEach {
                contentImages.addView(it)
            }
        }
    }

视图膨胀并正确插入到 LinearLayout 中,但似乎数据仅绑定到列表中的最后一项,这就是我的意思:image 里面有 6 个项目的列表,所以视图膨胀绝对是正确的(因为水平线,每个视图都必须膨胀)。

还有 rule_view_holder 布局screenshot

你能帮我解决这个问题吗?

【问题讨论】:

    标签: android kotlin layout-inflater


    【解决方案1】:

    您可以在没有父级的情况下膨胀项目 xml(通过 null),然后调用 addView

    data.forEach { rule ->
        val view = layoutInflater.inflate(R.layout.rule_view_holder, null).apply {
            setOnClickListener { content.flipVisibility() }
            title.text = rule.title
            contentText.text = rule.content.fromHtml()
            rule.images.map {
                createImageView(this.context, it)
            }.forEach {
                contentImages.addView(it)
            }
        }
        this.rulesContainer.addView(view)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多