概念
什么是recyclerView?我们常见的可以滑动的,分块的视图样式就可以认为是recyclerView。
比如:
在开发前还需要了解一些概念:
ViewHolder:可以看到recyclerView是以分块的视图形式组织的。分块无论其形状,里面的内容如何,每一个分快称之为ViewHolder。上图中一个title+subtitle的条形分块就是一个ViewHolder。
Adapter:可以确认的是,recyclerView一定是有数据填充的,也一定有一个工具性质的东西能够将数据正确地和每一个ViewHolder绑定,进而渲染其内容。实现这个流程的工具就是Adapter。
知道以上概念之后就可以开发了。
1.创建recylerView视图与对象
首先在需要展示的view里创建一个recyclerView:
<?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="wrap_content"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
随后在代码中获取其对象: