question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FirebaseRecyclerAdapter and RecyclerView scroll position is lost on orientation change

See original GitHub issue

Environment

  • Android device: API 27 Emulator / Google Pixel
  • Android OS version: 27 / 8.0.0
  • Google Play Services version: whatever the emulator has / 11.7.45 (940-174122868)
  • Firebase/Play Services SDK version: 11.4.2
  • FirebaseUI version: 3.1.0

The problem:

On orientation change the recycler view using FirebaseRecyclerAdapter looses it’s scroll position and is always scrolled to the top.

Steps to reproduce:

  1. In the https://github.com/firebase/FirebaseUI-Android/blob/master/app/src/main/java/com/firebase/uidemo/database/realtime/RealtimeDbChatActivity.java#L110 comment out:
        // Scroll to bottom on new messages
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                mRecyclerView.smoothScrollToPosition(adapter.getItemCount());
            }
        });

so it does not get execute. This is needed so there is no extra logic forcing a scroll position. 2. Build the demo app 3. Add a bunch of chat messages, a lot so you get a scrolling regardless of orientation 4. Scroll neither to the top nor the bottom 5. Rotate the emulator

Observed Results:

  • After rotation the recycler view lost it’s scroll position and is scrolled to the top.

Expected Results:

  • The recycler view should keep it’s scroll position on orientation change.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
ncheriancommented, Nov 7, 2017

@SUPERCILEX I solved my problem with a simpler solution. I just called adapter.startListening(); in onViewCreated() instead of onStart and called adapter.stopListening(); in onDestroyView() instead of onStop() That prevented the entire list from regenerating while coming back from next activity and thus retained the scroll position where it was previously.

1reaction
ashfaaaacommented, Nov 10, 2017

@SUPERCILEX @ncherian hi im new to android development… how can i get it done? can you give me detailed instruction on how can i get it done it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restore recyclerview scroll position on screen orientation ...
If you have the recycler view in a activity try saving the recyclerViewAdapter in a ViewModel of the activity and on orientation change...
Read more >
Flexible UIs: Alternative Resources - LearnHowToProgram.com
First we'll create an alternate landscape-orientation layout for our existing ... be inflated when the phone's orientation changes to landscape!
Read more >
how to keep recyclerview scrolled position when i back from ...
if the layout manager and adapter retain their correct values during navigation then it would keep the scroll position even after the view ......
Read more >
android - How to build a horizontal ListView with RecyclerView ...
Just use a LinearLayoutManager with orientation set to HORIZONTAL . ... I was struggling as well with actually using a horizontally scrolling RecyclerView....
Read more >
I've tried to make a RecyclerView with an Adapter but I cannot ...
It's a bit hard to debug without the xml. But here goes nothing: I think it'd be worth a shot modifying your view...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found