【问题标题】:Android - onClickListener not working for ImageButton in CardViewAndroid - onClickListener 不适用于 CardView 中的 ImageButton
【发布时间】:2021-04-04 10:59:04
【问题描述】:

我已经在 StackOverflow 上搜索了类似的问题,并且我已按照那里描述的说明进行操作,但我仍然无法让我的 onClickListener 为我的图像按钮触发。有人可以帮我吗?

我详细解释了我的问题。

这是我的 CardView xml:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp"
            android:textAppearance="?attr/textAppearanceHeadline6" />

        <TextView
            android:id="@+id/item_description"
            android:layout_below="@id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceBody1"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

        <com.google.android.material.button.MaterialButton
            style="?attr/borderlessButtonStyle"
            android:id="@+id/workout_start_action"
            android:layout_below="@id/item_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/start_workout" />

        <ImageButton
            android:id="@+id/workout_delete_imgButton"
            android:layout_below="@id/item_description"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:src="@drawable/ic_delete"
            android:padding="8dp"
            android:contentDescription="FavButtonDesc" />
    </RelativeLayout>

</com.google.android.material.card.MaterialCardView>

这是我的适配器:

package com.example.workouttimer.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.workouttimer.R
import com.example.workouttimer.model.Workout


class WorkoutItemAdapter(private val context: Context,
                         private val dataset: List<Workout>
) : RecyclerView.Adapter<WorkoutItemAdapter.WorkoutItemViewHolder>() {

    class WorkoutItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
        val textView = view.findViewById<TextView>(R.id.item_title)
        val descriptionTextView = view.findViewById<TextView>(R.id.item_description)
        val imgDeleteButton = view.findViewById<ImageView>(R.id.workout_delete_imgButton)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WorkoutItemViewHolder {
        // create a new view
        val adapterLayout = LayoutInflater.from(parent.context)
                .inflate(R.layout.workout_list_item, parent, false)

        return WorkoutItemViewHolder(adapterLayout)
    }

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

    override fun onBindViewHolder(holder: WorkoutItemViewHolder, position: Int) {
        val item = dataset[position]
        holder.textView.text = item.workoutName
        holder.descriptionTextView.text = item.workoutDescription

        holder.imgDeleteButton.setOnClickListener(View.OnClickListener {
            /*fun onClick(v: View) {
                Toast.makeText(v.context, "BORRADO!!!", Toast.LENGTH_LONG)
            }*/

            fun onClick(position: Int) {
                Toast.makeText(this.context, "BORRADO!!!", Toast.LENGTH_LONG)
            }
        })
    }
}

我做错了什么?提前致谢。

【问题讨论】:

  • view.findViewById 更改为 view.findViewById
  • 恐怕不行……

标签: android kotlin onclicklistener cardview android-imagebutton


【解决方案1】:

你确定它没有开火吗?我问是因为您忘记在敬酒时致电show()。所以在你的点击尝试记录事件,或者像这样在你的吐司上调用show()

Toast.makeText(context, "BORRADO!!!", Toast.LENGTH_LONG).show()

真正的问题

所以下面的代码是SAM conversion的例子

// What you did
View.OnClickListener { // This is where the SAM conversion happens
    fun onClick(position: Int) { // This is a local function defined inside your lambda
        Toast.makeText(this.context, "BORRADO!!!", Toast.LENGTH_LONG)
        }
    }
}

// What you should've done
View.OnClickListener { // This is where the SAM conversion happens
    Toast.makeText(this.context, "BORRADO!!!", Toast.LENGTH_LONG).show()
}

因为View.OnClickListener 只有一个抽象方法,通过使用 SAM 转换,您不必指定要覆盖的方法

编辑:在 Kotlin 中,您无法覆盖没有 override 关键字的方法

【讨论】:

  • 好吧,它在“onClick”中遇到了一个断点,但看起来它没有进入 Toast 行的函数。有什么想法吗?
  • 我看不出有什么特别之处。可能是onClick函数有错误的参数或什么?
  • 是的,Patrik 谢谢,这是真正的问题(我理解),现在它可以工作了,但我不明白的是这里提供的语法(我的代码基于该语法)(函数内函数):stackoverflow.com/questions/52404324/…stackoverflow.com/questions/36497691/…
  • 酷你能接受我的回答吗?我正在收集帽子:D
  • 是的,当然,但我必须弄清楚,如何(我是新人)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多