如何在自定义适配器内生成的按钮中设置一些动作
How to set some action in generated buttons inside custom Adapter
我得到了以下作文:
此布局是我的 RecyclerView.Adapter
的一个项目。单击 X 按钮 (holder.delete_button
) 会删除其自身和 EditText
;基本上它删除了行。
ADD FIELD 按钮添加新行(通过 inflater):
这是添加新行的代码:
holder.add_field_button.setOnClickListener {
holder.parent_layout.apply {
val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.generated_layout, this, false)
holder.parent_layout.addView(rowView, holder.parent_layout.childCount!! - 0)
}
}
我的问题是 我只能删除第一行,因为这是我唯一可以在 ViewHolder
中初始化的 button
id
共 delete_button
。 但是对于接下来的 X 个按钮,我不能做任何操作,因为按钮在外部布局中膨胀,称为 generated_layout
!我试图生成 id,但我不知道如何将它们放入数组中。下面是删除一行的代码:
holder.delete_button.setOnClickListener{
holder.parent_layout.removeView(holder.delete_button.parent as View)
}
下面是generated_layout的代码,还有:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:background="@android:drawable/ic_delete"/>
</LinearLayout>
像这样设置 onClick 侦听器
holder.add_field_button.setOnClickListener {
holder.parent_layout.apply {
val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.generated_layout, this, false)
val rowViewDeleteButton=rowView.findViewById(R.id.deletebutton)
rowViewDeleteButton.setOnClickListener{
holder.parent_layout.removeView(it.parent as View)
}
holder.parent_layout.addView(rowView, holder.parent_layout.childCount!! - 0)
}
}
并给你的删除按钮 id
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Button
android:id="@+id/deletebutton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:background="@android:drawable/ic_delete"/>
</LinearLayout>
我得到了以下作文:
此布局是我的 RecyclerView.Adapter
的一个项目。单击 X 按钮 (holder.delete_button
) 会删除其自身和 EditText
;基本上它删除了行。
ADD FIELD 按钮添加新行(通过 inflater):
这是添加新行的代码:
holder.add_field_button.setOnClickListener {
holder.parent_layout.apply {
val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.generated_layout, this, false)
holder.parent_layout.addView(rowView, holder.parent_layout.childCount!! - 0)
}
}
我的问题是 我只能删除第一行,因为这是我唯一可以在 ViewHolder
中初始化的 button
id
共 delete_button
。 但是对于接下来的 X 个按钮,我不能做任何操作,因为按钮在外部布局中膨胀,称为 generated_layout
!我试图生成 id,但我不知道如何将它们放入数组中。下面是删除一行的代码:
holder.delete_button.setOnClickListener{
holder.parent_layout.removeView(holder.delete_button.parent as View)
}
下面是generated_layout的代码,还有:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:background="@android:drawable/ic_delete"/>
</LinearLayout>
像这样设置 onClick 侦听器
holder.add_field_button.setOnClickListener {
holder.parent_layout.apply {
val inflater = LayoutInflater.from(context)
val rowView = inflater.inflate(R.layout.generated_layout, this, false)
val rowViewDeleteButton=rowView.findViewById(R.id.deletebutton)
rowViewDeleteButton.setOnClickListener{
holder.parent_layout.removeView(it.parent as View)
}
holder.parent_layout.addView(rowView, holder.parent_layout.childCount!! - 0)
}
}
并给你的删除按钮 id
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Button
android:id="@+id/deletebutton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:background="@android:drawable/ic_delete"/>
</LinearLayout>