问题
I have ListView
inside NestedScrollView
(do not want use RecycleListView) in my fragment. List adapter inits in onCreate
and not recreated when app back to the fragment from other fragment. But my NestedScrollView then scrolls to the top (can I prevent this?). Without NestedScrollView
my ListView stay at the same position.
回答1:
You should retain the list status in onPause()
method on the Fragment (save it to the Bundle) and then read and set it in onResume()
. Just create some constant key, save then read :-)
The value you will want to save is getScrollY()
most probably. Then you will want to restore it with scrollTo(int x, int y)
where x=0 and y=savedValue.
Also, you do want to use RecyclerView
:-) It is really cool and works really well with NestedScrollView
and CoordinatorLayout
. Also, it is much easier and quicker to implement than ListView
once you get the hang of it. It is also very lightweight. And I had once seen it save a puppy from a fire. RecyclerView is da man! :-D
回答2:
Remove @string/appbar_scrolling_view_behavior form the NestedScrollView layout.
来源:https://stackoverflow.com/questions/38438429/prevent-nestedscrollview-scrolls-to-top-automatically