Creación de un RecyclerView

RecyclerView (punto 4.3 en Plataforma Moodle)

Creación de listas con RecyclerView:

El patrón ViewHolder y su uso en un RecyclerView:

Cómo usar un RecyclerView:

Uso de un ListView:

Using an ArrayAdapter with ListView

El patrón ViewHolder, qué es y cómo utilizarlo

How ListView’s recycling mechanism works

Uso de un RecyclerView:

Using the RecyclerView

Ejercicio: Crear un Recycler View de contactos

Notifying the Adapter

Código en

addMoreButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // record this value before making any changes to the existing list
        int curSize = adapter.getItemCount();
        // Add a new contact
        // contacts.add(0, new Contact("Barney", true));
        contacts.add(curSize, new Contact("Barney", true));
        // Notify the adapter that an item was inserted at position 0

Attaching Click Handlers using Listeners

Creating Custom Listeners

Código en

/***** Creating OnItemClickListener *****/

// Define the listener interface
public interface OnItemClickListener {
    void onItemClick(View itemView, int position);

// Define listener member variable
private OnItemClickListener listener;

// Define the method that allows the parent activity or fragment to define the listener
public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;

 // Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{
    // Your holder should contain a member variable
    // for any view that will be set as you render a row
    public TextView nameTextView;
    public Button messageButton;

    // We also create a constructor that accepts the entire item row
    // and does the view lookups to find each subview
    public ViewHolder(View itemView) {
        // Stores the itemView in a public final member variable that can be used
        // to access the context from any ViewHolder instance.

        nameTextView = (TextView) itemView.findViewById(;
        messageButton = (Button) itemView.findViewById(;

     public void onClick(View view) {
         // Triggers click upwards to the adapter on click
         if (listener != null) {
             int position = getAdapterPosition();
             if (position != RecyclerView.NO_POSITION) {
                 listener.onItemClick(itemView, position);

Mejora: Utilizar ViewBinding en el adapter

Ejercicio: PersonalNote (en Plataforma Moodle)

Más información:

Cómo crear listas dinámicas con RecyclerView

RecyclerView + CardView – Part 1: Layouts

RecyclerView + CardView – Part 2: Adapter


Deja una respuesta