javaandroidandroid-recyclerviewadapter

Switching between Adapters in android


My recylerview is misbehavieng ie data of different adapters doesnt occupy space from previous adapter making data to skipp around going downwards even after calling 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.setAdapter(taskAdapter);
    taskAdapter.notifyDataSetChanged();
   page_title.setText("Tasks");
    return true

Tried applying notifyDataSetChanged();but still.Is there a way to refresh the ryclerview.


Solution

  • So, basically, I have decided to refresh the layout, and it works perfectly

    if (item.getItemId() == R.id.project) {
        page_title.setText("Projects");
        rcNotes.setLayoutManager(null);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
                2,
                StaggeredGridLayoutManager.VERTICAL
        );
        rcNotes.setLayoutManager(staggeredGridLayoutManager);
        rcNotes.setAdapter(projectAdapter);
        projectAdapter.notifyDataSetChanged();
        return true;
    } else if (item.getItemId() == R.id.task) {
        rcNotes.setLayoutManager(null);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
                2,
                StaggeredGridLayoutManager.VERTICAL
        );
        rcNotes.setLayoutManager(staggeredGridLayoutManager);
        rcNotes.setAdapter(taskAdapter);
        taskAdapter.notifyDataSetChanged();
        page_title.setText("Tasks");
        return true;
    }