欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

RecyclerView使用payload实现局部刷新

程序员文章站 2022-07-05 21:42:37
本文实例为大家分享了recyclerview使用payload实现局部刷新的具体代码,供大家参考,具体内容如下列表局部刷新:01.notifydatasetchanged() 刷新全部可见的item0...

本文实例为大家分享了recyclerview使用payload实现局部刷新的具体代码,供大家参考,具体内容如下

列表局部刷新:

01.notifydatasetchanged() 刷新全部可见的item
02.notifyitemchanged(int position) 更新列表position位置上的数据可以调用
03.notifyiteminserted(int position) 列表position位置添加一条数据时可以调用,伴有动画效果
04.notifyitemremoved(int position) 列表position位置移除一条数据时调用,伴有动画效果
05.notifyitemmoved(int fromposition, int toposition) 列表fromposition位置的数据移到toposition位置时调用,伴有动画效果
06.notifyitemrangechanged(int positionstart, int itemcount) 列表从positionstart位置到itemcount数量的列表项进行数据刷新
07.notifyitemrangeinserted(int positionstart, int itemcount) 列表从positionstart位置到itemcount数量的列表项批量添加数据时调用,伴有动画效果
08.notifyitemrangeremoved(int positionstart, int itemcount) 列表从positionstart位置到itemcount数量的列表项批量删除数据时调用,伴有动画效果

一、payload、notifyitemchanged()实现局部刷新:

1.在适配器中定义onbindviewholder(holder: viewholder, position: int, payloads: mutablelist)方法:

class newsadapter : listadapter<data, newsadapter.viewholder>(diff()) {

    //构建listview的数据比较结果
    class diff : diffutil.itemcallback<data>() {
        override fun areitemsthesame(olditem: data, newitem: data): boolean {
            return olditem.hashid == newitem.hashid
        }

        override fun arecontentsthesame(olditem: data, newitem: data): boolean {
            return olditem.content == newitem.content
        }
    }

    inner class viewholder(view: view) : recyclerview.viewholder(view) {
        val tvcontent: textview = view.findviewbyid(r.id.tvcontent)
        var tvplay: textview = view.findviewbyid(r.id.tvplay)
        var tvplay1: textview = view.findviewbyid(r.id.tvplay1)
        var tvplay2: textview = view.findviewbyid(r.id.tvplay2)
    }

    override fun oncreateviewholder(parent: viewgroup, viewtype: int): viewholder {
        val view = layoutinflater.from(parent.context).inflate(r.layout.layout_joke_list_item, parent, false)
        return viewholder(view)
    }

    override fun onbindviewholder(holder: viewholder, position: int) {
        holder.tvcontent.text = getitem(position).content
        holder.tvplay.text = "播放"
        holder.tvplay1.text = "播放"
        holder.tvplay2.text = "播放"
    }

    //局部刷新item
    override fun onbindviewholder(holder: viewholder, position: int, payloads: mutablelist<any>) {
        if(payloads.isempty()) {
            onbindviewholder(holder, position)
        } else {
            for (i in 0 until payloads.size) {
                when(payloads[i].tostring()) {
                    "aaa" -> {
                        holder.tvcontent.text = "000"
                    }
                    "bbb" -> {
                        holder.tvplay.text = "222"
                    }
                }
            }
        }
    }
}

2.使用notifyitemchanged()进行局部刷新:

class mainactivity : appcompatactivity() {

    private lateinit var recycler: recyclerview
    private lateinit var madapter: newsadapter

    val data = listof(
        data("123", "123", 1, "123"),
        data("456", "456", 1, "456"),
        data("789", "789", 1, "789")
    )

    override fun oncreate(savedinstancestate: bundle?) {
        super.oncreate(savedinstancestate)
        setcontentview(r.layout.activity_main)
        recycler = findviewbyid(r.id.recycler)
        madapter = newsadapter()
        val layoutmanager = linearlayoutmanager(this)
        recycler.layoutmanager = layoutmanager
        recycler.adapter = madapter
        madapter.submitlist(data)
        //点击局部刷新
        findviewbyid<button>(r.id.btn).setonclicklistener {
            madapter.notifyitemchanged(2, "aaa")
            madapter.notifyitemchanged(0, "aaa")
            madapter.notifyitemchanged(1, "aaa")
            madapter.notifyitemchanged(2, "bbb")
            madapter.notifyitemchanged(0, "bbb")
            madapter.notifyitemchanged(1, "bbb")
        }

    }
}

3.mainactivity布局文件:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".mainactivity">

    <button
        android:id="@+id/btn"
        android:text="局部刷新"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <androidx.recyclerview.widget.recyclerview
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</linearlayout>

4.列表item布局文件:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <androidx.cardview.widget.cardview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/white">

        <linearlayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="8dp">

            <textview
                android:id="@+id/tvcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textcolor="@android:color/black"
                android:textstyle="bold" />

            <textview
                android:id="@+id/tvplay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:text="哈哈" />

            <textview
                android:id="@+id/tvplay1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:text="哈哈" />

            <textview
                android:id="@+id/tvplay2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="end"
                android:text="哈哈" />

        </linearlayout>

    </androidx.cardview.widget.cardview>

</relativelayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: RecyclerView 刷新