【问题标题】:Dynamically generating a table with buttons using Kotlin for Android使用 Kotlin for Android 动态生成带有按钮的表格
【发布时间】:2020-08-06 16:56:45
【问题描述】:

最终的目标是成为一系列按钮(当前在一个表格中),它们将隐藏/显示每个之间包含固定数据的行。

我不确定我的方法是可行的还是最优的,我也不喜欢这种方法。我对 Kotlin/Android 几乎没有经验,但这里有一个硬编码示例的示例:

<TableLayout
    android:id="@+id/superTable"
    android:layout_width="match_parent"
    android:layout_height="0sp"
    android:layout_marginTop="0sp"
    android:stretchColumns="1"
    android:visibility="visible"
    app:layout_constraintTop_toBottomOf="@+id/chem2Table">

    <FrameLayout
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/superTableButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16sp"
            android:layout_marginLeft="16sp"
            android:layout_marginTop="0sp"
            android:onClick="superChemical1"
            android:text="@string/button_chem3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/chem2Table" />
    </FrameLayout>

    <TableRow
        android:id="@+id/superRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">

        <TextView
            android:id="@+id/sr1c1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#b0b0b0"
            android:padding="5sp"
            android:text="@string/chem3c1"
            android:textColor="#000"
            android:textSize="12sp" />

</TableLayout>

还有我的隐藏/显示方法:

fun superChemical1(view: View) {
    val chemId = R.id.superRow1
    val chemVisibility = findViewById<TableRow>(chemId)

    if(chemVisibility.visibility == View.VISIBLE) {
        chemVisibility.visibility = View.GONE
    } else {
        chemVisibility.visibility = View.VISIBLE
    }
}

目标是重现相同的功能,整个表是从外部源动态生成的。它将使用动态的 onClick 方法,该方法可以接收单击按钮的 ID,然后隐藏/显示包含关联数据的下一行。

我不确定如何完成的主要事情是将每个生成的按钮与以下数据行相关联。

【问题讨论】:

    标签: android kotlin android-tablelayout


    【解决方案1】:

    最后,我决定使用完全适合我的目的的 RecyclerView。 TableLayout 最终变得笨重且难以格式化,因此我放弃了该项目并使用 RecyclerView 重新启动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多