RecyclerView概述
RecyclerView是一个增强版的ListView,可以轻松实现ListView同样的效果,还优化了ListView的不足
首先根据书中所说 新建一个RecyclerView项目,并让Android studio自动帮我们创建好活动。
RecycleView使用
和百分比布局相似,RecyclerView属于新增控件,为了让RecyclerView在Android所有版本上可以使用,将RecentlyView定义在库中,想要使用的话,首先得在项目下的Build.gradle中添加依赖库
第一步 添加依赖
打开app/build.gradle文件,在dependencies闭包中添加依赖
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
添加完之后要点击一下Sync Now进行同步。
第二部 添加控件
然后在activity_main.xml中添加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="match_parent"
tools:context=".MainActivity">
//因为RecyclerView不是内置在SDK中的,因此需要把完整的包路径写出来
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>