介紹 在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"