背景
以前在展示列表時一直都是在使用ListView。
唯一的區(qū)別大概就是適配器Adapter的不同而已剂习。
但是后來接觸到了RecyclerView之后,就喜歡上了RecyclerView。
因此本篇打算說下RecyclerView的基本使用业栅,給大家參考下冈欢。
正所謂“學以致用”歉铝,驗明是否掌握了一門東西的最好方法是能夠把這個東西講給別人聽,別人還能聽懂凑耻。
所以基于此太示,本篇文章應(yīng)運而生。
RecyclerView的好處
這里我就截取官網(wǎng)上的兩段文字來說明香浩。
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在顯示大量數(shù)據(jù)的時候能起到高效的作用类缤。
實戰(zhàn)
前面鋪墊了那么多,接下來我們就實際看看一個最簡單的RecyclerView如何撰寫吧邻吭?
首先說明下RecyclerView的幾大組成部分:
- RecyclerView本身
- 適配器Adapter(下面的例子為MainAdapter)
- ViewHolder(下面的例子為MainViewHolder)
- Item布局(下面的例子為item_main.xml)
這四個組成部分依賴關(guān)系是Item->ViewHolder->Adapter->RecyclerView
好了餐弱,接下來就讓我們一步一步來實現(xiàn)一個展示文本的列表。
- 添加gradle依賴囱晴。在要使用的module的build.gradle文件中加入依賴
compile 'com.android.support:recyclerview-v7:26.+'
- 確定列表布局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>
- 創(chuàng)建列表布局的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進行初始化论笔。
- 創(chuàng)建適配器Adapter,繼承自RecyclerView.Adapter<MainViewHolder> 破停。將數(shù)據(jù)源和布局文件通過Adapter聯(lián)系起來翅楼。
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();
}
}
這里說明下幾個方法:
- 構(gòu)造函數(shù)。用于對數(shù)據(jù)源進行初始化真慢,以及傳遞activity進來毅臊。
- onCreateViewHolder。使用item布局來初始化MainViewHolder黑界。
- onBindViewHolder管嬉。通過MainViewHolder獲取到具體控件執(zhí)行對應(yīng)邏輯。
- getItemCount朗鸠。返回數(shù)據(jù)源的大小蚯撩。
- 創(chuàng)建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胎挎,所以創(chuàng)建的時候就初始化了沟启。
@BindView(R.id.recycler_view)
RecyclerView mRecyclerView;
private void initRecyclerView() {
//設(shè)置子視圖
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//添加分割線
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//設(shè)置適配器
mAdapter = new MainAdapter(this, mItemList);
mRecyclerView.setAdapter(mAdapter);
}
如此,一個簡單的RecycleView就完成了犹菇。
心動不如行動德迹,趕緊自己去敲下代碼熟悉下唄~
代碼傳送門