官宣:A flexible view for providing a limited window into a large data set险污。
RecyclerView是在Android5.0版本作為support-v7的一個(gè)組件出現(xiàn),高效替代了最初的ListView等列表組件谦疾。使用上實(shí)現(xiàn)了視圖代碼解耦耸彪,功能上具備強(qiáng)大的item復(fù)用機(jī)制碌上,并且提供默認(rèn)多種LayoutMananger用來(lái)處理多種布局掀鹅,本篇介紹RecyclerView重要組件和基本使用揽碘。
1次屠、RecyclerView重要組件
- LayoutManager :管理視圖渲染、包含視圖復(fù)用回收等功能雳刺。本身是一個(gè)抽象類劫灶,系統(tǒng)已經(jīng)實(shí)現(xiàn)的子類有LinearLayoutManager、GridLayoutManager掖桦、StaggeredGridLayoutManager等本昏。
- Adapter: 適配器、數(shù)據(jù)和視圖對(duì)接的橋梁枪汪。
- ItemDecoration:裝飾抽象類涌穆,常常用來(lái)實(shí)現(xiàn)列表的分割線或者實(shí)現(xiàn)懸浮等功能怔昨。
- ItemAnimator:動(dòng)效抽象類,常用定制item動(dòng)畫(huà)需求宿稀。
2趁舀、基本使用
-
庫(kù)引入
目前RecyclerView所在的support被遷移到androidx中,所以引入庫(kù)的時(shí)候需要在app/build.gradle中引入祝沸,具體代碼如下:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
- 布局 activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
- item布局 rv_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:background="@color/colorAccent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- 適配器 MyAdapter
public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> dataList;
MyAdapter(List<String> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String content = dataList.get(position);
holder.tv.setText(content);
}
@Override
public int getItemCount() {
return dataList == null ? 0 : dataList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
ViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
}
- 在MainActvity中添加代碼
private MyAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
//設(shè)置LayoutManager矮烹,以LinearLayoutManager為例子進(jìn)行線性布局
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//設(shè)置分割線
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
//創(chuàng)建適配器
adapter = new MyAdapter(getArrayList());
//設(shè)置適配器
recyclerView.setAdapter(adapter);
}
//模擬數(shù)據(jù)
private List<String> getArrayList() {
List<String> dataList = new ArrayList<>();
for (int n = 0; n < 100; n++) {
dataList.add("item position = " + n);
}
return dataList;
}
-
最終運(yùn)行效果