如何更新 RecyclerView 适配器数据
How to update RecyclerView Adapter Data
我想弄清楚更新 RecyclerView
的适配器有什么问题。
获得新的产品列表后,我尝试:
从创建recyclerView
的片段更新ArrayList
,将新数据设置到适配器,然后调用adapter.notifyDataSetChanged()
;它没有用。
像其他人一样创建一个新的适配器,它对他们有用,但对我来说没有任何变化:recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))
在 Adapter
中创建一个方法,更新数据如下:
public void updateData(ArrayList<ViewModel> viewModels) {
items.clear();
items.addAll(viewModels);
notifyDataSetChanged();
}
然后每当我想更新数据列表时调用这个方法;它没有用。
检查我是否可以以任何方式修改 recyclerView,我尝试至少删除一个项目:
public void removeItem(int position) {
items.remove(position);
notifyItemRemoved(position);
}
一切照旧。
这是我的适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {
private ArrayList<ViewModel> items;
private OnItemClickListener onItemClickListener;
public RecyclerViewAdapter(ArrayList<ViewModel> items) {
this.items = items;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
v.setOnClickListener(this);
return new ViewHolder(v);
}
public void updateData(ArrayList<ViewModel> viewModels) {
items.clear();
items.addAll(viewModels);
notifyDataSetChanged();
}
public void addItem(int position, ViewModel viewModel) {
items.add(position, viewModel);
notifyItemInserted(position);
}
public void removeItem(int position) {
items.remove(position);
notifyItemRemoved(position);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ViewModel item = items.get(position);
holder.title.setText(item.getTitle());
Picasso.with(holder.image.getContext()).load(item.getImage()).into(holder.image);
holder.price.setText(item.getPrice());
holder.credit.setText(item.getCredit());
holder.description.setText(item.getDescription());
holder.itemView.setTag(item);
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onClick(final View v) {
// Give some time to the ripple to finish the effect
if (onItemClickListener != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
}
}, 0);
}
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public TextView price, credit, title, description;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
price = (TextView) itemView.findViewById(R.id.price);
credit = (TextView) itemView.findViewById(R.id.credit);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
}
}
public interface OnItemClickListener {
void onItemClick(View view, ViewModel viewModel);
}
}
然后我启动 RecyclerView
如下:
recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
adapter = new RecyclerViewAdapter(items);
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);
那么,我该如何实际更新适配器数据以显示新收到的项目?
问题是 gridView 所在的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:tag="catalog_fragment"
android:layout_height="match_parent">
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
<ImageButton
android:id="@+id/fab"
android:layout_gravity="top|end"
style="@style/FabStyle"/>
</FrameLayout>
</LinearLayout>
然后我删除了 LinearLayout
并将 FrameLayout
作为父布局。
我正在使用 RecyclerView,删除和更新都运行良好。
移除:
从 RecyclerView 中删除项目有四个步骤
list.remove(position);
recycler.removeViewAt(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, list.size());
这些代码行对我有用。
更新数据:
我唯一要做的事情是:
mAdapter.notifyDataSetChanged();
您必须在 Actvity/Fragment 代码中执行所有这些操作,而不是在 RecyclerView 适配器代码中。
这对我有用:
recyclerView.setAdapter(new RecyclerViewAdapter(newList));
recyclerView.invalidate();
创建一个包含更新列表的新适配器(在我的例子中它是一个转换为 ArrayList 的数据库)并将其设置为适配器后,我尝试了 recyclerView.invalidate()
并且成功了。
将新数据添加到现有数据的最佳和最酷的方法是
ArrayList<String> newItems = new ArrayList<String>();
newItems = getList();
int oldListItemscount = alcontainerDetails.size();
alcontainerDetails.addAll(newItems);
recyclerview.getAdapter().notifyItemChanged(oldListItemscount+1, al_containerDetails);
您有两种选择:
从适配器刷新 UI:
mAdapter.notifyDataSetChanged();
或者从recyclerView本身刷新:
recyclerView.invalidate();
我用不同的方法解决了同样的问题。
我没有数据。我在后台线程中等待它,所以从一个空列表开始。
mAdapter = new ModelAdapter(getContext(), new ArrayList<Model>());
// Then when I get data
mAdapter.update(response.getModelList());
// And update it in my adapter
public void update(ArrayList<Model> modelList){
adapterModelList.clear();
for (Product model: modelList) {
adapterModelList.add(model);
}
mAdapter.notifyDataSetChanged();
}
就是这样。
另一种选择是使用 diffutil。它会将原始列表与新列表进行比较,如果有变化,则使用新列表作为更新。
基本上,我们可以使用 DiffUtil 来比较旧数据和新数据,让它为您调用 notifyItemRangeRemoved、notifyItemRangeChanged 和 notifyItemRangeInserted。
使用 diffUtil 代替 notifyDataSetChanged 的快速示例:
DiffResult diffResult = DiffUtil
.calculateDiff(new MyDiffUtilCB(getItems(), items));
//any clear up on memory here and then
diffResult.dispatchUpdatesTo(this);
//and then, if necessary
items.clear()
items.addAll(newItems)
我在主线程之外执行 calculateDiff 工作,以防它是一个使用 AsyncListDiffer
的大列表
更新listview、gridview、recyclerview的数据:
mAdapter.notifyDataSetChanged();
或者:
mAdapter.notifyItemRangeChanged(0, itemList.size());
好久才得到答案:
SELECTEDROW.add(dt);
notifyItemInserted(position);
SELECTEDROW.remove(position);
notifyItemRemoved(position);
如果以上评论中提到的任何内容都不适合您,则可能意味着问题出在其他地方。
我发现解决方案的一个地方是我将列表设置到适配器的方式。在我的 activity 中,列表是一个实例变量,当任何数据更改时我直接更改它。由于它是一个参考变量,所以发生了一些奇怪的事情。
所以我把引用变量改成本地变量,用另一个变量更新数据,然后传递给前面答案中提到的addAll()
函数
这些方法非常有效,非常适合开始使用基本 RecyclerView
。
private List<YourItem> items;
public void setItems(List<YourItem> newItems)
{
clearItems();
addItems(newItems);
}
public void addItem(YourItem item, int position)
{
if (position > items.size()) return;
items.add(item);
notifyItemInserted(position);
}
public void addMoreItems(List<YourItem> newItems)
{
int position = items.size() + 1;
newItems.addAll(newItems);
notifyItemChanged(position, newItems);
}
public void addItems(List<YourItem> newItems)
{
items.addAll(newItems);
notifyDataSetChanged();
}
public void clearItems()
{
items.clear();
notifyDataSetChanged();
}
public void addLoader()
{
items.add(null);
notifyItemInserted(items.size() - 1);
}
public void removeLoader()
{
items.remove(items.size() - 1);
notifyItemRemoved(items.size());
}
public void removeItem(int position)
{
if (position >= items.size()) return;
items.remove(position);
notifyItemRemoved(position);
}
public void swapItems(int positionA, int positionB)
{
if (positionA > items.size()) return;
if (positionB > items.size()) return;
YourItem firstItem = items.get(positionA);
videoList.set(positionA, items.get(positionB));
videoList.set(positionB, firstItem);
notifyDataSetChanged();
}
您可以在适配器 Class 或您的片段或 Activity 中实现它们,但在这种情况下,您必须实例化适配器以调用通知方法。在我的例子中,我通常在适配器中实现它。
这是一般性的回答。解释了更新适配器数据的各种方法。该过程每次包括两个主要步骤:
- 更新数据集
- 将更改通知适配器
插入单个项目
在索引 2
处添加“猪”。
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);
插入多项
在索引 2
处再插入三只动物。
ArrayList<String> items = new ArrayList<>();
items.add("Pig");
items.add("Chicken");
items.add("Dog");
int insertIndex = 2;
data.addAll(insertIndex, items);
adapter.notifyItemRangeInserted(insertIndex, items.size());
删除单个项目
从列表中删除“猪”。
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
移除多项
从列表中删除“骆驼”和“绵羊”。
int startIndex = 2; // inclusive
int endIndex = 4; // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);
删除所有项目
清除整个列表。
data.clear();
adapter.notifyDataSetChanged();
用新列表替换旧列表
清除旧列表然后添加新列表。
// clear old list
data.clear();
// add new list
ArrayList<String> newList = new ArrayList<>();
newList.add("Lion");
newList.add("Wolf");
newList.add("Bear");
data.addAll(newList);
// notify adapter
adapter.notifyDataSetChanged();
adapter
引用了 data
,因此我没有将 data
设置为新对象很重要。相反,我从 data
中清除了旧项目,然后添加了新项目。
更新单个项目
更改“绵羊”项目,使其显示“我喜欢绵羊”。
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);
移动单个项目
将“绵羊”从位置 3
移动到位置 1
。
int fromPosition = 3;
int toPosition = 1;
// update data array
String item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);
// notify adapter
adapter.notifyItemMoved(fromPosition, toPosition);
代码
这里是项目代码,供大家参考。 RecyclerView Adapter 代码可以在 .
找到
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
List<String> data;
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
data = new ArrayList<>();
data.add("Horse");
data.add("Cow");
data.add("Camel");
data.add("Sheep");
data.add("Goat");
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvAnimals);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
public void onButtonClick(View view) {
insertSingleItem();
}
private void insertSingleItem() {
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);
}
private void insertMultipleItems() {
ArrayList<String> items = new ArrayList<>();
items.add("Pig");
items.add("Chicken");
items.add("Dog");
int insertIndex = 2;
data.addAll(insertIndex, items);
adapter.notifyItemRangeInserted(insertIndex, items.size());
}
private void removeSingleItem() {
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
}
private void removeMultipleItems() {
int startIndex = 2; // inclusive
int endIndex = 4; // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);
}
private void removeAllItems() {
data.clear();
adapter.notifyDataSetChanged();
}
private void replaceOldListWithNewList() {
// clear old list
data.clear();
// add new list
ArrayList<String> newList = new ArrayList<>();
newList.add("Lion");
newList.add("Wolf");
newList.add("Bear");
data.addAll(newList);
// notify adapter
adapter.notifyDataSetChanged();
}
private void updateSingleItem() {
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);
}
private void moveSingleItem() {
int fromPosition = 3;
int toPosition = 1;
// update data array
String item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);
// notify adapter
adapter.notifyItemMoved(fromPosition, toPosition);
}
}
备注
- 如果使用
notifyDataSetChanged()
,则不会执行任何动画。这也可能是一项昂贵的操作,因此如果您只更新单个项目或一系列项目,不建议使用 notifyDataSetChanged()
。
- 如果您要对列表进行大量或复杂的更改,请查看 DiffUtil。
进一步研究
我发现重新加载 RecyclerView 的一个非常简单的方法就是调用
recyclerView.removeAllViews();
这将首先删除 RecyclerView 的所有内容,然后使用更新后的值再次添加它。
我强烈建议您使用[DiffUtil.ItemCallback][1]
来处理RecyclerView.Adapter
中的变化:
fun setData(data: List<T>) {
val calculateDiff = DiffUtil.calculateDiff(DiffUtilCallback(items, data))
items.clear()
items += data
calculateDiff.dispatchUpdatesTo(this)
}
在引擎盖下它处理大部分事情 AdapterListUpdateCallback
:
/**
* ListUpdateCallback that dispatches update events to the given adapter.
*
* @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)
*/
public final class AdapterListUpdateCallback implements ListUpdateCallback {
@NonNull
private final RecyclerView.Adapter mAdapter;
/**
* Creates an AdapterListUpdateCallback that will dispatch update events to the given adapter.
*
* @param adapter The Adapter to send updates to.
*/
public AdapterListUpdateCallback(@NonNull RecyclerView.Adapter adapter) {
mAdapter = adapter;
}
/** {@inheritDoc} */
@Override
public void onInserted(int position, int count) {
mAdapter.notifyItemRangeInserted(position, count);
}
/** {@inheritDoc} */
@Override
public void onRemoved(int position, int count) {
mAdapter.notifyItemRangeRemoved(position, count);
}
/** {@inheritDoc} */
@Override
public void onMoved(int fromPosition, int toPosition) {
mAdapter.notifyItemMoved(fromPosition, toPosition);
}
/** {@inheritDoc} */
@Override
public void onChanged(int position, int count, Object payload) {
mAdapter.notifyItemRangeChanged(position, count, payload);
}
}
对于 Kotlin,我会使用 Adapter。,
class ProductAdapter(var apples: List<Apples>?= null) : RecyclerView.Adapter<ProductViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {...}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {...}
override fun getItemCount(): Int {...}
fun setData(newApples: List<Apples>) {
apples = newApples
}
}
在Fragment/Activity
val appleAdapter = ProductAdapter()
val recyclerView = binding.appleRecycler // or findViewById or synthetics or whatever.
recyclerView.adapter = appleAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
现在处理片段中的数据变化
fun updateRecyclerData(apples: List<Apples>){
adapter.setData(apples)
adapter.notifyDataSetChanged()
}
updateRecyclerData(applesList)
这很好用试试看。
ArrayList.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
我建议您探索 DiffUtil。它还在处理列表更新时提高了 RecyclerView 的性能。
在您的适配器中定义一个变量:
differList = AsyncListDiffer(this, this.callback);
differList.submitList(list)
此处的列表可以是您的初始原始列表,也可以只是一个空列表,前提是您稍后会对其进行更新。
实现回调:
private val callback : DiffUtil.ItemCallback<Item> = object: DiffUtil.ItemCallback<Item>() {
override fun areItemsTheSame(oldItem: Item, newItem: Item) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Item, newItem: Item) =
oldItem == newItem
}
此外,在同一个适配器中,您将有一些 public 函数来设置列表。
fun setData(list: List<Item>) {
differList.submitList(list)
//yeah, that's it!
}
现在,在您对列表进行任何更改后 (insert/update/delete),只需从您的 [=48] 调用此 setData(list) =].
mAdapter.setData(list)
简单吧?
我想弄清楚更新 RecyclerView
的适配器有什么问题。
获得新的产品列表后,我尝试:
从创建
recyclerView
的片段更新ArrayList
,将新数据设置到适配器,然后调用adapter.notifyDataSetChanged()
;它没有用。像其他人一样创建一个新的适配器,它对他们有用,但对我来说没有任何变化:
recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))
在
Adapter
中创建一个方法,更新数据如下:public void updateData(ArrayList<ViewModel> viewModels) { items.clear(); items.addAll(viewModels); notifyDataSetChanged(); }
然后每当我想更新数据列表时调用这个方法;它没有用。
检查我是否可以以任何方式修改 recyclerView,我尝试至少删除一个项目:
public void removeItem(int position) { items.remove(position); notifyItemRemoved(position); }
一切照旧。
这是我的适配器:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {
private ArrayList<ViewModel> items;
private OnItemClickListener onItemClickListener;
public RecyclerViewAdapter(ArrayList<ViewModel> items) {
this.items = items;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
v.setOnClickListener(this);
return new ViewHolder(v);
}
public void updateData(ArrayList<ViewModel> viewModels) {
items.clear();
items.addAll(viewModels);
notifyDataSetChanged();
}
public void addItem(int position, ViewModel viewModel) {
items.add(position, viewModel);
notifyItemInserted(position);
}
public void removeItem(int position) {
items.remove(position);
notifyItemRemoved(position);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ViewModel item = items.get(position);
holder.title.setText(item.getTitle());
Picasso.with(holder.image.getContext()).load(item.getImage()).into(holder.image);
holder.price.setText(item.getPrice());
holder.credit.setText(item.getCredit());
holder.description.setText(item.getDescription());
holder.itemView.setTag(item);
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onClick(final View v) {
// Give some time to the ripple to finish the effect
if (onItemClickListener != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
}
}, 0);
}
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public TextView price, credit, title, description;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
price = (TextView) itemView.findViewById(R.id.price);
credit = (TextView) itemView.findViewById(R.id.credit);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
}
}
public interface OnItemClickListener {
void onItemClick(View view, ViewModel viewModel);
}
}
然后我启动 RecyclerView
如下:
recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
adapter = new RecyclerViewAdapter(items);
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);
那么,我该如何实际更新适配器数据以显示新收到的项目?
问题是 gridView 所在的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:tag="catalog_fragment"
android:layout_height="match_parent">
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"/>
<ImageButton
android:id="@+id/fab"
android:layout_gravity="top|end"
style="@style/FabStyle"/>
</FrameLayout>
</LinearLayout>
然后我删除了 LinearLayout
并将 FrameLayout
作为父布局。
我正在使用 RecyclerView,删除和更新都运行良好。
移除:
从 RecyclerView 中删除项目有四个步骤
list.remove(position); recycler.removeViewAt(position); mAdapter.notifyItemRemoved(position); mAdapter.notifyItemRangeChanged(position, list.size());
这些代码行对我有用。
更新数据:
我唯一要做的事情是:
mAdapter.notifyDataSetChanged();
您必须在 Actvity/Fragment 代码中执行所有这些操作,而不是在 RecyclerView 适配器代码中。
这对我有用:
recyclerView.setAdapter(new RecyclerViewAdapter(newList));
recyclerView.invalidate();
创建一个包含更新列表的新适配器(在我的例子中它是一个转换为 ArrayList 的数据库)并将其设置为适配器后,我尝试了 recyclerView.invalidate()
并且成功了。
将新数据添加到现有数据的最佳和最酷的方法是
ArrayList<String> newItems = new ArrayList<String>();
newItems = getList();
int oldListItemscount = alcontainerDetails.size();
alcontainerDetails.addAll(newItems);
recyclerview.getAdapter().notifyItemChanged(oldListItemscount+1, al_containerDetails);
您有两种选择:
从适配器刷新 UI:
mAdapter.notifyDataSetChanged();
或者从recyclerView本身刷新:
recyclerView.invalidate();
我用不同的方法解决了同样的问题。 我没有数据。我在后台线程中等待它,所以从一个空列表开始。
mAdapter = new ModelAdapter(getContext(), new ArrayList<Model>());
// Then when I get data
mAdapter.update(response.getModelList());
// And update it in my adapter
public void update(ArrayList<Model> modelList){
adapterModelList.clear();
for (Product model: modelList) {
adapterModelList.add(model);
}
mAdapter.notifyDataSetChanged();
}
就是这样。
另一种选择是使用 diffutil。它会将原始列表与新列表进行比较,如果有变化,则使用新列表作为更新。
基本上,我们可以使用 DiffUtil 来比较旧数据和新数据,让它为您调用 notifyItemRangeRemoved、notifyItemRangeChanged 和 notifyItemRangeInserted。
使用 diffUtil 代替 notifyDataSetChanged 的快速示例:
DiffResult diffResult = DiffUtil
.calculateDiff(new MyDiffUtilCB(getItems(), items));
//any clear up on memory here and then
diffResult.dispatchUpdatesTo(this);
//and then, if necessary
items.clear()
items.addAll(newItems)
我在主线程之外执行 calculateDiff 工作,以防它是一个使用 AsyncListDiffer
的大列表更新listview、gridview、recyclerview的数据:
mAdapter.notifyDataSetChanged();
或者:
mAdapter.notifyItemRangeChanged(0, itemList.size());
好久才得到答案:
SELECTEDROW.add(dt);
notifyItemInserted(position);
SELECTEDROW.remove(position);
notifyItemRemoved(position);
如果以上评论中提到的任何内容都不适合您,则可能意味着问题出在其他地方。
我发现解决方案的一个地方是我将列表设置到适配器的方式。在我的 activity 中,列表是一个实例变量,当任何数据更改时我直接更改它。由于它是一个参考变量,所以发生了一些奇怪的事情。
所以我把引用变量改成本地变量,用另一个变量更新数据,然后传递给前面答案中提到的addAll()
函数
这些方法非常有效,非常适合开始使用基本 RecyclerView
。
private List<YourItem> items;
public void setItems(List<YourItem> newItems)
{
clearItems();
addItems(newItems);
}
public void addItem(YourItem item, int position)
{
if (position > items.size()) return;
items.add(item);
notifyItemInserted(position);
}
public void addMoreItems(List<YourItem> newItems)
{
int position = items.size() + 1;
newItems.addAll(newItems);
notifyItemChanged(position, newItems);
}
public void addItems(List<YourItem> newItems)
{
items.addAll(newItems);
notifyDataSetChanged();
}
public void clearItems()
{
items.clear();
notifyDataSetChanged();
}
public void addLoader()
{
items.add(null);
notifyItemInserted(items.size() - 1);
}
public void removeLoader()
{
items.remove(items.size() - 1);
notifyItemRemoved(items.size());
}
public void removeItem(int position)
{
if (position >= items.size()) return;
items.remove(position);
notifyItemRemoved(position);
}
public void swapItems(int positionA, int positionB)
{
if (positionA > items.size()) return;
if (positionB > items.size()) return;
YourItem firstItem = items.get(positionA);
videoList.set(positionA, items.get(positionB));
videoList.set(positionB, firstItem);
notifyDataSetChanged();
}
您可以在适配器 Class 或您的片段或 Activity 中实现它们,但在这种情况下,您必须实例化适配器以调用通知方法。在我的例子中,我通常在适配器中实现它。
这是一般性的回答。解释了更新适配器数据的各种方法。该过程每次包括两个主要步骤:
- 更新数据集
- 将更改通知适配器
插入单个项目
在索引 2
处添加“猪”。
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);
插入多项
在索引 2
处再插入三只动物。
ArrayList<String> items = new ArrayList<>();
items.add("Pig");
items.add("Chicken");
items.add("Dog");
int insertIndex = 2;
data.addAll(insertIndex, items);
adapter.notifyItemRangeInserted(insertIndex, items.size());
删除单个项目
从列表中删除“猪”。
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
移除多项
从列表中删除“骆驼”和“绵羊”。
int startIndex = 2; // inclusive
int endIndex = 4; // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);
删除所有项目
清除整个列表。
data.clear();
adapter.notifyDataSetChanged();
用新列表替换旧列表
清除旧列表然后添加新列表。
// clear old list
data.clear();
// add new list
ArrayList<String> newList = new ArrayList<>();
newList.add("Lion");
newList.add("Wolf");
newList.add("Bear");
data.addAll(newList);
// notify adapter
adapter.notifyDataSetChanged();
adapter
引用了 data
,因此我没有将 data
设置为新对象很重要。相反,我从 data
中清除了旧项目,然后添加了新项目。
更新单个项目
更改“绵羊”项目,使其显示“我喜欢绵羊”。
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);
移动单个项目
将“绵羊”从位置 3
移动到位置 1
。
int fromPosition = 3;
int toPosition = 1;
// update data array
String item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);
// notify adapter
adapter.notifyItemMoved(fromPosition, toPosition);
代码
这里是项目代码,供大家参考。 RecyclerView Adapter 代码可以在
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
List<String> data;
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
data = new ArrayList<>();
data.add("Horse");
data.add("Cow");
data.add("Camel");
data.add("Sheep");
data.add("Goat");
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.rvAnimals);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
public void onButtonClick(View view) {
insertSingleItem();
}
private void insertSingleItem() {
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);
}
private void insertMultipleItems() {
ArrayList<String> items = new ArrayList<>();
items.add("Pig");
items.add("Chicken");
items.add("Dog");
int insertIndex = 2;
data.addAll(insertIndex, items);
adapter.notifyItemRangeInserted(insertIndex, items.size());
}
private void removeSingleItem() {
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
}
private void removeMultipleItems() {
int startIndex = 2; // inclusive
int endIndex = 4; // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);
}
private void removeAllItems() {
data.clear();
adapter.notifyDataSetChanged();
}
private void replaceOldListWithNewList() {
// clear old list
data.clear();
// add new list
ArrayList<String> newList = new ArrayList<>();
newList.add("Lion");
newList.add("Wolf");
newList.add("Bear");
data.addAll(newList);
// notify adapter
adapter.notifyDataSetChanged();
}
private void updateSingleItem() {
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);
}
private void moveSingleItem() {
int fromPosition = 3;
int toPosition = 1;
// update data array
String item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);
// notify adapter
adapter.notifyItemMoved(fromPosition, toPosition);
}
}
备注
- 如果使用
notifyDataSetChanged()
,则不会执行任何动画。这也可能是一项昂贵的操作,因此如果您只更新单个项目或一系列项目,不建议使用notifyDataSetChanged()
。 - 如果您要对列表进行大量或复杂的更改,请查看 DiffUtil。
进一步研究
我发现重新加载 RecyclerView 的一个非常简单的方法就是调用
recyclerView.removeAllViews();
这将首先删除 RecyclerView 的所有内容,然后使用更新后的值再次添加它。
我强烈建议您使用[DiffUtil.ItemCallback][1]
来处理RecyclerView.Adapter
中的变化:
fun setData(data: List<T>) {
val calculateDiff = DiffUtil.calculateDiff(DiffUtilCallback(items, data))
items.clear()
items += data
calculateDiff.dispatchUpdatesTo(this)
}
在引擎盖下它处理大部分事情 AdapterListUpdateCallback
:
/**
* ListUpdateCallback that dispatches update events to the given adapter.
*
* @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)
*/
public final class AdapterListUpdateCallback implements ListUpdateCallback {
@NonNull
private final RecyclerView.Adapter mAdapter;
/**
* Creates an AdapterListUpdateCallback that will dispatch update events to the given adapter.
*
* @param adapter The Adapter to send updates to.
*/
public AdapterListUpdateCallback(@NonNull RecyclerView.Adapter adapter) {
mAdapter = adapter;
}
/** {@inheritDoc} */
@Override
public void onInserted(int position, int count) {
mAdapter.notifyItemRangeInserted(position, count);
}
/** {@inheritDoc} */
@Override
public void onRemoved(int position, int count) {
mAdapter.notifyItemRangeRemoved(position, count);
}
/** {@inheritDoc} */
@Override
public void onMoved(int fromPosition, int toPosition) {
mAdapter.notifyItemMoved(fromPosition, toPosition);
}
/** {@inheritDoc} */
@Override
public void onChanged(int position, int count, Object payload) {
mAdapter.notifyItemRangeChanged(position, count, payload);
}
}
对于 Kotlin,我会使用 Adapter。,
class ProductAdapter(var apples: List<Apples>?= null) : RecyclerView.Adapter<ProductViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {...}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {...}
override fun getItemCount(): Int {...}
fun setData(newApples: List<Apples>) {
apples = newApples
}
}
在Fragment/Activity
val appleAdapter = ProductAdapter()
val recyclerView = binding.appleRecycler // or findViewById or synthetics or whatever.
recyclerView.adapter = appleAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
现在处理片段中的数据变化
fun updateRecyclerData(apples: List<Apples>){
adapter.setData(apples)
adapter.notifyDataSetChanged()
}
updateRecyclerData(applesList)
这很好用试试看。
ArrayList.remove(position);
notifyItemRemoved(position);
notifyDataSetChanged();
我建议您探索 DiffUtil。它还在处理列表更新时提高了 RecyclerView 的性能。
在您的适配器中定义一个变量:
differList = AsyncListDiffer(this, this.callback); differList.submitList(list)
此处的列表可以是您的初始原始列表,也可以只是一个空列表,前提是您稍后会对其进行更新。
实现回调:
private val callback : DiffUtil.ItemCallback<Item> = object: DiffUtil.ItemCallback<Item>() { override fun areItemsTheSame(oldItem: Item, newItem: Item) = oldItem.id == newItem.id override fun areContentsTheSame(oldItem: Item, newItem: Item) = oldItem == newItem }
此外,在同一个适配器中,您将有一些 public 函数来设置列表。
fun setData(list: List<Item>) { differList.submitList(list) //yeah, that's it! }
现在,在您对列表进行任何更改后 (insert/update/delete),只需从您的 [=48] 调用此 setData(list) =].
mAdapter.setData(list)
简单吧?