FirebaseRecyclerAdapter and RecyclerView scroll position is lost on orientation change
See original GitHub issueEnvironment
- 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:
- 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:
- Created 6 years ago
- Comments:15 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
@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?