回到上一个像ListView的Fragment之后,我可以保持ScrollView的位置吗? [英] Could I keep position of ScrollView after back to prev Fragment like ListView?

查看:58
本文介绍了回到上一个像ListView的Fragment之后,我可以保持ScrollView的位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试同时使用ScrollView和ListView.

I have tried to use both ScrollView and ListView.

我发现,如果我使用ListView并单击其中一项,则将当前片段替换为下一个片段,然后按返回按钮以显示上一个片段. ListView的滚动位置将保持在相同位置.但是,如果我使用ScrollView,则不会.

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.

我不明白为什么他们与众不同?按下后退按钮后,如何使ScrollView保持其位置?

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

实际上,我已经在StackOverflow上搜索了一些答案.但是我想知道原因,并找到一种使ScrollView和ListView具有相同行为的简便方法.

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.

请帮助我!谢谢!

推荐答案

只要分配了ID,片段就会自动保存和恢复其视图的状态.

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

因此,如果您为ScrollView分配一个ID,则可以恢复其滚动状态:

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

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

这篇关于回到上一个像ListView的Fragment之后,我可以保持ScrollView的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆