gpt4 book ai didi

android - DiffUtil 没有更新 recyclerview

转载 作者:行者123 更新时间:2023-11-29 18:54:07 39 4
gpt4 key购买 nike

我正在使用下面的代码来测试 DiffUtil实现。根据文档-

DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.

我的新列表比旧列表多了一项。但是我的适配器仅在调用适配器上的 dispatchUpdatesTo 后才显示旧列表。

  val diffResult = DiffUtil.calculateDiff(MyDiffCallback(newlist, commitmentList),true);
commitmentList=newlist
diffResult.dispatchUpdatesTo(commitmentAdapter);

MyDiffCallback.java

public class MyDiffCallback extends DiffUtil.Callback{

ArrayList<Commitment> oldPersons;
ArrayList<Commitment> newPersons;

public MyDiffCallback(ArrayList<Commitment> newPersons, ArrayList<Commitment> oldPersons) {
this.newPersons = newPersons;
this.oldPersons = oldPersons;
}

@Override
public int getOldListSize() {
return oldPersons.size();
}

@Override
public int getNewListSize() {
return newPersons.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {

return(oldPersons.get(oldItemPosition)
.equals(newPersons.get(newItemPosition))); }

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {

return(oldPersons.get(oldItemPosition)
.compareTo(newPersons.get(newItemPosition))==0); }


}

适配器

class CommitmentAdapter(private val context: Context, private val data: ArrayList<Commitment>?) : RecyclerView.Adapter<CommitmentAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommitmentAdapter.ViewHolder {

val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_commitment, parent, false)

return ViewHolder(itemView)
}

override fun getItemCount(): Int {
return data!!.size
}

override fun onBindViewHolder(holder: CommitmentAdapter.ViewHolder, position: Int) {

holder.bindata(data!![position],position,context)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindata(data: Commitment,position:Int,context: Context) {
var pedoDB: PedometerDaoImpl=PedometerDaoImpl(context)
itemView.tvDate.text = "Start date : " + getDat(data.startDate!!.toLong()* 1000L) + " End date : " + getDat(data.endDate!!.toLong()* 1000L)
itemView.pb_calories.max = 100

itemView.clCommitment.tag=position
itemView.clCommitment.setOnClickListener {
val intent= Intent(context,EditCommitment::class.java).putExtra("Data",data)
context.startActivity(intent)

}


}

fun getDat(timeStamp: Long): String {

try {
val sdf = SimpleDateFormat("dd/MM/yyyy")
val netDate = Date(timeStamp)
return sdf.format(netDate)
} catch (ex: Exception) {
return "xx"
}

}
}

}

最佳答案

在你的代码中

val diffResult = DiffUtil.calculateDiff(MyDiffCallback(newlist, commitmentList),true);
commitmentList=newlist

commitmentListnewlist 指的是同一个对象。

所以问题可能是,当您收到包含另外一项的新列表时,您的 commitmentList 也已经是最新的了。

在这种情况下,您的areItemsTheSame() 方法将始终返回 true,并且您的适配器不会收到有关更改的通知

关于android - DiffUtil 没有更新 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288882/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com