RecyclerView.使用ViewModelProviders时,适配器无法访问CreateViewHolder



我的回收视图未到达CreateViewHolder或getItemCount。我已经检查并确保我的项计数不是0,我在适配器中添加了断点,但除了构造函数之外,没有其他方法被调用。

我看过的问题:

  • Recyclerview未调用CreateViewHolder
  • RecyclerView适配器的生命周期是什么

RecyclerView。适配器:

// I have added only the required methods.
public class JournalAdapter extends RecyclerView.Adapter<JournalAdapter.ViewHolder> {
public JournalAdapter(Context context, List<Map<String, Object>> diaryListMaps) {
this.mInflater = LayoutInflater.from(context);
this.diaryListMaps = diaryListMaps;
Log.d(TAG, "Adapter Size: " + diaryListMaps.size());
Log.d(TAG, String.valueOf(diaryListMaps));
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.jourrnal_card, parent, false);
JournalAdapter.ViewHolder viewHolder = new JournalAdapter.ViewHolder(view);
Log.d(TAG, "onCreateViewHolder");
return viewHolder;
}
@Override
public int getItemCount() {
if (diaryListMaps.size() == 0) {
Log.d(TAG, "getItem: " + 1);
return 1;
}
else {
Log.d(TAG, "getItem: " + 10);
return diaryListMaps.size();
}
}
}

片段:

mViewModel = ViewModelProviders.of(this).get(DiaryDashboardViewModel.class);
mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
diaryListMaps.addAll(journalList);
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
});
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
journalRecycler.setHasFixedSize(true);
journalRecycler.setLayoutManager(layoutManager);
journalRecycler.setAdapter(journalAdapter);

getJournals

public MutableLiveData<List<Map<String, Object>>> getJournals() {
Log.d(TAG, "getJournal");
dailyWeekColRef
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
List<Map<String, Object>> dummyList = new ArrayList<>();
for (QueryDocumentSnapshot documentSnapshot: Objects.requireNonNull(task.getResult())) {
Map<String, Object> dummyMap = documentSnapshot.getData();
Log.d(TAG, "Retrived data " + dummyMap);
dummyList.add(dummyMap);
}
journal.postValue(dummyList);
}
else {
Log.d(TAG, "task unsuccessful " + task);
}
});
return journal;
}

试试这个

mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
if(hournalAdapter==null){
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
LinearLayoutManager layoutManager = new 
LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false);
journalRecycler.setLayoutManager(layoutManager);
journalRecycler.setAdapter(journalAdapter);
} else {
diaryListMaps.clear();
diaryListMaps.addAll(journalList);
journalAdapter.notifyDataSetChanged();
}});

同时更改

@Override
public int getItemCount() {
return diaryListMaps.size();
}

代码的问题是,每当数据发生更改时,适配器都会一次又一次地启动,这不是正确的处理方式。您可以在observe方法上检查适配器是否为null,如果不为null,则只需更新列表并调用notifyDataSetChanged

请在调用setAdapter方法之前调用new JournalAdapter(getContext(), diaryListMaps);,并在观察者获得值时通知

mViewModel.getJournals().observe(getViewLifecycleOwner(), journalList -> {
Log.d(TAG, String.valueOf(journalList));
diaryListMaps.clear();
diaryListMaps.addAll(journalList);
journalAdapter.notifyDataSetChanged();
});

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.VERTICAL);
journalRecycler.setHasFixedSize(true);
journalRecycler.setLayoutManager(layoutManager);
journalAdapter = new JournalAdapter(getContext(), diaryListMaps);
journalRecycler.setAdapter(journalAdapter);

最新更新