android - 如何在自定义适配器内生成的按钮中设置一些 Action

标签 android kotlin adapter onclicklistener

我得到了以下组成:

enter image description here

此布局是我的RecyclerView.Adapter 的一个项目。单击 X 按钮 (holder.delete_button) 会删除其自身和 EditText;基本上它删除了行。

ADD FIELD 按钮添加新行(通过 inflater):

enter image description here

这是添加新行的代码:

  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 中初始化的按钮 delete_buttonid但是对于接下来的 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>

关于android - 如何在自定义适配器内生成的按钮中设置一些 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54132100/

相关文章:

java - 通过 Activity 将数据从一个 fragment 传递到另一个 fragment - 滞后

java - 如何使用多部分/表单数据?

android - E/RecyclerView : No adapter attached; skipping layout

android - Proguard 和 RxAndroid V1.1.0

android - kotlin 类,我在类外修改了一个列表,它最终修改了类内的值

android - 二维列表的 Kotlin reduce 函数不起作用

kotlin - 订阅链中的可观察对象并将观察者设置为可观察对象

kotlin - Kotlin中的相同属性名称和主要构造函数参数名称

android - 我应该为每个 recyclerview 创建适配器吗?

java - 从数组创建适配器(AutoCompleteTextView)