希望有人可以帮助我。您将如何将 diffutils 用于具有对象列表的对象。因此,举个简单的例子,可能没有意义,但可以得到一个更好的主意。
data class Exercise(
val name: String,
var sets: String,
var bodyTarget: String,
var weight: String,
)
data class Workout(
private val workOutId: String,
private val date: String,
private val exercises: MutableList<Exercise>
)
我的适配器数据是锻炼列表。所以我不确定我会如何比较健身课上的练习。我想我需要在 areContentsTheSame 中一一比较练习?或者有没有更简单的方法来完成这个
class DiffUtilSample(
private val oldList: List<Workout>,
private val newList: List<Workout>
) : DiffUtil.Callback(){
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
oldList[oldItemPosition].workOutId == newList[newItemPosition].workOutId
override fun getOldListSize(): Int = oldList.size
override fun getNewListSize(): Int = newList.size
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
//Compare the list of exercises from each workout one by one? So for example I'd compare
//the reps, bodyTarget, weight etc
}
}
任何帮助,将不胜感激。谢谢!