天天看點

RecyclerView基本使用

部落格位址

背景

以前在展示清單時一直都是在使用ListView。

唯一的差別大概就是擴充卡Adapter的不同而已。

但是後來接觸到了RecyclerView之後,就喜歡上了RecyclerView。

是以本篇打算說下RecyclerView的基本使用,給大家參考下。

正所謂“學以緻用”,驗明是否掌握了一門東西的最好方法是能夠把這個東西講給别人聽,别人還能聽懂。

是以基于此,本篇文章應運而生。

RecyclerView的好處

這裡我就截取官網上的兩段文字來說明。

Many apps need to display user-interface elements based on large data sets, or data that frequently changes. For example, a music app might need to display information about thousands of albums, but only a dozen of those albums might be on-screen at a time. If the app created UI widgets for each of those albums, the app would end up using a lot of memory and storage, potentially making the app slow and crash-prone. On the other hand, if the app created UI widgets each time a new album scrolled onto the screen and destroyed the widgets when it scrolled off, that would also cause the app to run slowly, since creating UI objects is a resource-intensive operation.

To address this common situation, the Android Support Library provides the RecyclerView suite of objects. RecyclerView and its associated classes and interfaces help you to design and implement a dynamic user interface that runs efficiently. You can use these classes as they are, or customize them to suit your specific needs.

傳送門
A flexible view for providing a limited window into a large data set.

簡單的說RecyclerView在顯示大量資料的時候能起到高效的作用。

實戰

前面鋪墊了那麼多,接下來我們就實際看看一個最簡單的RecyclerView如何撰寫吧?

首先說明下RecyclerView的幾大組成部分:

  • RecyclerView本身
  • 擴充卡Adapter(下面的例子為MainAdapter)
  • ViewHolder(下面的例子為MainViewHolder)
  • Item布局(下面的例子為item_main.xml)

這四個組成部分依賴關系是Item->ViewHolder->Adapter->RecyclerView

好了,接下來就讓我們一步一步來實作一個展示文本的清單。

  1. 添加gradle依賴。在要使用的module的build.gradle檔案中加入依賴
compile 'com.android.support:recyclerview-v7:26.+'
           
  1. 确定清單布局item_main.xml。這裡清單展示的就是一個文本,是以隻需要一個TextView即可。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_title"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_50"
    android:gravity="center"
    >
</TextView>
           
  1. 建立清單布局的ViewHolder,繼承自RecyclerView.ViewHolder,并初始化清單中的控件。
public class MainViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.txt_title)
    TextView mTxtTitle;

    public MainViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

}
           

這裡使用了

butterknife

對TextView進行初始化。

  1. 建立擴充卡Adapter,繼承自RecyclerView.Adapter<MainViewHolder> 。将資料源和布局檔案通過Adapter聯系起來。
public class MainAdapter extends RecyclerView.Adapter<MainViewHolder> {

    private Activity activity;
    private List<String> itemList;

    public MainAdapter(Activity activity, List<String> itemList) {
        if (activity == null || itemList == null) {
            throw new IllegalArgumentException("params can't be null");
        }
        this.activity = activity;
        this.itemList = itemList;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.mTxtTitle.setText(itemList.get(position));
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}
           

這裡說明下幾個方法:

  • 構造函數。用于對資料源進行初始化,以及傳遞activity進來。
  • onCreateViewHolder。使用item布局來初始化MainViewHolder。
  • onBindViewHolder。通過MainViewHolder擷取到具體控件執行對應邏輯。
  • getItemCount。傳回資料源的大小。
  1. 建立RecyclerView并進行初始化。

在使用RecyclerView的布局檔案中加入下面代碼:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />
           

初始化RecyclerView并将其與擴充卡綁定。

由于這裡使用了butterknife,是以建立的時候就初始化了。

@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
private void initRecyclerView() {
        //設定子視圖
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //添加分割線
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        //設定擴充卡
        mAdapter = new MainAdapter(this, mItemList);
        mRecyclerView.setAdapter(mAdapter);
    }
           

如此,一個簡單的RecycleView就完成了。

心動不如行動,趕緊自己去敲下代碼熟悉下呗~

代碼傳送門

繼續閱讀