Android-RecycleView的使用

一、RecycleView简述

RecyclerView 是 Android 中一个非常强大和灵活的用于展示大量数据集的 UI 组件,它是ListViewGirdView的高级和改进版。RecyclerView 是展示列表数据的首选方式,因为它提供了更好的性能和更多的功能。

主要特点:

  1. 布局灵活性RecyclerView 支持不同的布局管理器,如 LinearLayoutManager(线性布局)、GridLayoutManager(网格布局)和 StaggeredGridLayoutManager(瀑布流布局),使得列表可以以多种形式展示。

  2. 性能优化RecyclerView 通过重用(回收)不再显示的视图(或称为条目、项目)来减少视图创建的数量,从而提高性能,特别是在滚动列表时。

  3. 动画支持RecyclerView 提供了默认的项目动画,并且可以自定义动画,使得添加、移除和排序列表项时可以有更加平滑的动画效果。

  4. 适配器分离RecyclerView 强制使用 RecyclerView.Adapter 类来分离数据集与视图的表示,这样可以更容易地扩展和修改数据集。

  5. 视图持有者模式RecyclerView 使用 RecyclerView.ViewHolder 模式来缓存视图信息,减少了 findViewById() 的调用次数,提高了滚动和动态加载的效率。

  6. 项目装饰RecyclerView 允许开发者通过 RecyclerView.ItemDecoration 类添加边界或分隔线,而不需要修改项目视图。

  7. 项目动画RecyclerView.ItemAnimator 类提供了处理项目动画的方法,可以自定义项目添加、删除和移动时的动画效果。

  8. 滚动监听RecyclerView 提供了 RecyclerView.OnScrollListener,允许监听滚动事件,这对于实现例如“上拉加载更多”等功能非常有用。

使用步骤:

  1. 布局文件中定义:在主要布局文件中添加 RecyclerView 控件。

  2. 创建适配器:创建一个扩展自 RecyclerView.Adapter 的适配器类,用于将数据绑定到视图。

  3. 创建视图持有者:在适配器类中创建一个扩展自 RecyclerView.ViewHolder 的内部类,用于缓存视图。

  4. 设置布局管理器:在代码中为 RecyclerView 设置一个布局管理器。

  5. 设置适配器:将适配器实例设置到 RecyclerView 控件上。

  6. (可选)添加装饰、动画和监听器:根据需要为 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值