Как я могу получить позицию элемента, выбранного с помощью checkBox
в RecyclerView
? здесь вы можете посмотреть, как это выглядит
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 моего кода. Если у вас возникли какие-либо проблемы, дайте мне знать
Поскольку вы используете Kotlin, я предлагаю использовать лямбду для прослушивателя кликов вместо реализации интерфейса, что-то вроде
listener : (Int) -> Unit
, и один из способов добиться того, чего вы хотите, — это создать еще один прослушиватель для флажка.