Android RecyclerView AsyncListDiffer DiffUtil,Kotlin(b)

Android RecyclerView AsyncListDiffer DiffUtil,Kotlin(b)

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 java.util.UUID
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 newList = mutableListOf<MyItem>()
        for (i in 0 until ITEM_COUNT) {
            val item = MyItem()
            item.pos = i
            item.change = Random.Default.nextBoolean()
            item.data = System.currentTimeMillis()
            item.contentId = UUID.randomUUID()
            newList.add(item)
        }
        mAdapter?.onChange(newList)
    }

    class MyAdapter : RecyclerView.Adapter<MyVH> {
        private val 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.contentId == newItem.contentId
            }

            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(i)
                item.data = 0L
                item.change = false
                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 = "data=${mDiffer.currentList[position].data}"

            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 pos = 0
        var data = 0L
        var contentId = UUID.randomUUID()
        var change = false

        constructor(pos: Int) {
            this.pos = pos
        }

        constructor() : this(0) {
        }
    }
}
相关推荐
每次的天空8 小时前
Android学习总结之算法篇四(字符串)
android·学习·算法
x-cmd9 小时前
[250331] Paozhu 发布 1.9.0:C++ Web 框架,比肩脚本语言 | DeaDBeeF 播放器发布 1.10.0
android·linux·开发语言·c++·web·音乐播放器·脚本语言
高林雨露10 小时前
Java 与 Kotlin 对比示例学习(三)
java·kotlin
lc99910210 小时前
基于kotlin native的C与kotlin互相调用
开发语言·kotlin
tangweiguo0305198712 小时前
Android BottomNavigationView 完全自定义指南:图标、文字颜色与选中状态
android
遥不可及zzz13 小时前
Android 应用程序包的 adb 命令
android·adb
无知的前端13 小时前
Flutter 一文精通Isolate,使用场景以及示例
android·flutter·性能优化
_一条咸鱼_13 小时前
Android Compose 入门之字符串与本地化深入剖析(五十三)
android
ModestCoder_14 小时前
将一个新的机器人模型导入最新版isaacLab进行训练(以unitree H1_2为例)
android·java·机器人
robin_suli15 小时前
Spring事务的传播机制
android·java·spring