CodeLab:Android fundamentals 04.5: RecyclerView

本教程介绍了如何在Android应用中使用RecyclerView显示可滚动的单词列表,并添加点击交互。首先,创建新项目和数据集,使用ArrayList存储20个以数字结尾的单词。然后,设置RecyclerView显示列表,通过点击列表项实现文字高亮,浮动操作按钮(FAB)用于添加新单词。此外,学习如何使用ViewHolder和Adapter动态添加和更新列表内容。
摘要由CSDN通过智能技术生成

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:

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 using getText().
  • Add an onClick() handler to a View.
  • 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

  1. Start Android Studio.
  2. 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).

  1. 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”, … ].

  1. 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 ...
}
  1. Add code within the onCreate() method that populates mWordList 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值