Kotlin/Android - 获取 onBindViewHolder Item 中生成的按钮的 id
Kotlin/Android - Get the id of generated button inside onBindViewHolder Item
我得到了一个 recyclerView
,其中包含多个项目 (ViewHolders
)。在其中一个 (ViewHolderItemTratamentos) 中,我得到了以下元素:
单击第一个 "add button
" 时,通过 inflator
布局,相同的元素(editText
和 button
)会在之前的元素下方创建。就像这样:
来自我的 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_field
的 button
中获取 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"/>
您应该在将按钮添加到父级后获取该按钮 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
}
}
您可以在展开的布局中获取视图(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)
我得到了一个 recyclerView
,其中包含多个项目 (ViewHolders
)。在其中一个 (ViewHolderItemTratamentos) 中,我得到了以下元素:
单击第一个 "add button
" 时,通过 inflator
布局,相同的元素(editText
和 button
)会在之前的元素下方创建。就像这样:
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_field
的 button
中获取 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"/>
您应该在将按钮添加到父级后获取该按钮 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
}
}
您可以在展开的布局中获取视图(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)