Kotlin Android Studio-手柄按钮在recyclerView适配器内单击

时间:2018-12-09 20:03:16

标签: android kotlin android-recyclerview

我有一个名为ArticleAdapter的recyclerView适配器

import kotlinx.android.synthetic.main.articlerecycler_item.view.*


class ArticleAdapter(private val controller: IController) : RecyclerView.Adapter<ArticleViewHolder>() {

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
      val view = LayoutInflater.from(parent.context).inflate(R.layout.articlerecycler_item, parent, false)
      return ArticleViewHolder(view)


  }
    // How implement a button click for `btnSave`
    /** Called when the user taps the Save button  */
     btnSave.setOnClickListener(){
        (R.layout.articlerecycler_item)
        AlertDialog.Builder(this)
                .setMessage("Article Saved.")
                .create()
                .show()
        // Do something in response to button click
    }

  override fun getItemCount(): Int {
      return controller.articles.size
  }

  override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {

      val article: Article = controller.articles[position]

      if(article.title.length > 100) {
        holder.itemView.titleTv.text = "${article.title.substring(0, 99)}..."
      } else {
        holder.itemView.titleTv.text = article.title
      }

      holder.itemView.authorTv.text = article.author
  }

}
class ArticleViewHolder(view: View?) : RecyclerView.ViewHolder(view)

然后在我的recyclerView布局中,有一个按钮

<Button
    android:id="@+id/btnSave"
    android:layout_width="70dp"
    android:layout_height="40dp"
    android:onClick="btnSave"
    android:text="@string/save"
    android:textAlignment="center" />

我可以在onBindViewHolder内部处理事件吗?我不确定如何在我的recyclerView适配器中实现侦听器

3 个答案:

答案 0 :(得分:1)

我想您应该添加一个扩展程序列表程序。这是https://dzone.com/articles/click-listener-for-recyclerview-adapter。 另外,您必须为保存按钮添加自定义列表器。

override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {

  val article: Article = controller.articles[position]

  if(article.title.length > 100) {
    holder.itemView.titleTv.text = "${article.title.substring(0, 99)}..."
  } else {
    holder.itemView.titleTv.text = article.title
  }

  holder.itemView.authorTv.text = article.author


  *holder.btnSave.setOnClickListener(){_ ->
    **yourCustomListener**.OnClick(data/position) }*
}

答案 1 :(得分:1)

您必须在onBindViewHolder()中处理它,但是负责创建和显示AlertDialog的逻辑不应放在RecyclerView中。您需要创建一个接口,该接口负责将onClick()事件信息从适配器传递到活动/片段,并在其中显示AlertDialog

class ArticleAdapter(private val controller: IController) : RecyclerView.Adapter<ArticleViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.articlerecycler_item, parent, false)
    return ArticleViewHolder(view)
}

override fun getItemCount(): Int {
    return controller.articles.size
}

override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
    val article: Article = controller.articles[position]

    if(article.title.length > 100) {
        holder.itemView.titleTv.text = "${article.title.substring(0, 99)}..."
    } else {
        holder.itemView.titleTv.text = article.title
    }

    btnSave.setOnClickListener {
        listener?.onSaveButtonClick()
    }

    holder.itemView.authorTv.text = article.author
}

private var listener: OnClickListener? = null

fun setListener(listener: OnClickListener) {
    this.listener = listener
}

}

interface OnClickListener {
    fun onSaveButtonClick()
}

class ArticleViewHolder(view: View?) : RecyclerView.ViewHolder(view)

现在通过OnClickListener方法将setListener()的实例从您的活动传递到适配器。

答案 2 :(得分:1)

处理适配器单击的项目的一个好方法是将它们传递给创建它们的活动或片段。

使用kotlin,您可以使用函数定义而不是接口来进行此简单的工作,因此,在适配器类内部添加var类型的函数:

private var listener: ((item: DataClass) -> Unit)? = null

fun setOnItemClickListener(listener: (item: DataClass) -> Unit) {
    this.listener = listener
}

ViewHolder类中设置侦听器,如下所示:

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    init {
        itemView.btn_save.setOnClickListener { listener?.invoke(data[adapterPosition]) }
    }

    // ...
}

最后,在您的活动或片段中,您可以通过添加新的侦听器轻松访问项目:

adapter.setOnItemClickListener { it -> TODO() }

请注意,在ViewHolder构造函数(init)中设置侦听器效率更高,因为不必为每个数据绑定都设置侦听器。