
本文共 17876 字,大约阅读时间需要 59 分钟。
文章目录
Create dynamic lists with RecyclerView
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they’re needed.
As the name implies, RecyclerView recycles those individual elements. When an item scrolls off the screen, RecyclerView doesn’t destroy its view. Instead, RecyclerView reuses the view for new items that have scrolled onscreen. This reuse vastly improves performance, improving your app’s responsiveness and reducing power consumption.
Note: Besides being the name of the class, RecyclerView is also the name of the library. In this page, RecyclerView
in code font
always means the class in the RecyclerView library.
Key classes
Several different classes work together to build your dynamic list.
- is the that contains the views corresponding to your data. It’s a view itself, so you add
RecyclerView
into your layout the way you would add any other UI element. - Each individual element in the list is defined by a view holder object. When the view holder is created, it doesn’t have any data associated with it. After the view holder is created, the
RecyclerView
binds it to its data. You define the view holder by extending . - The
RecyclerView
requests those views, and binds the views to their data, by calling methods in the adapter. You define the adapter by extending . - The layout manager arranges the individual elements in your list. You can use one of the layout managers provided by the RecyclerView library, or you can define your own. Layout managers are all based on the library’s abstract class.
You can see how all the pieces fit together in the or .
Steps for implementing your RecyclerView
If you’re going to use RecyclerView, there are a few things you need to do. They’ll be discussed in detail in the following sections.
- First of all, decide what the list or grid is going to look like. Ordinarily you’ll be able to use one of the RecyclerView library’s standard layout managers.
- Design how each element in the list is going to look and behave. Based on this design, extend the
ViewHolder
class. Your version ofViewHolder
provides all the functionality for your list items. Your view holder is a wrapper around aView
, and that view is managed byRecyclerView
. - Define the
Adapter
that associates your data with theViewHolder
views.
There are also that let you tailor your RecyclerView to your exact needs.
Plan your layout
The items in your RecyclerView are arranged by a class. The RecyclerView library provides three layout managers, which handle the most common layout situations:
- arranges the items in a one-dimensional list.
GridLayoutManager
arranges all items in a two-dimensional grid:- If the grid is arranged vertically,
GridLayoutManager
tries to make all the elements in each row have the same width and height, but different rows can have different heights. - If the grid is arranged horizontally,
GridLayoutManager
tries to make all the elements in each column have the same width and height, but different columns can have different widths.
- If the grid is arranged vertically,
- is similar to
GridLayoutManager
, but it does not require that items in a row have the same height (for vertical grids) or items in the same column have the same width (for horizontal grids). The result is that the items in a row or column can end up offset from each other.
You’ll also need to design the layout of the individual items. You’ll need this layout when you design the view holder, as described in the next section.
Implementing your adapter and view holder
Once you’ve determined your layout, you need to implement your Adapter
and ViewHolder
. These two classes work together to define how your data is displayed. The ViewHolder
is a wrapper around a View
that contains the layout for an individual item in the list. The Adapter
creates ViewHolder
objects as needed, and also sets the data for those views. The process of associating views to their data is called binding.
When you define your adapter, you need to override three key methods:
onCreateViewHolder()
:RecyclerView
calls this method whenever it needs to create a newViewHolder
. The method creates and initializes theViewHolder
and its associatedView
, but does not fill in the view’s contents — theViewHolder
has not yet been bound to specific data.onBindViewHolder()
:RecyclerView
calls this method to associate aViewHolder
with data. The method fetches the appropriate data and uses the data to fill in the view holder’s layout. For example, if theRecyclerView
dislays a list of names, the method might find the appropriate name in the list and fill in the view holder’sTextView
widget.getItemCount()
:RecyclerView
calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses.RecyclerView
uses this to determine when there are no more items that can be displayed.
Here’s a typical example of a simple adapter with a nested ViewHolder
that displays a list of data. In this case, the RecyclerView displays a simple list of text elements. The adapter is passed an array of strings, containing the text for the ViewHolder
elements.
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private String[] localDataSet; /** * Provide a reference to the type of views that you are using * (custom ViewHolder). */ public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textView; public ViewHolder(View view) { super(view); // Define click listener for the ViewHolder's View textView = (TextView) view.findViewById(R.id.textView); } public TextView getTextView() { return textView; } } /** * Initialize the dataset of the Adapter. * * @param dataSet String[] containing the data to populate views to be used * by RecyclerView. */ public CustomAdapter(String[] dataSet) { localDataSet = dataSet; } // Create new views (invoked by the layout manager) @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { // Create a new view, which defines the UI of the list item View view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.text_row_item, viewGroup, false); return new ViewHolder(view); } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder viewHolder, final int position) { // Get element from your dataset at this position and replace the // contents of the view with that element viewHolder.getTextView().setText(localDataSet[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return localDataSet.length; }}
The layout for the each view item is defined in an XML layout file, as usual. In this case, the app has a text_row_item.xml
file like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/list_item_height" android:layout_marginLeft="@dimen/margin_medium" android:layout_marginRight="@dimen/margin_medium" android:gravity="center_vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/element_text"/></FrameLayout>
Next steps
A simple RecyclerView
implementation like this may be all you need. However, the library also offers many ways to customize your implementation. For more information, see .
准备
IDE:
Android Studio 4.1.1Build #AI-201.8743.12.41.6953283, built on November 5, 2020Runtime version: 1.8.0_242-release-1644-b01 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.oWindows 10 10.0
Android Virtual Devices:
Name: Pixel_2_API_28CPU/ABI: Google Play Intel Atom (x86)Path: C:\Users\86188\.android\avd\Pixel_2_API_28.avdTarget: google_apis_playstore [Google Play] (API level 28)Skin: pixel_2SD Card: 512Mfastboot.chosenSnapshotFile: runtime.network.speed: fullhw.accelerometer: yeshw.device.name: pixel_2hw.lcd.width: 1080hw.initialOrientation: Portraitimage.androidVersion.api: 28tag.id: google_apis_playstorehw.mainKeys: nohw.camera.front: emulatedavd.ini.displayname: Pixel 2 API 28hw.gpu.mode: autohw.ramSize: 1536PlayStore.enabled: truefastboot.forceColdBoot: nohw.cpu.ncore: 4hw.keyboard: yeshw.sensors.proximity: yeshw.dPad: nohw.lcd.height: 1920vm.heapSize: 256skin.dynamic: yeshw.device.manufacturer: Googlehw.gps: yeshw.audioInput: yesimage.sysdir.1: system-images\android-28\google_apis_playstore\x86\showDeviceFrame: yeshw.camera.back: virtualsceneAvdId: Pixel_2_API_28hw.lcd.density: 420hw.arc: falsehw.device.hash2: MD5:55acbc835978f326788ed66a5cd4c9a7fastboot.forceChosenSnapshotBoot: nofastboot.forceFastBoot: yeshw.trackBall: nohw.battery: yeshw.sdCard: yestag.display: Google Playruntime.network.latency: nonedisk.dataPartition.size: 6442450944hw.sensors.orientation: yesavd.ini.encoding: UTF-8hw.gpu.enabled: yes
注意:以下示例仅在安卓虚拟设备上运行测试,并没有在真实的设备上运行测试。
项目
新建项目,选择 Empty Activity,在配置项目时,Minimum SDK
选择 API 16: Android 4.1 (Jelly Bean)
新建 src\main\res\layout\recycler_view_item.xml
布局文件,做为 RecyclerView
列表中的子项(item):
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="40dp" android:background="#FFFFFF"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="0dp" android:gravity="center_vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
新建 RecyclerViewAdapter
适配器类:
- 内部静态类
ViewHolder
继承自RecyclerView.ViewHolder
; - 内部静态类
ItemDecoration
继承自RecyclerView.ItemDecoration
; - 重写
onCreateViewHolder()
、onBindViewHolder()
,以及getItemCount()
三个方法。
package com.mk;import android.graphics.Rect;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.recyclerview.widget.RecyclerView;import java.util.List;/** * The Adapter creates ViewHolder objects as needed, * and also sets the data for those views. * The process of associating views to their data is called binding. */public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { private List<String> dataSet; private ViewHolder.OnClickListener listener; public RecyclerViewAdapter(List<String> dataSet) { this.dataSet = dataSet; } /** * RecyclerView calls this method whenever it needs to create a new ViewHolder. * The method creates and initializes the ViewHolder and its associated View, * but does not fill in the view's contents — the ViewHolder * has not yet been bound to specific data. * @param parent * @param viewType * @return */ @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Create a new view, which defines the UI of the list item View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.recycler_view_item, parent, false); return new ViewHolder(view); } /** * RecyclerView calls this method to associate a ViewHolder with data. * The method fetches the appropriate data * and uses the data to fill in the view holder's layout. * For example, if the RecyclerView dislays a list of names, * the method might find the appropriate name in the list * and fill in the view holder's TextView widget. * @param holder * @param position */ @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.binding(dataSet.get(position)); holder.setOnClickListener(listener); } /** * RecyclerView calls this method to get the size of the data set. * For example, in an address book app, this might be the total number of addresses. * RecyclerView uses this to determine when there are no more items that can be displayed. * @return */ @Override public int getItemCount() { return dataSet.size(); } public void setItemOnClickListener(ViewHolder.OnClickListener listener) { this.listener = listener; } /** * The ViewHolder is a wrapper around a View * that contains the layout for an individual item in the list. */ public static class ViewHolder extends RecyclerView.ViewHolder { private TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.textView); } public void binding(String data) { textView.setText(data); } public void setOnClickListener(OnClickListener listener) { if (listener != null) { textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onClick(v); } }); } } public interface OnClickListener { void onClick(View v); } } public static class ItemDecoration extends RecyclerView.ItemDecoration { @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.set(0, 0, 0, 2); } }}
编辑 src\main\res\layout\activity_main.xml
布局文件:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#BBBBBB" tools:context=".MainActivity"> <Button android:id="@+id/buttonAdd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:text="Add" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="16dp" android:background="#BBBBBB" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonAdd" /></androidx.constraintlayout.widget.ConstraintLayout>
编辑 MainActivity
文件:
package com.mk;import androidx.appcompat.app.AppCompatActivity;import androidx.recyclerview.widget.LinearLayoutManager;import androidx.recyclerview.widget.RecyclerView;import android.content.Context;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class MainActivity extends AppCompatActivity { private Context context; private RecyclerViewAdapter recyclerViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; // 初始化数据 List<String> data = new ArrayList<>(); // RecyclerView 适配器 recyclerViewAdapter = new RecyclerViewAdapter(data); recyclerViewAdapter.setItemOnClickListener(new RecyclerViewAdapter.ViewHolder.OnClickListener() { @Override public void onClick(View v) { String text = ((TextView) v).getText().toString(); Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } }); // 初始化 RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.addItemDecoration(new RecyclerViewAdapter.ItemDecoration()); recyclerView.setAdapter(recyclerViewAdapter); // 点击按钮添加数据,然后通知 RecyclerView 适配器插入数据 ((Button) findViewById(R.id.buttonAdd)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data.add(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS").format(new Date())); recyclerViewAdapter.notifyItemInserted(data.size()); } }); }}