簡體   English   中英

當視圖更新 Android Kotlin 時,DiffUtil 不能與 ListAdpater 一起使用

[英]DiffUtil Not working with ListAdpater when view is updating Android Kotlin

嘿,我有ReyclerviewDiffUtill使用ListAdapter 我通過submitList函數添加了元素。 但是在更新列表視圖時不會重繪元素。 直到我再次使用notifyDataSetChanged()或設置適配器 那么DiffUtill的用例是什么 當項目在列表和 reyclerview 中更新時重繪項目的正確方法是什么。

主要活動

class MainActivity : AppCompatActivity() {

    private var list = mutableListOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    private lateinit var binding: ActivityMainBinding
    var i = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Log.e("List", " $list")
        val intAdapter = IntAdapter()
        binding.recylerview.adapter = intAdapter
        intAdapter.submitList(list)
        binding.button.setOnClickListener {
            list.add(++i)
            intAdapter.submitList(list)
//            binding.recylerview.adapter = intAdapter
//            intAdapter.notifyDataSetChanged()
        }
    }
}

內部適配器

class IntAdapter : ListAdapter<Int, IntViewHolder>(comparator) {

    companion object {
        private val comparator = object : DiffUtil.ItemCallback<Int>() {
            override fun areItemsTheSame(oldItem: Int, newItem: Int): Boolean {
                return oldItem == newItem
            }

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IntViewHolder {
        return IntViewHolder(
            IntLayoutBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: IntViewHolder, position: Int) {
        holder.bindItem(getItem(position))
    }

}

IntViewHolder

class IntViewHolder(val binding: IntLayoutBinding) : RecyclerView.ViewHolder(binding.root) {

    fun bindItem(item: Int?) {
        binding.intNumber.text = item.toString()
    }

}

活動_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recylerview"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

int_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/intNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

您需要確保每次調用submitList時都傳遞不同的 List 實例。 如果您傳遞一個 List,對其進行變異,然后再次傳遞同一個 List 實例,DiffUtil 只會將同一個列表與其自身進行比較,因此它會假設列表中沒有任何更改,並且不會更新任何內容。 當您第一次提交 List 時,它沒有某種記憶。

為了進一步概括,您根本不能將可變 List 與 ListAdapter 一起使用。 ListAdapter 假定您傳遞給submitList的列表不會更改。 如果你改變它,可能會有意想不到的錯誤。

在您的代碼中解決此問題的兩種方法。

  1. 每次傳遞列表時創建一個只讀副本:

    intAdapter.submitList(list.toList())

  2. 根本不要使用 MutableList。 每次修改列表中應包含的內容時,都會創建一個新列表。 這是更簡單、不易出錯的解決方案。

class MainActivity : AppCompatActivity() {

    private var list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
    private lateinit var binding: ActivityMainBinding
    var i = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        Log.e("List", " $list")
        val intAdapter = IntAdapter()
        binding.recylerview.adapter = intAdapter
        intAdapter.submitList(list)
        binding.button.setOnClickListener {
            list += ++i // create a new list from old list contents plus a new item
            intAdapter.submitList(list)
        }
    }
}

旁注:當您將varMutable____結合使用Mutable____ ,這應該是一個Mutable____信號。 您需要兩種不同的方式來改變某些東西的情況應該很少見。 那很容易出錯。

您需要提交一個新的List ,目前您正在提交已發生變異的同一個List

在您的點擊偵聽器中嘗試以下操作:

binding.button.setOnClickListener {
    val current = intAdapter.currentList
    val update = current + (current.size + 1) // returns a new list with added value
    intAdapter.submitList(update)
}

具有此邏輯的示例:

例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM