Duration
10 minutes
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
-
Create a new Xamarin.Android application (Single View) named Dining. Create a new Xamarin.Android application named Dining. - 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.
-
Open Resources > layout > content_main.axml. Open Main.axml. -
Remove the TextView
that was included as part of the start code.Remove the button that was included as part of the start code. -
Add a
RecyclerView
to your root layout. Give it an id so you can access it from your code-behind. - Open MainActivity.cs.
- 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.
-
Add a
RecyclerView
field to yourMainActivity
class. -
Inside
OnCreate
, useFindViewById
to get a reference to theRecyclerView
in your UI and load the reference into your field. - 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.