如何获取当前选中的item在RecyclerView中的位置?

How to get currently selected item position in RecyclerView?

如何获取在 RecyclerView 中使用 checkBox 选择的项目的项目位置? here你可以看到它的样子

TaskRecyclerAdapter.kt

 class TaskRecycleAdapter: RecyclerView.Adapter<TaskRecycleAdapter.MyViewHolder>() {

    private var list = emptyList<Data>()

    private lateinit var mListener : OnItemClickListener


     class MyViewHolder(binding: TaskHolderBinding, listener: OnItemClickListener): RecyclerView.ViewHolder(binding.root) {

        init {

            itemView.setOnClickListener {

                listener.onItemClick(adapterPosition)

            }

        }
    }

    interface OnItemClickListener {

        fun onItemClick(position: Int)

    }

    fun setOnItemClickListener(listener: OnItemClickListener) {

        mListener = listener

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(TaskHolderBinding.inflate(LayoutInflater.from(parent.context), parent, false), mListener)
    }

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

        val item = list[position]

        holder.itemView.findViewById<TextView>(R.id.task).text = item.task
        holder.itemView.findViewById<TextView>(R.id.taskDescription).text = item.task_Details
    }

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

    fun setData(newList: List<Data>) {
        val diffUtil = DiffUtil(list, newList )
        val diffResults = calculateDiff(diffUtil)
        list = newList
        diffResults.dispatchUpdatesTo(this)
    }
    fun getTaskAt(position: Int): Data {
        return list[position]
    }
}

task_holder.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/taskHolder"
    app:cardCornerRadius="15dp"
    android:backgroundTint="@color/card"
    app:cardElevation="3dp"
    app:contentPadding="3dp"
    android:layout_marginTop="7dp"
    android:layout_marginBottom="7dp"
    android:layout_marginEnd="3dp"
    android:layout_marginStart="3dp" >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="88dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/task_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/task"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="98dp"
            android:layout_marginTop="4dp"
            android:contentDescription="@string/task"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold|italic"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/taskDescription"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="100dp"
            android:layout_marginBottom="4dp"
            android:contentDescription="@string/task"
            android:textAlignment="center"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/task"
            app:layout_constraintVertical_bias="1.0" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.924"
            app:layout_constraintStart_toEndOf="@+id/task"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.1" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

我是 Java 开发人员,所以我无法使用 Kotlin 进行编码,但我将向您展示如何在 RecyclerView 中获取任何视图的位置

holder.itemView.setOnClickListener(view -> {
            Toast.makeText(context, "This is position of your Every ItemViews", Toast.LENGTH_SHORT).show();
        });

在您的 onBindViewHolder 中使用此代码 holder.itemView.setOnClickListener 并创建新的点击侦听器。在这里你可以通过编码得到你的物品的位置,你可以通过添加Toast来检查。 您的代码如下所示:-

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

        val item = list[position]

        holder.itemView.findViewById<TextView>(R.id.task).text = item.task
        holder.itemView.findViewById<TextView>(R.id.taskDescription).text = item.task_Details
holder.itemView.setOnClickListener(view -> {
            Toast.makeText(context, "This is position of your Every ItemViews", Toast.LENGTH_SHORT).show();
        });
    }

确保将我的代码的 JAVA 更改为 Kotlin。如果您有任何问题,请告诉我