Android RecyclerView AsyncListDiffer DiffUtil,Kotlin(a)

Android RecyclerView AsyncListDiffer DiffUtil,Kotlin(a)

Kotlin 复制代码
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.random.Random

const val ITEM_COUNT = 30

class MainActivity : AppCompatActivity() {
    private var mAdapter: MyAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rv = findViewById<RecyclerView>(R.id.rv)
        val layoutManager = GridLayoutManager(this, 3)
        rv.layoutManager = layoutManager

        mAdapter = MyAdapter()
        rv.adapter = mAdapter
    }

    override fun onResume() {
        super.onResume()

        val list = mutableListOf<MyItem>()
        for (i in 0 until ITEM_COUNT) {
            val item = MyItem()
            item.pos = i
            item.change = Random.Default.nextBoolean()
            item.time = System.currentTimeMillis()
            list.add(item)
        }

        mAdapter?.onChange(list)
    }

    class MyAdapter : RecyclerView.Adapter<MyVH> {
        private var mDiffer = AsyncListDiffer(this, object : DiffUtil.ItemCallback<MyItem>() {
            override fun areItemsTheSame(oldItem: MyItem, newItem: MyItem): Boolean {
                return oldItem.pos == newItem.pos
            }

            override fun areContentsTheSame(oldItem: MyItem, newItem: MyItem): Boolean {
                return oldItem == newItem
            }

            override fun getChangePayload(oldItem: MyItem, newItem: MyItem): Any? {
                return super.getChangePayload(oldItem, newItem)
            }
        })

        constructor() {
            val list = mutableListOf<MyItem>()
            for (i in 0 until ITEM_COUNT) {
                val item = MyItem()
                item.pos = i
                item.change = false
                item.time = 0L
                list.add(item)
            }

            onChange(list)
        }

        fun onChange(list: MutableList<MyItem>) {
            mDiffer.submitList(list)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {
            val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_2, parent, false)
            return MyVH(view)
        }

        override fun getItemCount(): Int {
            return mDiffer.currentList.size
        }

        override fun onBindViewHolder(holder: MyVH, position: Int) {
            holder.text1.text = "pos=${mDiffer.currentList[position].pos}"
            holder.text2.text = "time=${mDiffer.currentList[position].time}"

            if (mDiffer.currentList[position].change) {
                holder.text1.setTextColor(Color.RED)
                holder.text2.setTextColor(Color.RED)
            } else {
                holder.text1.setTextColor(Color.BLACK)
                holder.text2.setTextColor(Color.BLACK)
            }
        }
    }

    class MyVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val text1: TextView = itemView.findViewById(android.R.id.text1)
        val text2: TextView = itemView.findViewById(android.R.id.text2)
    }

    class MyItem {
        var change = false
        var pos = 0
        var time = 0L

        override fun equals(other: Any?): Boolean {
            return pos == (other as MyItem).pos && time == other.time
        }
    }
}

Android RecyclerView AsyncListUtil手动刷新fillData,kotlin_recycler refresh data-CSDN博客文章浏览阅读410次。基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。【代码】Android Paging 3,kotlin(1)_recycler refresh datahttps://blog.csdn.net/zhangphil/article/details/131519811

相关推荐
Yang-Never3 小时前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
XeonYu4 小时前
Kotlin 协程之 突破 Flow 限制:Channel 与 Flow 的结合之道
kotlin·coroutine·channelflow·callbackflow·receiveasflow·consumeasflow
XeonYu8 小时前
Kotlin 协程之 Flow 的理解使用及源码解析
kotlin·flow·coroutine
一笑的小酒馆8 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺11 小时前
Android BLE 扫描完整实战
android
TeleostNaCl13 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952714 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
低调小一14 小时前
Swift 语法学习指南 - 与 Kotlin 对比
微信·kotlin·swift
2501_9159184115 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong95115 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone