一、RecyclerView 刷新分类
- 整体刷新:刷新整个列表,整个列表重新绑定,数据全部变化,一般采用 notifyDataSetChanged() 函数来实现。
- 多行刷新:刷新列表的连续几行,刷新的几行数据进行重新绑定,一般调用 notifyItemRangeChanged(start, count) 函数来实现。
- 单行刷新:刷新列表的指定行,该行数据进行重新绑定,一般调用 notifyItemChanged(position) 函数来实现。
- 单行局部刷新:刷新指定行的局部数据,只有局部数据进行重新绑定,一般调用 notifyItemChanged(position, payload) 函数来实现,其中 payload 为局部控件的 id 值。
二、实现局部刷新
1、数据模型
package com.android.recycleview.modelimport androidx.annotation.DrawableResdata class UserModel(val userName: String,val age: Int,@DrawableRes val photo: Int,val isEnable: Boolean
)
2、布局文件
(1)item_user_info.xm
- 列表项布局,局部刷新 CheckBox 控件(id 为 enable_check)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"android:paddingHorizontal="@dimen/dp_20"android:background="@color/gray"><TextViewandroid:id="@+id/name_text"android:layout_width="@dimen/dp_50"android:layout_height="wrap_content"android:textSize="@dimen/sp_20"android:textColor="@color/black"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"android:layout_marginStart="@dimen/dp_10"/><TextViewandroid:id="@+id/age_text"android:layout_width="@dimen/dp_50"android:layout_height="wrap_content"android:textSize="@dimen/sp_20"android:textColor="@color/black"app:layout_constraintTop_toTopOf="@+id/name_text"app:layout_constraintLeft_toRightOf="@+id/name_text"/><ImageViewandroid:id="@+id/photo_image"android:layout_width="@dimen/dp_20"android:layout_height="@dimen/dp_20"android:src="@mipmap/photo_icon"app:layout_constraintTop_toTopOf="@+id/name_text"app:layout_constraintBottom_toBottomOf="@+id/name_text"app:layout_constraintLeft_toRightOf="@+id/age_text"tools:ignore="ContentDescription" /><CheckBoxandroid:id="@+id/enable_check"android:layout_width="wrap_content"android:layout_height="wrap_content"app