如何更新回收视图适配器数据?试图找出更新的问题所在RecyclerView是适配器。在我得到一份新的产品清单之后,我试着:更新ArrayList从碎片recyclerView,将新数据设置为适配器,然后调用adapter.notifyDataSetChanged()没有用。像其他人一样,创建一个新适配器,它对他们起了作用,但对我来说没有变化:recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))创建一个方法Adapter它更新数据如下:public void updateData(ArrayList<ViewModel> viewModels) {
items.clear();
items.addAll(viewModels);
notifyDataSetChanged();}然后,每当我想更新数据列表时,我都会调用这个方法;它没有工作。若要检查是否可以任何方式修改“回收视图”,并且尝试删除至少一个项目: 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);
}因此,如何实际更新适配器数据以显示新接收的项?
3 回答
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
list.remove(position);recycler.removeViewAt(position);mAdapter.notifyItemRemoved(position); mAdapter.notifyItemRangeChanged(position, list.size());
mAdapter.notifyDataSetChanged();
ABOUTYOU
TA贡献1812条经验 获得超5个赞
recyclerView.setAdapter(new RecyclerViewAdapter(newList));recyclerView.invalidate();
recyclerView.invalidate()
- 3 回答
- 0 关注
- 436 浏览
添加回答
举报
0/150
提交
取消