Can I maintain a ScrollView position after returning to the previous fragment, for example ListView?

I tried using both ScrollView and ListView.

I realized that if I use ListView and I click on one of the elements, let the current fragment be replaced by the next fragment and click the "Back" button for the preliminary fragment. The scroll position of the ListView will be kept in the same position. But if I use ScrollView, it will not.

I do not understand why they are different? And how can I get ScrollView to maintain its position after clicking a button?

Actually, I was looking for an answer in StackOverflow. But I want to know the reasons and find an easier way to make ScrollView and ListView have the same behavior.

Please help me! Thanks!

+6
source share
1 answer

Fragments automatically save and restore the state of their representations, since they have identifiers.

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

<ScrollView android:id="@+id/scrollview" ...> 
+21
source

Source: https://habr.com/ru/post/975916/


All Articles