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

相关推荐
m0_548514773 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯3 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯3 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐4 小时前
Handle
android
m0_748232925 小时前
Android Https和WebView
android·网络协议·https
m0_748251725 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
m0_748254667 小时前
go官方日志库带色彩格式化
android·开发语言·golang
zhangphil7 小时前
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现“刮刮乐”效果,Kotlin(2)
android·kotlin
爱学测试的李木子8 小时前
从0到1搭建 Android 自动化 python+appium 环境
android·软件测试·python·测试工具·自动化
咸芝麻鱼8 小时前
Android Studio | 连接手机设备后,启动App时出现:Waiting For DebuggerApplication (App名)...
android·adb·智能手机·android studio