介绍 在Android应用程序中,只要您想显示数据列表,就可使用 RecyclerView 。 早期的Android提供 ListView 了一样的东西。 RecyclerView 能够被认为是一个更高级和性能优化的版本 ListView 。 顾名思义, RecyclerView 也可使用 ViewHolder 模式 回收物java
在Android应用程序中,只要您想显示数据列表,就可使用RecyclerView
。早期的Android提供ListView
了一样的东西。RecyclerView
能够被认为是一个更高级和性能优化的版本ListView
。顾名思义,RecyclerView
也可使用ViewHolder
模式回收物品。除此以外,它还使用了一个LayoutManager
更灵活的方法来建立水平,垂直甚至交错的列表。它还提供垂直和水平滚动以及项目动画。虽然ListView
也能够修改成使用' ViewHolder
'模式,RecyclerView
强制使用相同的模式。它甚至能够处理大量数据,同时提供响应式界面。android
在这篇文章中,咱们将看到如何RecyclerView
使用Android Studio在Android应用程序中实现一个简单的。我将使用Android studio版本。git
SimpleRecyclerViewExample
”并将包装为“ com.example
”RecyclerView
,咱们须要添加recycleler视图依赖项。打开build.gradle(Module:app)文件,并com.android.support:recyclerview-v7:26.1.0
在dependencies部分添加如下“implementation' '”。单击当即同步以同步更改并构建项目。<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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" tools:context="com.example.simplerecyclerviewexample.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rvFootballTeamInfo" android:scrollbars="vertical" ></android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>
对于Recycler
视图,咱们提供了一个id并指定了滚动条是垂直的。性能优化
Recycler
视图中显示足球队相关信息。咱们将建立一个用于存储信息的模型类。建立一个类FootballTeam.java和建立变量Name
,League
和YearEstablished
。
package com.example.simplerecyclerviewexample;
public class FootballTeam { private String name; private String league; private int yearEstablished; public FootballTeam() { } public FootballTeam(String name, String league, int yearEstablished) { this.name = name; this.league = league; this.yearEstablished = yearEstablished; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLeague() { return league; } public void setLeague(String league) { this.league = league; } public int getYearEstablished() { return yearEstablished; } public void setYearEstablished(int yearEstablished) { this.yearEstablished = yearEstablished; } }
RelativeLayout
包含三个文本视图的简单视图,用于显示已创建的名称,联盟和年份。布局的完整XML以下所示:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" > <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" tools:text="Team Name" android:textSize="20dp" android:textStyle="bold" /> <TextView android:id="@+id/tvLeague" android:layout_width="match_parent" android:layout_height="wrap_content" tools:text="Team League"