Android開發中如何使用RecyclerView

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

建立一個新項目

  1. 打開Android Studio。轉到文件 - >「新建項目」,填寫應用程序名稱和其餘詳細信息,選擇API版本爲API 15選擇清空活動,在下一個屏幕中保留默認選項,而後單擊完成。將建立新項目以及所需文件。(在示例中,我將應用程序名稱指定爲「 SimpleRecyclerViewExample」並將包裝爲「 com.example
  2. 要使用RecyclerView,咱們須要添加recycleler視圖依賴項。打開build.gradle(Module:app)文件,並com.android.support:recyclerview-v7:26.1.0在dependencies部分添加如下「implementation' '」。單擊當即同步以同步更改並構建項目。
  3. 在佈局文件夾中打開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並指定了滾動條是垂直的。性能優化

建立模型類和佈局文件

  1. 咱們將在Recycler視圖中顯示足球隊相關信息咱們將建立一個用於存儲信息的模型類。建立一個類FootballTeam.java和建立變量NameLeagueYearEstablished
    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; } }
  2. 建立一個名爲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"          android:layout_below="@id/tvName" /> <TextView          android:id="@+id/tvYear"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          tools:text="1999"          android:layout_alignParentRight="true"           /> </RelativeLayout>

對於這個例子,我直接對填充和textSizeXML進行了硬編碼,但在單獨的XML文件中使用它是一個好主意。我使用了「 tools:Text」屬性來了解個人佈局在設計器中的外觀。運行應用程序時不會呈現相同的內容。app

建立適配器

如今咱們已經建立了Model項目佈局,如今是時候建立咱們的adapter類了。adapter班是負責建立ViewHolder對象和數據綁定到他們。ide

  1. 建立一個名爲MyAdapter.java的類,並輸入如下代碼:
    package com.example.simplerecyclerviewexample;
    
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { public List<FootballTeam> teamList; public MyAdapter(List<FootballTeam> footballTeamList) { teamList = footballTeamList; } public class ViewHolder extends RecyclerView.ViewHolder { TextView name, league, yearEstablished; public ViewHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.tvName); league = (TextView) itemView.findViewById(R.id.tvLeague); yearEstablished = (TextView) itemView.findViewById(R.id.tvYear); } } public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) { holder.name.setText(teamList.get(position).getName()); holder.league.setText(teamList.get(position).getLeague()); holder.yearEstablished.setText(String.valueOf   (teamList.get(position).getYearEstablished())); } @Override public int getItemCount() { return teamList.size(); } }

咱們的Adapter類擴展了RecyclerView.Adapter類並管理全部ViewHolder對象。咱們ViewHolder在適配器中定義了類,並TextViewViewHolder構造函數中初始化了函數

適配器負責ViewHolder根據須要建立對象。viewHolder當用戶滾動列表項時,它還建立了新的對象。咱們須要覆蓋三種重要的方法。佈局

「 onCreateViewHolder()」方法用於建立ViewHolder,咱們正在膨脹咱們以前在其中建立的佈局。性能

「 onBindViewHolder()」方法用於將數據綁定到ViewHolder它根據列表位置肯定須要顯示的數據並填充ViewHoldergradle

「 getItemCount()」方法返回的大小dataset在咱們的例子中,這將是該項目中的項目數量teamList

咱們還建立了一個構造函數,咱們將使用它來分配teamList咱們將經過咱們集合Mainactivity如今咱們已經建立了適配器,如今是將全部內容綁定在一塊兒的時候了。

主要活動變化

  1. 打開MainActivity.java並進行以下所示的更改:
    package com.example.simplerecyclerviewexample;
    
    import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { RecyclerView rvTeams; List<FootballTeam> teamList = new ArrayList<>(); MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rvTeams = (RecyclerView) findViewById(R.id.rvFootballTeamInfo); setTeamData(); adapter = new MyAdapter(teamList); RecyclerView.LayoutManager manager = new LinearLayoutManager(getApplicationContext()); rvTeams.setLayoutManager(manager); rvTeams.setAdapter(adapter); } private void setTeamData() { FootballTeam team = new FootballTeam("Manchester United", "English Premier League", 1878); teamList.add(team); team = new FootballTeam("Arsenal", "English Premier League", 1886); teamList.add(team); team = new FootballTeam("Liverpool", "English Premier League", 1892); teamList.add(team); team = new FootballTeam("Juventus", "Serie A", 1897); teamList.add(team); team = new FootballTeam("Real Madrid", "La Liga", 1902); teamList.add(team); team = new FootballTeam("Bayern Munich", "Bundesliga", 1900); teamList.add(team); team = new FootballTeam("PSG", "France Ligue 1", 1970); teamList.add(team); team = new FootballTeam("Ajax", "Eredivisie", 1900); teamList.add(team); } }

MainActivity.java文件中,咱們建立了對Recycler視圖的引用,而且咱們還爲Adapter和list建立了變量。咱們已經使用該setTeamData()方法來填充咱們ArrayList將要顯示的數據RecyclerView

在「 onCreate()」方法中,咱們正在建立一個LinearLayoutManager併爲咱們分配相同的方法RecyclerViewLayoutManager所使用的再循環器,以便在屏幕上每一個單獨的物品位置。咱們習慣於LinearLayoutManager水平放置物品。其餘類型的選項包括GridLayoutManagerStaggeredGridLayoutManager若是須要,咱們還能夠建立自定義佈局管理器。

設置佈局管理器後,咱們將自定義適配器分配給recycler視圖。在運行應用程序時,您將看到List以下圖所示的項目列表您還能夠滾動列表。RecyclerView還提供了指定動畫的不一樣選項,或者onclick若是但願列表中的項目可單擊而且您但願對列表項單擊執行某些操做,則能夠實現偵聽器。

如何在Android中使用RecyclerView

相關文章
相關標籤/搜索