【问题标题】:Kotlin/Android - Get the id of generated button inside onBindViewHolder ItemKotlin/Android - 获取 onBindViewHolder 项内生成按钮的 id
【发布时间】:2018-12-31 16:28:58
【问题描述】:

我得到了一个包含多个项目的recyclerView (ViewHolders)。在其中一个(ViewHolderItemTratamentos)中,我得到了以下元素:

当点击第一个“添加button”时,通过inflator 布局,相同的元素(editTextbutton)会在之前的元素下方创建。就像这样:

我的adapter 中的代码(它得到了设置点击的逻辑)创建新行:

holder.add_field_button.setOnClickListener {
    holder.parent_linear_layout.apply {
        val inflater = LayoutInflater.from(context)
        val rowView = inflater.inflate(R.layout.used_products_field, this, false)
        holder.parent_linear_layout.addView(rowView, holder.parent_linear_layout.childCount!! - 0)
        holder.add_field_button.text = "-"
     }
  }

所以,问题是我无法从layout.used_products_fieldbutton 中获取id 来生成另一个新行。我应该夸大这个布局吗(它确实有意义吗?)?我应该给每个button(静态的和生成的)同样的id吗?

R.layout.used_products_field 的内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
    android:id="@+id/number_edit_text"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="5"
    android:inputType="phone"/>

<Button
    android:id="@+id/add_field_button"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    style="@style/botaoCard"
    android:textSize="24dp"
    android:text="+"
    android:padding="5dp"/>

【问题讨论】:

  • 我想如果你的按钮总是在那里 visibility="GONE" 那么你就不会遇到这些问题
  • @epicPandaForce 也许你不明白这一点。我的问题是我无法获取第二个按钮的 id,它位于与第一个不同的布局内。
  • @PedroRelvas 你有另一个问题没有接受答案,即使你在这个新问题中明确使用它,我的意思是val inflater = LayoutInflater.from(context) 部分,请参阅stackoverflow.com/questions/53981684/…
  • 像往常一样在R.layout.used_products_field中指定id,稍后点击监听器调用holder.parent_linear_layout.findViewById(R.id.button)
  • @OmarMainegra 你好!对不起,你是对的!我不是故意的!感谢您提醒我注意。

标签: android kotlin android-recyclerview adapter android-viewholder


【解决方案1】:

您可以获取膨胀布局内的视图 (used_products_field)

val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.used_products_field, this, false)
val button = rowView.findViewById<Button>(R.id.add_field_button)
val edittext = rowView.findViewById<EditText>(R.id.number_edit_text)

【讨论】:

  • 是的,但仅适用于第一个按钮。如果我有 16 个按钮,那么其他 15 个将无法工作......
【解决方案2】:

您应该在将按钮添加到父 ViewGroup 后获取该按钮,在您的情况下是行单击侦听器块。

holder.add_field_button.setOnClickListener {
    holder.parent_linear_layout.apply {
        val inflater = LayoutInflater.from(context)
        val rowView = inflater.inflate(R.layout.used_products_field, this, false)
        addView(rowView, childCount!! - 0) // addView and childCount are available in the scope
        holder.add_field_button.text = "-"

        val button = findViewById<Button>(R.id.add_field_button) // findViewById is available in the scope
    }
}

【讨论】:

  • 嘿@OmarMainegra。问题是我可以通过这种方式获取 id,但是下一个生成的按钮没有 setOnClickListener 的操作。你知道为什么吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2021-04-12
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多