我有一个要求,我应该在滚动和更新列表的同时下载广告项。由于调用 notifyDatasetChnaged()
会重置所有内容,因此我正在调用 notifyItemInserted(position)
。但是,调用它会重复列表中的项目。我发现列表中没有重复的项目。但是在调用 notifyItemInserted
之后,它会复制该项目。我不知道如何解决这个问题。这就是我正在做的:
mNewsList.add(mPreviousAdPosition, newsItem);
mAdapter.notifyItemInserted(mPreviousAdPosition);
如果我打电话,它工作正常,没有重复的项目。但我不想重新创建我的列表项。可能是什么问题?
最佳答案
对于完全相同的用例,我遇到了同样的问题,解决方案是:
在您的适配器中实现此方法:
@Override
public long getItemId(int position) {
//Return the stable ID for the item at position
return items.get(position).getId();
}
在适配器的构造函数中调用此方法:
//Indicates whether each item in the data set can be represented with a unique identifier
setHasStableIds(true);
关于android - Recyclerview.notifyItemInserted() 复制列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652892/