Skip to content

RecyclerView automatically scrolls to the bottom when data is loaded the first time because of call to notifyItemInserted(index). #886

Description

@muddassir235

I was trying to implement a vertical recyclerview laid out from top to bottom with a firebase node as a data source. I implemented it, but when the data got loaded the list always scrolled to the bottom. I looked into the FirebaseUIDatabase code and discovered the following calls.

@Override
public void onChildChanged(ChangeEventListener.EventType type,
                            DataSnapshot snapshot,
                            int index,
                            int oldIndex) {
     switch (type) {
         case ADDED:
             notifyItemInserted(index); // This causes the list to scroll to the bottom
             break;
         case CHANGED:
             notifyItemChanged(index);
             break;
         case REMOVED:
             notifyItemRemoved(index);
             break;
         case MOVED:
             notifyItemMoved(oldIndex, index);
             break;
         default:
             throw new IllegalStateException("Incomplete case statement");
     }
}

In a top to bottom layout this isn't desired.

I have implemented a solution for this and wanted to share my code.

          // ....
          case ADDED:
              if(mClipTopFirstTime) notifyDataSetChanged(); else notifyItemInserted(index);
              break;
          // .....

The mClipTopFirstTime variable, if true initially, would allow children to be added without scrolling to them. After a certain delay it would be set to false, afterwards, the default behaviour of scrolling to newly added children would be restored.

Should I make a pull request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions