通过屏幕旋转保持GridView控件的滚动位置控件、屏幕、位置、GridView

2023-09-05 06:15:39 作者:庝嘚無法哭

我怎样才能保持整个屏幕重新定位一个GridView控件(它充满了搜索结果)的滚动位置?

How can I maintain the scroll position of a GridView (it's filled with search results) throughout a screen reorientation?

我在GridView一个片段里,当我旋转屏幕就跳回到列表的顶部。

I have the GridView inside a Fragment, and when I rotate the screen it jumps back up to the top of the list.

要使它变得更加棘手,在横向模式下我的GridView有3列。在纵向模式下,它有2列。

To make it even more tricky, in landscape mode my GridView has 3 columns.. in portrait mode it has 2 columns.

我似乎无法找出如何保持片段的GridView控件滚动到附近的任何地方,它应该是屏幕的方向改变时。

I can't seem to figure out how to keep the fragment's GridView scrolled to anywhere near where it should be when the screen orientation changes.

推荐答案

您可以试试这个:

首先,你必须通过调用来获取GridView控件的当前滚动位置:

Initially you have to get the current scrolling position of the GridView by calling:

int index = gridview.getFirstVisiblePosition();

然后你要保存这个值,而取向的变化和GridView控件再次创建时,你有你的GridView控件移动到该索引。

Then you have to save this value while orientation changes and when the GridView is created again you have to move your gridview to this index.

我想这可能是工作:

gridview.smoothScrollToPosition(int index)

希望这有助于!

Hope this helps!

 
精彩推荐
图片推荐