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
Neither of the solutions offered here seemed to work for me. In my case, I have a
ListView
in aFragment
which I'm replacing in aFragmentTransaction
, so a newFragment
instance is created each time the fragment is shown, which means that theListView
state can not be stored as a member of theFragment
.Instead, I ended up storing the state in my custom
Application
class. The code below should give you an idea how this works:The basic idea is that you store the state outside the fragment instance. If you don't like the idea of having a static field in your application class, I guess you could do it by implementing a fragment interface and storing the state in your activity.
Another solution would be to store it in
SharedPreferences
, but it gets a bit more complicated, and you would need to make sure you clear it on application launch unless you want the state to be persisted across app launches.Also, to avoid the "scroll position not saved when first item is visible", you can display a dummy first item with
0px
height. This can be achieved by overridinggetView()
in your adapter, like this:For an activity derived from ListActivity that implements LoaderManager.LoaderCallbacks using a SimpleCursorAdapter it did not work to restore the position in onReset(), because the activity was almost always restarted and the adapter was reloaded when the details view was closed. The trick was to restore the position in onLoadFinished():
in onListItemClick():
in onLoadFinished():
in onBackPressed():
My answer is for Firebase and position 0 is a workaround
and convertView
position 0 a add a blank item that you are not using
I have seen this work temporarily without disturbing the user, I hope it works for you
BEST SOLUTION IS:
YOU MUST CALL IN POST AND IN THREAD!
Try this:
Explanation:
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. SoListView.getChildAt(0)
returns theView
for the top list item, and thenView.getTop() - mList.getPaddingTop()
returns its relative offset from the top of theListView
. Then, to restore theListView
's scroll position, we callListView.setSelectionFromTop()
with the index of the item we want and an offset to position its top edge from the top of theListView
.