每当新项目添加到列表中时,我都会使用Observer
类来更新RecyclerView
。以下是我的代码 -
Observer<List<StudentEntity>> myObserver = new Observer<List<StudentEntity>>() {
@Override
public void onChanged(@Nullable List<StudentEntity> studentEntities) {
//Update Listview adapter
lstStudents = studentEntities;
if (recyclerView.getAdapter()==null){
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
recyclerView.setAdapter(recyclerViewAdapter);
}else {
//recyclerView.getAdapter().notifyDataSetChanged(); --- This does not work
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
recyclerView.setAdapter(recyclerViewAdapter);
Toast.makeText(getContext(),"New record added",Toast.LENGTH_SHORT).show();
}
}
};
在我使用notifydatasetchanged()
时的if条件中,适配器正在重新绘制自己但使用旧数据。但是,当我使用setAdapter方法时,它工作正常。你能解释一下吗?
答案 0 :(得分:1)
private void setupRecyclerView() {
aDummyList.clear();
aDummyList = MyListUtil.getaDummyList(); //load data or get data from your source
myAdapter = new MyAdapter(aDummyList);
myAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
// checkAdapterIsEmpty();
if (layoutManager.getItemCount() == 0) {
Log.w(TAG, " listSize == 0 ");
recyclerView.setVisibility(View.GONE);
} else {
Log.w(TAG, " listSize NOT ZERO ");
recyclerView.setVisibility(View.VISIBLE);
}
}
});
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
这个怎么样! 并在onCreate
中设置您的recyclerview配置noteAdapter = new NoteAdapter(aDummyList, yourContext);
layoutManager = new LinearLayoutManager(yourContext, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(v.getContext(), DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(noteAdapter);
答案 1 :(得分:0)
试试这个
声明您之前的onCreate()
方法
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter;
比使用以下条件
lstStudents.addAll(studentEntities);
if (recyclerViewAdapter==null){
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
recyclerView.setAdapter(recyclerViewAdapter);
}else {
recyclerViewAdapter.notifyItemInserted(arrayList.size());
recyclerViewAdapter.notifyDataSetChanged();
}
或尝试
声明您之前的onCreate()
方法
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter;
boolean isFtym = true;
比使用以下条件
lstStudents.addAll(studentEntities);
if (isFtym){
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
recyclerView.setAdapter(recyclerViewAdapter);
isFtym = false;
}else {
recyclerViewAdapter.notifyItemInserted(arrayList.size());
recyclerViewAdapter.notifyDataSetChanged();
}
答案 2 :(得分:0)
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter;
boolean isAdd= true;
Observer<List<StudentEntity>> myObserver = new Observer<List<StudentEntity>>() {
@Override
public void onChanged(@Nullable List<StudentEntity> studentEntities) {
lstStudents = studentEntities;
recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
if (isAdd){
recyclerView.setAdapter(recyclerViewAdapter);
isAdd= false;
}else {
recyclerView.getAdapter().notifyDataSetChanged();
Toast.makeText(getContext(),"New record added",Toast.LENGTH_SHORT).show();
}
}
};
在recyclerViewAdapter中尝试第一个设置列表,然后检查适配器是否为空,如果为null,则使用setAdapter方法,否则使用notifyDataSetChanged方法。
答案 3 :(得分:0)
notifyDataSetChanged()
顾名思义,只是通知适配器 您已经更新了数据集,因此可以继续重绘强>
实质上,您必须在通话notifyDataSetChanged()
在您的情况下,您的适配器应实现更新方法,如updateStudentList(List<StudentEntity> studentList)
MyStudentEntityRecyclerViewAdapter recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
Observer<List<StudentEntity>> myObserver = new Observer<List<StudentEntity>>() {
@Override
public void onChanged(@Nullable List<StudentEntity> studentEntities) {
//Update Listview adapter
lstStudents = studentEntities;
recyclerViewAdapter = new MyStudentEntityRecyclerViewAdapter(lstStudents, mListener);
if (recyclerView.getAdapter()!=null){
recyclerView.setAdapter(recyclerViewAdapter);
}else {
recyclerViewAdapter.updateStudentList(lstStudents); // You should update the students list before you call notifyDataSetChanged()
recyclerViewAdapter.notifyDataSetChanged();
Toast.makeText(getContext(),"New record added",Toast.LENGTH_SHORT).show();
}
}
};