RecyclerView
fa23
Messages postés
95
Statut
Membre
-
BunoCS Messages postés 16550 Statut Modérateur -
BunoCS Messages postés 16550 Statut Modérateur -
Bonjour je suis débutant en Kotlin et je voudrais avoir un peu d'aide,
Dans mon code actuelle, j'ai seulement créé un listener sur l’ensemble de la ligne c’est à dire le parent.
Ce que je veux faire c'est de donner la possibilité à l’utilisateur de cliquer sur les enfants (Views) qui composent la ligne du RecyclerView.
Lorsqu'il clique sur textCours, un message s'affiche du genre " Vous avez cliquer sur le nom" à l’aide d’un Toast.
Et aussi, lorsqu'il clique sur textTitre, un message s'affiche du genre " Vous avez cliquer sur.." à l’aide d’un Toast.
Pour connaître sur quels Views l’utilisateur a cliqué.
Merci
fa23
Dans mon code actuelle, j'ai seulement créé un listener sur l’ensemble de la ligne c’est à dire le parent.
Ce que je veux faire c'est de donner la possibilité à l’utilisateur de cliquer sur les enfants (Views) qui composent la ligne du RecyclerView.
Lorsqu'il clique sur textCours, un message s'affiche du genre " Vous avez cliquer sur le nom" à l’aide d’un Toast.
Et aussi, lorsqu'il clique sur textTitre, un message s'affiche du genre " Vous avez cliquer sur.." à l’aide d’un Toast.
Pour connaître sur quels Views l’utilisateur a cliqué.
Merci
fa23
Configuration: Windows / Chrome 81.0.4044.138
5 réponses
Mon code RecyclerAdapter:
package com.example.ContactActivity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.notekotlincours1.R
class NoteRecyclerAdapter (private val context: Context, private val notes: List<ContactNote>): RecyclerView.Adapter<NoteRecyclerAdapter.ViewHolder>() {
private val layoutInflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = layoutInflater.inflate(R.layout.item_list_note, parent, false)
return ViewHolder(itemView)
}
override fun getItemCount() = notes.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val note = notes[position]
holder.textCours.text = note.contactInfo?.titreProf
holder.textTitre.text = note.Commentaire
holder.notePosition = position
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val textCours = itemView.findViewById<TextView>(R.id.textCours)
val textTitre = itemView.findViewById<TextView>(R.id.textTitre)
var notePosition = 0
init {
itemView?.setOnClickListener {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(EXTRA_NOTE_POSITION, notePosition)
context.startActivity(intent)
}
}
}
}
Et mon item_list_note:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:contentPadding="16dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:tint="@android:color/holo_orange_dark"
app:layout_constraintBottom_toBottomOf="@+id/textTitre"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_assignment_white_24dp" />
<TextView
android:id="@+id/textCours"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="@android:color/holo_orange_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textTitre"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/textCours"
app:layout_constraintTop_toBottomOf="@+id/textCours" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
Hello,
2 possibilités :
- soit, dans ton onClickListener(), tu recherches la vue qui a été tapée
- soit, dans ton ViewHolder, tu attaches un clickListener sur chaque vue cliquable
2 possibilités :
- soit, dans ton onClickListener(), tu recherches la vue qui a été tapée
- soit, dans ton ViewHolder, tu attaches un clickListener sur chaque vue cliquable
Bonjour, j'ai choisi de faire avec onClickListener(), est ce que tu connais un site qui explique comment faire ca ?
Merci,
fa23
Merci,
fa23
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Hello,
La doc officielle : http://developer.android.com/index.html
Après, y'a quand même pas mal de tuto/article qui parle du onClickListener avec une RecyclerView...
La doc officielle : http://developer.android.com/index.html
Après, y'a quand même pas mal de tuto/article qui parle du onClickListener avec une RecyclerView...