Kotlin / Android - Получить идентификатор сгенерированной кнопки внутри элемента onBindViewHolder - PullRequest
0 голосов
/ 31 декабря 2018

Я получил recyclerView с несколькими предметами (ViewHolders).В одном ( ViewHolderItemTratamentos ) из них я получил следующие элементы:

enter image description here

Когда первый «add button»при щелчке по макету inflator те же элементы (editText и button) создаются под предыдущими.Вот так:

enter image description here Код из моего 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 = "-"
     }
  }

Итак, проблема в том, что я не могу получить id из button layout.used_products_field для генерации другой новой строки.Должен ли я раздуть этот макет (это имеет какой-то смысл?)?И должен ли я давать одинаковые id каждому button (статическому и сгенерированному)?

Содержимое поля R.layout.used_products_:

<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"/>

Ответы [ 2 ]

0 голосов
/ 31 декабря 2018

Вы можете получить виды внутри раздутого макета (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)
0 голосов
/ 31 декабря 2018

Вы должны получить кнопку после добавления ее к родителю 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
    }
}
...