【问题标题】:RecyclerView onClickListener not firing (Kotlin)RecyclerView onClickListener 未触发(Kotlin)
【发布时间】:2021-06-09 00:01:02
【问题描述】:

我正在尝试为我的 Kotlin RecyclerView 实现 onClickListener。根本就没有开火。该代码没有标记为红色或类似的东西。您在 NotesActivity.kt 中看到的 Log 和 Toast 没有被执行。我的 recyclerview 本身就在工作,但 onClickListener 根本不会触发。

NotesAdapter.kt

class NotesAdapter(private val dataSet: MutableList<Note>) :
    RecyclerView.Adapter<NotesAdapter.ViewHolder>() {

var clickListener: ClickListener? = null

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {


    val textViewTitle: TextView
    val textViewBody: TextView

    init {
        // Define click listener for the ViewHolder's View.
        textViewTitle = view.findViewById(R.id.textViewTitle)
        textViewBody = view.findViewById(R.id.textViewBody)

        if (clickListener != null) {
            itemView.setOnClickListener(this)
        }
    }

    override fun onClick(view: View?) {
        if (view != null) {
            clickListener?.onItemClick(view, adapterPosition)
        }
    }
}

fun setOnItemClickListener(clickListener: ClickListener) {
    this.clickListener = clickListener
}

interface ClickListener {
    fun onItemClick(v: View, position: Int)
}

// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
    // Create a new view, which defines the UI of the list item
    val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.adapter_note_layout, viewGroup, false)

    return ViewHolder(view)
}

// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {

    // Get element from your dataset at this position and replace the
    // contents of the view with that element
    viewHolder.textViewTitle.text = dataSet[position].title
    viewHolder.textViewBody.text = dataSet[position].body
}

override fun getItemCount() = dataSet.size
}

NotesActivity.kt

    adapter = NotesAdapter(mutableListOf())
    recyclerView.adapter = adapter

    adapter.setOnItemClickListener(object : NotesAdapter.ClickListener {
        override fun onItemClick(v: View, position: Int) {

            Log.i("test", "pos: $position")

            Toast.makeText(applicationContext, "pos: $position", Toast.LENGTH_SHORT)
        }
    })

adapter_note_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardBackgroundColor="@color/colorBackroundBright"
app:cardCornerRadius="15dp">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    android:paddingBottom="8dp">

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="title"
        android:textColor="@color/colorText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textViewBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="body"
        android:textColor="@color/colorText"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewTitle" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

【问题讨论】:

  • 你有没有试过这个post的解决方案?
  • 你知道onClick(view: View?) 是否被调用?
  • 你的clickListener为空,你只设置了点击监听if (clickListener != null) {
  • 将 logcat 控制杆更改为 Info 或将 .show() 添加到您的 toast 然后再次测试
  • @HenryTwist,我刚刚再次检查,它没有被调用,但我真的不知道为什么

标签: android kotlin android-recyclerview onclicklistener


【解决方案1】:

您只需要在将适配器设置为回收器之前设置您的侦听器。

只需更改您的代码

adapter = NotesAdapter(mutableListOf())
recyclerView.adapter = adapter

adapter.setOnItemClickListener(object : NotesAdapter.ClickListener {
    override fun onItemClick(v: View, position: Int) {

        Log.i("test", "pos: $position")

        Toast.makeText(applicationContext, "pos: $position", Toast.LENGTH_SHORT)
    }
})

到这里

 adapter = NotesAdapter(mutableListOf())
 adapter.setOnItemClickListener(object : NotesAdapter.ClickListener {
    override fun onItemClick(v: View, position: Int) {

        Log.i("test", "pos: $position")

        Toast.makeText(applicationContext, "pos: $position", Toast.LENGTH_SHORT)
    }
})

recyclerView.adapter = adapter

【讨论】:

  • 你试过在构造函数中声明clickListener吗?
  • 我猜你的viewholder初始化块有问题,这会影响你的itemview监听器的初始化。尝试删除if语句,只剩下itemView.setOnClickListener(this)。
猜你喜欢
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多