Tutorial source : Google CodeLab
Date : 2021/04/06
Complete course : 教程目录 (java).
Note : The link in this article requires Google access
1、Welcome
This practical codelab is part of Unit 2: User experience in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence:
- For the complete list of codelabs in the course, see Codelabs for Android Developer Fundamentals (V2).
- For details about the course, including links to all the concept chapters, apps, and slides, see Android Developer Fundamentals (Version 2).
Note: This course uses the terms “codelab” and “practical” interchangeably.
Introduction
Letting the user display, scroll, and manipulate a list of similar data items is a common app feature. Examples of scrollable lists include contact lists, playlists, saved games, photo directories, dictionaries, shopping lists, and indexes of documents.
In the practical on scrolling views, you use ScrollView
to scroll a View
or ViewGroup
. ScrollView
is easy to use, but it’s not recommended for long, scrollable lists.
RecyclerView
is a subclass of ViewGroup
and is a more resource-efficient way to display scrollable lists. Instead of creating a View
for each item that may or may not be visible on the screen, RecyclerView
creates a limited number of list items and reuses them for visible content.
In this practical you do the following:
- Use
RecyclerView
to display a scrollable list. - Add a click handler to each list item.
- Add items to the list using a floating action button (FAB), the pink button in the screenshot in the app overview section. Use a FAB for the primary action that you want the user to take.
What you should already know
You should be able to:
- Create and run apps in Android Studio.
- Create and edit UI elements using the layout editor, entering XML code directly, and accessing elements from your Java code.
- Create and use string resources.
- Convert the text in a
View
to a string usinggetText()
. - Add an
onClick()
handler to aView
. - Display a
Toast
message.
What you’ll learn
- How to use the
RecyclerView
class to display items in a scrollable list. - How to dynamically add items to the
RecyclerView
as they become visible through scrolling. - How to perform an action when the user taps a specific item.
- How to show a FAB and perform an action when the user taps it.
What you’ll do
-
Create a new app that uses a
RecyclerView
to display a list of items as a scrollable list and associate click behavior with the list items. -
Use a FAB to let the user add items to the
RecyclerView
.
2、App Overview
The RecyclerView app demonstrates how to use a RecyclerView
to display a long scrollable list of words. You create the dataset (the words), the RecyclerView
itself, and the actions the user can take:
- Tapping a word marks it clicked.
- Tapping the floating action button (FAB) adds a new word.
3、Task 1: Create a new project and dataset
Before you can display a RecyclerView
, you need data to display. In this task, you will create a new project for the app and a dataset. In a more sophisticated app, your data might come from internal storage (a file, SQLite database, saved preferences), from another app (Contacts, Photos), or from the internet (cloud storage, Google Sheets, or any data source with an API). Storing and retrieving data is a topic of its own covered in the data storage chapter. For this exercise, you will simulate data by creating it in the onCreate()
method of MainActivity
.
1.1. Create the project and layout
- Start Android Studio.
- Create a new project with the name RecyclerView, select the Basic Activity template, and generate the layout file.
The Basic Activity template, introduced in the chapter on using clickable images, provides a floating action button (FAB) and app bar in the Activity
layout (activity_main.xml
), and a layout for the Activity
content (content_main.xml
).
- Run your app. You should see the RecyclerView app title and “Hello World” on the screen.
If you encounter Gradle-related errors, sync your project as described in the practical on installing Android Studio and running Hello World.
1.2. Add code to create data
In this step you create a LinkedList
of 20 word strings that end in increasing numbers, as in [“Word 1”, “Word 2”, “Word 3”, … ].
- Open MainActivity and add a private member variable for the
mWordList
linked list.
public class MainActivity extends AppCompatActivity {
private final LinkedList<String> mWordList = new LinkedList<>();
// ... Rest of MainActivity code ...
}
- Add code within the
onCreate()
method that populatesmWordList
with words:
// Put initial data into the word list.
for (int i = 0; i < 20; i++) {
mWordList.addLast("Word " + i);
}
The code concatenates the string "Word "
with the value of i
while increasing its value. This is all you need as a dataset for this exercise.
1.3. Change the FAB icon
For this practical, you will use a FAB to generate a ne