I have a long
ListView that the user can scroll around before returning to the previous screen. When the user opens this
ListView again, I want the list to be scrolled to the same point that it was previously. Any ideas on how to achieve this?
// save index and top position int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null) ? 0 : (v.getTop() - mList.getPaddingTop()); // ... // restore index and position mList.setSelectionFromTop(index, top);
ListView.getFirstVisiblePosition() returns the top visible list item. But this item may be partially scrolled out of view, and if you want to restore the exact scroll position of the list you need to get this offset. So
ListView.getChildAt(0) returns the
View for the top list item, and then
View.getTop() - mList.getPaddingTop() returns its relative offset from the top of the
ListView. Then, to restore the
ListView's scroll position, we call
ListView.setSelectionFromTop() with the index of the item we want and an offset to position its top edge from the top of the
- How to create curved bottom border rectangle in android?
- SVG issue with Glide using Shorthand Hexadecimal Colors
- Gradle error : Write access is allowed from event dispatch thread only in android studio
- 'Start rollout to beta' disabled in Play Store Developer Console
- CardView background color always white
- Error:Execution failed for task ':app:packageDebug'. > !zip.isFile()
- Generating Android build with Gitlab CI
- How to turn On/OFF closed captions in HLS Streaming URL in Exoplayer?
- What is AndroidX
- Choose a Valid JDK Directory