介绍 在Android应用程序中,只要您想显示数据列表,就可以使用 RecyclerView 。 早期的Android提供 ListView 了同样的东西。 RecyclerView 可以被认为是一个更高级和性能优化的版本 ListView 。 顾名思义, RecyclerView 也可以使用 ViewHolder 模式 回收物
在Android应用程序中,只要您想显示数据列表,就可以使用RecyclerView
。早期的Android提供ListView
了同样的东西。RecyclerView
可以被认为是一个更高级和性能优化的版本ListView
。顾名思义,RecyclerView
也可以使用ViewHolder
模式回收物品。除此之外,它还使用了一个LayoutManager
更灵活的方法来创建水平,垂直甚至交错的列表。它还提供垂直和水平滚动以及项目动画。虽然ListView
也可以修改为使用‘ ViewHolder
‘模式,RecyclerView
强制使用相同的模式。它甚至可以处理大量数据,同时提供响应式界面。
在这篇文章中,我们将看到如何RecyclerView
使用Android Studio在Android应用程序中实现一个简单的。我将使用Android studio版本。
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"