来自布局的访问按钮以编程方式在另一个布局中膨胀

时间:2017-12-06 14:25:58

标签: android android-layout kotlin kotlin-android-extensions

我正在将一个布局列表programmatically从xml扩展到另一个布局。

最终结果如下图所示。 Image

checkboxes 的数量在每次运行时都有所不同,我如何获取其状态并将侦听器添加到图像按钮?理想情况下,我只想要一个具有所选布局ID的侦听器。

这是xml代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/chk_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
    android:id="@+id/checkBox_chkitem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView_chkitem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="24dp"
    android:text="TextView"
    app:layout_constraintEnd_toStartOf="@+id/imageButton_chkitem"
    app:layout_constraintStart_toEndOf="@+id/checkBox_chkitem"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/imageButton_chkitem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/stat_notify_more" />
</android.support.constraint.ConstraintLayout>

这就是我如何夸大布局

        val checkitemlist = listOf<ChecklistItem>(ChecklistItem("Categoria", "conteudo"),
            ChecklistItem("Categoria", "conteudo"),
            ChecklistItem("Categoria", "conteudo"),
            ChecklistItem("Categoria", "conteudo"))
    for (item in checkitemlist) {
        val inflater = LayoutInflater.from(context)
        val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout
        layout.textView_chkitem.text = item.textSimple
        checklist_content_layout.addView(layout)
    }

1 个答案:

答案 0 :(得分:0)

替换你的代码

   for (item in checkitemlist) {
            checklist_content_layout.removeAllViews()
            val inflater = LayoutInflater.from(context)
            val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout
            layout.textView_chkitem.text = item.textSimple
            checklist_content_layout.addView(layout)
        }
相关问题