一、RecycleView简述
RecyclerView
是 Android 中一个非常强大和灵活的用于展示大量数据集的 UI 组件,它是ListView
和GirdView
的高级和改进版。RecyclerView
是展示列表数据的首选方式,因为它提供了更好的性能和更多的功能。
主要特点:
-
布局灵活性:
RecyclerView
支持不同的布局管理器,如LinearLayoutManager
(线性布局)、GridLayoutManager
(网格布局)和StaggeredGridLayoutManager
(瀑布流布局),使得列表可以以多种形式展示。 -
性能优化:
RecyclerView
通过重用(回收)不再显示的视图(或称为条目、项目)来减少视图创建的数量,从而提高性能,特别是在滚动列表时。 -
动画支持:
RecyclerView
提供了默认的项目动画,并且可以自定义动画,使得添加、移除和排序列表项时可以有更加平滑的动画效果。 -
适配器分离:
RecyclerView
强制使用RecyclerView.Adapter
类来分离数据集与视图的表示,这样可以更容易地扩展和修改数据集。 -
视图持有者模式:
RecyclerView
使用RecyclerView.ViewHolder
模式来缓存视图信息,减少了findViewById()
的调用次数,提高了滚动和动态加载的效率。 -
项目装饰:
RecyclerView
允许开发者通过RecyclerView.ItemDecoration
类添加边界或分隔线,而不需要修改项目视图。 -
项目动画:
RecyclerView.ItemAnimator
类提供了处理项目动画的方法,可以自定义项目添加、删除和移动时的动画效果。 -
滚动监听:
RecyclerView
提供了RecyclerView.OnScrollListener
,允许监听滚动事件,这对于实现例如“上拉加载更多”等功能非常有用。
使用步骤:
-
布局文件中定义:在主要布局文件中添加
RecyclerView
控件。 -
创建适配器:创建一个扩展自
RecyclerView.Adapter
的适配器类,用于将数据绑定到视图。 -
创建视图持有者:在适配器类中创建一个扩展自
RecyclerView.ViewHolder
的内部类,用于缓存视图。 -
设置布局管理器:在代码中为
RecyclerView
设置一个布局管理器。 -
设置适配器:将适配器实例设置到
RecyclerView
控件上。 -
(可选)添加装饰、动画和监听器:根据需要为
RecyclerView
添加项目装饰、动画和滚动监听器。
二、RecycleViewDemo1-单类型(重复)的布局
1.activity_main.xml中建立RecycleView控件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecycleView的基础布局如上所示,RecycleView的特点中适配器分离,简单理解为RecycleView的布局和内容布局是分开的,内容布局需要适配器来处理, 以分离数据集与视图的表示。
2.新建多个layout作为RecycleView中的数据布局样式RecycleView_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
android:layout_height="wrap_content" 属性最好是随内容变化。
3.新建drawable下的背景文件mybackground:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black"/>
<corners android:radius="8dp"/>
</shape>
这里设置的背景为黑色,8dp的圆角。
可将背景添加到RecycleView_item的布局中,根据需要进行选择。
4.在RecycleView_item中添加各种控件进行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="8dp"
android:background="@drawable/item_background"
android:layout_marginBottom="10dp"
>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:layout_marginStart="25dp"
android:layout_marginBottom="5dp"
tools:ignore="DuplicateIds"
android:textColor="@color/white"/>
<TextView
android:id="@+id/item_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="14sp"
android:layout_marginTop="5dp"
android:layout_marginStart="25dp"
android:layout_marginBottom="5dp"
android:textColor="@color/white"/>
</LinearLayout>
<!--给LinearLayout内部添加分割线-->
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginStart="25dp"
android:background="@drawable/custom_divider"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="8dp"
android:background="@drawable/item_background">
<TextView
android:id="@+id/tv_1"
android:layout_width="m