Duration
15 minutes
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.
- Add an
Action<int>
parameter to your view holder's constructor, invoke it when the user touches theitemView
. - Add an
ItemClick
event to your adapter. Raise your event when your view holder reports a touch has occured. - 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
-
Open
RestaurantViewHolder.cs
. -
Add a second parameter to your constructor of type
Action<int>
. Store theAction
in a field namedlistener
. -
Subscribe to the
Click
event on theitemView
(the one passed to your view holder as a constructor parameter). -
In your click handler, retrieve the
AdapterPosition
, test to make sure it's notRecyclerView.NoPosition
, then invoke theAction<int>
stored in yourlistener
field (optionally, you can add a check fornull
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.
Report user actions
-
Open
RestaurantAdapter.cs
. -
Add the event shown below to your adapter.
public event EventHandler<int> ItemClick;
-
Add a method to your adapter that fits the
Action<int>
signature as shown below. Complete the implementation of this method so it raises theItemClick
event whenever it is called.void OnItemClick(int position) { // TODO: raise ItemClick event }
-
Modify your implementation of
OnCreateViewHolder
so it passes yourOnItemClick
method as a parameter to your view holder's constructor.
Respond to item-click
-
Open
MainActivity.cs
. -
Locate the code where you create your adapter. Subscribe a handler to your adapter's
ItemClick
event. In your handler, useSystem.Diagnostics.Debug.WriteLine
to print out the position of the click. - 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.