通过屏幕旋转保持GridView的滚动位置 [英] Maintain Scroll Position of GridView through Screen Rotation

查看:22
本文介绍了通过屏幕旋转保持GridView的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在整个屏幕重定向过程中保持 GridView(它充满了搜索结果)的滚动位置?

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

我在 Fragment 中有 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)

希望这有帮助!

这篇关于通过屏幕旋转保持GridView的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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