RecyclerView

Fermé
fa23 Messages postés 79 Date d'inscription lundi 20 juin 2016 Statut Membre Dernière intervention 28 juin 2024 - 21 mai 2020 à 04:34
BunoCS Messages postés 15480 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 26 juillet 2024 - 25 mai 2020 à 09:01
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




Configuration: Windows / Chrome 81.0.4044.138

5 réponses

fa23 Messages postés 79 Date d'inscription lundi 20 juin 2016 Statut Membre Dernière intervention 28 juin 2024
Modifié le 22 mai 2020 à 09:14
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)
            }
        }
    }
}

0
fa23 Messages postés 79 Date d'inscription lundi 20 juin 2016 Statut Membre Dernière intervention 28 juin 2024
21 mai 2020 à 04:37
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>
0
BunoCS Messages postés 15480 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 26 juillet 2024 3 904
22 mai 2020 à 09:17
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
0
fa23 Messages postés 79 Date d'inscription lundi 20 juin 2016 Statut Membre Dernière intervention 28 juin 2024
23 mai 2020 à 02:39
Bonjour, j'ai choisi de faire avec onClickListener(), est ce que tu connais un site qui explique comment faire ca ?

Merci,

fa23
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
BunoCS Messages postés 15480 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 26 juillet 2024 3 904
25 mai 2020 à 09:01
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...
0