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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You can maintain the scroll state after a reload if you save the state before you reload and restore it after. In my case I made a asynchronous network request and reloaded the list in a callback after it completed. This is where I restore state. Code sample is Kotlin.
For some looking for a solution to this problem, the root of the issue may be where you are setting your list views adapter. After you set the adapter on the listview, it resets the scroll position. Just something to consider. I moved setting the adapter into my onCreateView after we grab the reference to the listview, and it solved the problem for me. =)
If you are saving/restoring scroll position of
ListView
yourself you are essentially duplicating the functionality already implemented in android framework. TheListView
restores fine scroll position just well on its own except one caveat: as @aaronvargas mentioned there is a bug inAbsListView
that won't let to restore fine scroll position for the first list item. Nevertheless the best way to restore scroll position is not to restore it. Android framework will do it better for you. Just make sure you have met the following conditions:setSaveEnabled(false)
method and not setandroid:saveEnabled="false"
attribute for the list in the xml layout fileExpandableListView
overridelong getCombinedChildId(long groupId, long childId)
method so that it returns positive long number (default implementation in classBaseExpandableListAdapter
returns negative number). Here are examples:.
ListView
orExpandableListView
is used in a fragment do not recreate the fragment on activity recreation (after screen rotation for example). Obtain the fragment withfindFragmentByTag(String tag)
method.ListView
has anandroid:id
and it is unique.To avoid aforementioned caveat with first list item you can craft your adapter the way it returns special dummy zero pixels height view for the
ListView
at position 0. Here is the simple example project showsListView
andExpandableListView
restore their fine scroll positions whereas their scroll positions are not explicitly saved/restored. Fine scroll position is restored perfectly even for the complex scenarios with temporary switching to some other application, double screen rotation and switching back to the test application. Please note, if you are explicitly exiting the application (by pressing the Back button) the scroll position won't be saved (as well as all other Views won't save their state). https://github.com/voromto/RestoreScrollPosition/releasesIsn't simply
android:saveEnabled="true"
in the ListView xml declaration enough?A very simple way:
The method setSelection will reset the list to the supplied item. If not in touch mode the item will actually be selected if in touch mode the item will only be positioned on screen.
A more complicated approach:
If you're using fragments hosted on an activity you can do something like this: