notifyDataSetChanged()在MVP android中不工作
我有一个刷新按钮,可以获取更新的项目列表。单击此按钮时,刷新按钮位于片段标题上,我将更新适配器 片段内的适配器代码(FragmentView) 在Presenter内部,这是实现notifyDataSetChanged()在MVP android中不工作,android,Android,我有一个刷新按钮,可以获取更新的项目列表。单击此按钮时,刷新按钮位于片段标题上,我将更新适配器 片段内的适配器代码(FragmentView) 在Presenter内部,这是实现 public void onLanguageAutoRenewClicked() { // Retrofit call onResponseSuccess() { FragmentView.reloadData(); } } 在我的片段视图中,我重新加载了数据 @Over
public void onLanguageAutoRenewClicked() {
// Retrofit call
onResponseSuccess() {
FragmentView.reloadData();
}
}
在我的片段视图中,我重新加载了数据
@Override
public void reloadData() {
langAdapter.notifyDataSetChanged();
}
我正在调用WS-on-click of按钮,该按钮将为我提供新的列表项,我正在调用reloadData并在片段类上设置notifyDataSetChanged,但我仍然无法更新适配器中的项。根据您的评论: 我认为您的问题在于传递给适配器的列表。要调用notify的方法,您应该更新传递给适配器的列表 您可以使用DiffUtil实现如下适配器,以增加操作的简单性 此代码由Kotlin语言开发。如果您需要Java,我可以为您重写:) 使用DiffUtil,您不再需要调用notifyDataSetChanged()或其他notify方法
class FooRvAdapter(
private val dataList: ArrayList<Fee>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
fun updateItems(newDataList: List<Fee>) {
val diffCallback = FooRvAdapterDiffCallback(dataList, newDataList)
val diffResult = DiffUtil.calculateDiff(diffCallback, false)
dataList.clear()
dataList.addAll(newDataList)
diffResult.dispatchUpdatesTo(this@FooRvAdapter)
}
/*
OnBind(),ViewHolder, etc.
...
*/
inner class FooRvAdapterDiffCallback(
private val oldList: List<Fee>,
private val newList: List<Fee>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
newList[newItemPosition].id == oldList[oldItemPosition].id
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
newList[newItemPosition] == oldList[oldItemPosition]
}
interface FooCallBack {
fun onItemClick(data: Fee)
}
}
类FoorAdapter(
私有val数据列表:ArrayList
):RecyclerView.Adapter(){
乐趣更新项目(newDataList:List){
val diffCallback=FooRvAdapterDiffCallback(数据列表,新数据列表)
val diffredesult=DiffUtil.calculateDiff(diffCallback,false)
dataList.clear()
addAll(newDataList)
Differesult.dispatchUpdatesTo(this@FooRvAdapter)
}
/*
OnBind()、ViewHolder等。
...
*/
内部类FoorAdapterDiffCallback(
private val oldList:List,
private val newList:List
):DiffUtil.Callback(){
覆盖相同的项目(旧项目位置:Int,新项目位置:Int)=
newList[newItemPosition].id==oldList[oldItemPosition].id
重写getOldListSize()=oldList.size
重写getNewListSize()=newList.size
覆盖相同的内容(oldItemPosition:Int,newItemPosition:Int)=
newList[newItemPosition]==oldList[oldItemPosition]
}
接口回调{
趣味mclick(数据:费用)
}
}
您不需要使用:langAdapter.notifyDataSetChanged();因为您尝试将新适配器设置为RecyclerView。检查您加载的列表。您是如何更新片段内部的langList
的?列表的更新发生在片段演示器中,而不是片段@Md.asaduzzamanches中。更改应反映在langList
中。在调用notifyDataSetChanged
之前,您能否通过调试检查langList
是否在reloadData
内部更新了
@Override
public void reloadData() {
langAdapter.notifyDataSetChanged();
}
class FooRvAdapter(
private val dataList: ArrayList<Fee>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
fun updateItems(newDataList: List<Fee>) {
val diffCallback = FooRvAdapterDiffCallback(dataList, newDataList)
val diffResult = DiffUtil.calculateDiff(diffCallback, false)
dataList.clear()
dataList.addAll(newDataList)
diffResult.dispatchUpdatesTo(this@FooRvAdapter)
}
/*
OnBind(),ViewHolder, etc.
...
*/
inner class FooRvAdapterDiffCallback(
private val oldList: List<Fee>,
private val newList: List<Fee>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
newList[newItemPosition].id == oldList[oldItemPosition].id
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
newList[newItemPosition] == oldList[oldItemPosition]
}
interface FooCallBack {
fun onItemClick(data: Fee)
}
}