VIDEO
On a vu dans l’article précédent comment créer une liste stylée avec un RecyclerView. Si tu ne l’as pas lu et que tu ne sais pas ce qu’est un RecyclerView, je t’invite à aller le voir : https://developpezvosappsandroid.com/creer-recyclerview
Dans l’article d’aujourd’hui, nous allons voir comment gérer les clics sur les items de ton RecyclerView !
Nous allons voir deux types de clic :
Le clic sur une ligne de ton RecyclerView
Le clic sur un bouton se trouvant sur une ligne de ton RecyclerView
Tu vas voir que ces deux cas sont assez similaires et ne sont pas compliqués à gérer (du moins quand on sait le faire 😉 )
row_ingredient.xml :
Spoiler
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom"
android:textSize="20sp" />
<TextView
android:id="@+id/quantite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantite"
android:textSize="15sp" />
<Button
android:id="@+id/le_bouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Le bouton"/>
</LinearLayout>
[collapse]
IngredientAdapter.Java :
Spoiler
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.IngredientViewHolder>{
Context context;
List<Ingredient> listeIngredient;
public static class IngredientViewHolder extends RecyclerView.ViewHolder{
TextView nom;
TextView quantite;
Button button;
public IngredientViewHolder(View itemView) {
super(itemView);
nom = itemView.findViewById(R.id.nom);
quantite = itemView.findViewById(R.id.quantite);
button = itemView.findViewById(R.id.le_bouton);
}
}
public IngredientAdapter(Context context, List<Ingredient> listeIngredient) {
this.context = context;
this.listeIngredient = listeIngredient;
}
@Override
public IngredientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_ingredient, parent, false);
IngredientViewHolder ingredientViewHolder = new IngredientViewHolder(view);
return ingredientViewHolder;
}
@Override
public void onBindViewHolder(IngredientViewHolder holder, final int position) {
Ingredient ingredient = listeIngredient.get(position);
holder.nom.setText(ingredient.getNom());
holder.quantite.setText(ingredient.getQuantite());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clic sur le bouton", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, listeIngredient.get(position).getNom(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return listeIngredient.size();
}
}
[collapse]
La ligne à modifier dans MainActivity.Java :
ingredientAdapter = new IngredientAdapter(this, listeCourse);