RecyclerView and CardView in Android

Duration

15 minutes

Tip: If you are doing this exercise live in a session, make sure to make good use of the instructor, they are online to answer any questions you have!

Goals

In this exercise, you will add an item-click event to your adapter. This will require adding code to both your adapter and view holder: your view holder will detect the user's action and your adapter will report it to the client code.

Required assets

The provided Resources folder for this part contains a subfolder named Completed with a solution you can use to check your work. This lab is a continuation of the previous one. If you did not complete the previous exercise, you can use the Completed solution from the previous part as starter code for this part.

Challenge

Use the guidelines here to code an item-click event. Alternatively, you can use the step-by-step instructions given below.

  1. Add an Action<int> parameter to your view holder's constructor, invoke it when the user touches the itemView.
  2. Add an ItemClick event to your adapter. Raise your event when your view holder reports a touch has occured.
  3. In your client code, subscribe to your adapter's event and print out which item was touched.

Steps

Below are the step-by-step instructions to implement the exercise.

Detect user actions

  1. Open RestaurantViewHolder.cs.
  2. Add a second parameter to your constructor of type Action<int>. Store the Action in a field named listener.
  3. Subscribe to the Click event on the itemView (the one passed to your view holder as a constructor parameter).
  4. In your click handler, retrieve the AdapterPosition, test to make sure it's not RecyclerView.NoPosition, then invoke the Action<int> stored in your listener field (optionally, you can add a check for null before you invoke the listener; the solution won't do this to keep the code simple). The complete code for these last few steps is given below if you need it, hidden behind a button.

    Show Code

Report user actions

  1. Open RestaurantAdapter.cs.
  2. Add the event shown below to your adapter.
    public event EventHandler<int> ItemClick;
    
  3. Add a method to your adapter that fits the Action<int> signature as shown below. Complete the implementation of this method so it raises the ItemClick event whenever it is called.
    void OnItemClick(int position)
    {
      // TODO: raise ItemClick event
    }
    
  4. Modify your implementation of OnCreateViewHolder so it passes your OnItemClick method as a parameter to your view holder's constructor.

Respond to item-click

  1. Open MainActivity.cs.
  2. Locate the code where you create your adapter. Subscribe a handler to your adapter's ItemClick event. In your handler, use System.Diagnostics.Debug.WriteLine to print out the position of the click.
  3. Run the app to test your work.

Summary

Here you coded an item-click event in your adapter. Your implementation was a collaboration between your view holder and your adapter. Your view holder has references to the UI views and knows the position of the data item it represents so it is the natural place to detect user actions. However, your view holder is an implementation detail that client code never sees so it is not the appropriate place to report user actions; that code belongs in your adapter.

Go Back