android - 使用 Kotlin 将多个 OnClickListener 添加到 RecyclerView

标签 android kotlin android-recyclerview

我正在尝试使用 Kotlin 构建一个可运行的应用程序,但在尝试为我的三个按钮实现 OnClickListeners 时遇到了障碍。我的 RecyclerView 已正确填充,但尽管遵循 this SO post 上的建议(Kotlin 中除外)及以下 the documentation ,尽管我仍然无法让实现正常工作。



下面的代码是我用于实现的适配器类。

class BrowseHabitsAdapter(private val habits: ArrayList<Habit>) :
    RecyclerView.Adapter<BrowseHabitsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.habit_card, parent, false)

        return ViewHolder(itemView, object: HabitClickListener {
            override fun onDecrease(position: Int) {
                val streak = itemView.dayCounter.text.toString().toInt()
                itemView.dayCounter.text = streak.dec().toString()
            }

            override fun onIncrease(position: Int) {
                val streak = itemView.dayCounter.text.toString().toInt()
                itemView.dayCounter.text = streak.inc().toString()
            }

            override fun onEdit(position: Int) {
                TODO("Change Activity to Edit")
            }
        })


    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = habits[position]

        holder.habitTitle.text = currentItem.title
        holder.streak.text = currentItem.streak.toString()
    }

    override fun getItemCount() = habits.size


    class ViewHolder(itemView : View, listener : HabitClickListener) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        val habitTitle: TextView = itemView.habitTitle
        val streak: TextView = itemView.dayCounter
        val decreaseCounterButton : Button = itemView.decreaseCounterButton
        val increaseCounterButton : Button = itemView.increaseCounterButton
        val listener = listener

        init {
            decreaseCounterButton.setOnClickListener(this)
            increaseCounterButton.setOnClickListener(this)
        }

        override fun onClick(v: View?) {
            when (itemView.id) {
                itemView.decreaseCounterButton.id -> listener.onDecrease(this.layoutPosition)
                itemView.increaseCounterButton.id -> listener.onIncrease(this.layoutPosition)
            }
        }
    }

    interface HabitClickListener {
        fun onDecrease(position : Int)
        fun onIncrease(position : Int)
        fun onEdit(position : Int)
    }
}

以下是定义我的一张卡片的 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:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="10dp"
    app:cardBackgroundColor="#eeeeee"
    app:cardCornerRadius="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/cardHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/habitTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:text="@string/default_card_title"
                android:textSize="18sp" />

            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1" />

            <ImageView
                android:id="@+id/settingsIcon"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_gravity="bottom"
                android:layout_marginRight="10dp"
                app:srcCompat="@android:drawable/ic_menu_manage" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/cardControls"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/decreaseCounterButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="-"
                android:textAllCaps="false"
                android:textSize="30sp" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/dayCounter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:fontFamily="sans-serif-medium"
                android:text="0"
                android:textAlignment="center"
                android:textSize="30sp"
                android:textStyle="bold" />

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <Button
                android:id="@+id/increaseCounterButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="+"
                android:textSize="30sp" />
        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

任何可以提供关于我做错了什么以及详细情况的额外解释将不胜感激!

最佳答案

您使用的是 kotlin,因此需要实现 View.OnClickListener,您可以在任何 View 上直接使用 setOnClickListener

在 ViewHolder 类中:

itemView.increaseCounterButton.setOnClickListener{

       listener.onIncrease(this.layoutPosition)
 }       

itemView.decreaseCounterButton.setOnClickListener{

      listener.onDecrease(this.layoutPosition)
 } 

关于android - 使用 Kotlin 将多个 OnClickListener 添加到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62078515/

相关文章:

android - 从 Google Play 安装应用程序时,Android 中的 AppLinks(已验证的 Deeplinks)不起作用

android命令sqlite表

android - 尝试在 Android 库中使用 Room,但找不到方法 ksp()(Kotlin 符号处理)

java - 单击 RecyclerView 项目更改其样式

android - 无法在 Kotlin (Android Studio) 中实现 Groupie

android - 自定义 ListView 和 Adapter 以及 Listview 选择变得疯狂?

android - 我想在用户卸载并重新安装应用程序后向他们提供反馈?

kotlin - Mockito when子句在Kotlin中不起作用

android - 无法从内部类 : Kotlin android 访问变量

Android RecyclerView 找出 ItemDecoration 上的第一个和最后一个 View