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) {
        }
    }
}
相关推荐
wk灬丨23 分钟前
Android Kotlin Flow 冷流 热流
android·kotlin·flow
千雅爸爸24 分钟前
Android MVVM demo(使用DataBinding,LiveData,Fresco,RecyclerView,Room,ViewModel 完成)
android
晨曦_子画1 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
大福是小强1 小时前
005-Kotlin界面开发之程序猿初试Composable
kotlin·界面开发·桌面应用·compose·jetpack·可组合
孤客网络科技工作室1 小时前
AJAX 全面教程:从基础到高级
android·ajax·okhttp
Mr Lee_3 小时前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野3 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&3 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
Winston Wood5 小时前
Android Parcelable和Serializable的区别与联系
android·序列化
清风徐来辽5 小时前
Android 项目模型配置管理
android