Android開發中如何使用RecyclerView
阿新 • • 發佈:2019-03-01
ins 初始化 find count target otto 例子 並且 詳細信息
介紹 在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版本。
創建一個新項目
- 打開Android Studio。轉到文件 - >“新建項目”
SimpleRecyclerViewExample
”並將包裝為“com.example
” - 要使用
RecyclerView
,我們需要添加recycleler視圖依賴項。打開build.gradle(Module:app)文件,並com.android.support:recyclerview-v7:26.1.0
在dependencies部分添加以下“implementation‘ - 在布局文件夾中打開activity_main.xml並添加Recycler View小部件。更改後,XML文件應如下所示:
<?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; } }
- 創建一個名為row.xml的布局文件。此布局將用於在回收站視圖中顯示每條記錄。我們將創建一個
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"