Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could I keep position of ScrollView after back to prev Fragment like ListView?

I have tried to use both ScrollView and ListView.

I figured out that if I use ListView and I click one of items let current fragment be replaced by next fragment and press back button to prev fragment. The scroll position of ListView will keep at same position. But if I use ScrollView, it will not.

I don't understand why they are different? And how can I make ScrollView keep its position after press back button?

Actually, I have searched some answer at StackOverflow. But I want to know the reasons and find an easier way to make ScrollView and ListView have same behavior.

Please help me! Thank you!

like image 562
Azure Chen Avatar asked Sep 27 '14 02:09

Azure Chen


People also ask

How can I retain the scroll position of a scrollable area when pressing back button?

You can try $("div#element"). load(function() {}) and place the element outside the document ready handler. Also, if a user scroll down the page, then leave the page, and go back to the page again, it should be a new start for him so it should start at the top. But now it also remembers the previous position.

What is the difference between ListView and ScrollView?

ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.

Is ScrollView a Viewgroup?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

Is ListView scrollable by default?

ListView itself is scrollable.


1 Answers

Fragments automatically save and restore the states of their Views, as long they have IDs assigned.

So if you assign an ID to your ScrollView, its scroll state can be restored:

<ScrollView android:id="@+id/scrollview"     ...> 
like image 111
Floern Avatar answered Sep 20 '22 12:09

Floern