我的 recylerview 行为不当,即不同适配器的数据不占用前一个适配器的空间,即使在调用notify datsetchange ie之后,数据也会向下跳过:
if (item.getItemId() == R.id.project) {
page_title.setText("Projects");
rcNotes.setAdapter(projectAdapter);
projectAdapter.notifyDataSetChanged();
return true;
} else if (item.getItemId() == R.id.task) {
rcNotes.clearOnChildAttachStateChangeListeners();
rcNotes.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
page_title.setText("Tasks");
return true
尝试应用notifyDataSetChanged();但仍然。有没有办法刷新ryclerview。
所以基本上我决定刷新布局,它工作得非常完美
if (item.getItemId() == R.id.project) { page_title.setText("项目"); rcNotes.setLayoutManager(null); StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); rcNotes.setLayoutManager(staggeredGridLayoutManager); rcNotes.setAdapter(projectAdapter);项目适配器.notifyDataSetChanged();返回真; } else if (item.getItemId() == R.id.task) { rcNotes.clearOnChildAttachStateChangeListeners(); rcNotes.setLayoutManager(null); StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); rcNotes.setLayoutManager(staggeredGridLayoutManager); rcNotes.setAdapter(taskAdapter); taskAdapter.notifyDataSetChanged(); page_title.setText("任务");返回真; }