RecyclerView and CardView in Android

Duration

10 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

The goal here is to create a new project and add a RecyclerView to your main Activity. This first part is mostly setup; the project will eventually display information about a collection of restaurants.

Required assets

The provided Resources folder for this part contains a subfolder named Completed with a solution you can use to check your work. Please make sure you have this folder before you begin.

Steps

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

Dining application

  1. Create a new Xamarin.Android application (Single View) named Dining. Create a new Xamarin.Android application named Dining.
  2. Add the Xamarin Support Library v7 RecyclerView NuGet package to your project. Note that the RecyclerView package needs Xamarin.Android.Support.v4 - this dependency will be added automatically if it is not already part of your project.
  3. Open Resources > layout > content_main.axml. Open Main.axml.
  4. Remove the TextView that was included as part of the start code. Remove the button that was included as part of the start code.
  5. Add a RecyclerView to your root layout. Give it an id so you can access it from your code-behind.

    Show Code

  6. Open MainActivity.cs.
  7. Remove the code to set the click-count on the button. This was added as part of the starter template and will not be needed.
  8. Add a RecyclerView field to your MainActivity class.
  9. Inside OnCreate, use FindViewById to get a reference to the RecyclerView in your UI and load the reference into your field.
  10. Compile your app. Do not run it; it will fail since you have not yet loaded a layout manager.

Summary

Android packaged the RecyclerView class in a support library so it will be widely available across Android versions. In this exercise, you setup your project to use a RecyclerView by first adding the package and then creating an instance in your UI.

Go Back