Android的ListView的y位置位置、Android、ListView

由网友(旧城的旧伤)分享简介:好像ListView控件不公开的滚动型做同样的方式其y位置。换句话说:我需要记住ListView控件已滚动并设置它的确切位置,当我返回到活动It seems like ListView doesn't expose its y-position in the same way a ScrollView does....

好像ListView控件不公开的滚动型做同样的方式其y位置。换句话说:我需要记住ListView控件已滚动并设置它的确切位置,当我返回到活动

It seems like ListView doesn't expose its y-position in the same way a ScrollView does. In other words: I need to remember the exact position the ListView was scrolled to and set it when I return to the activity.

只是为了澄清:我不需要选择的项目......这是pretty的直线前进。我需要恢复的ListView的确切逐像素的y位置。

Just to clarify: I don't need the selected item... that's pretty straight forward. I need to restore the exact pixel-wise y position on the ListView.

谢谢!

推荐答案

我已经保存(在的onSaveInstanceState)和恢复(的onCreate)时使用此成功,转换方向时:

I've used this successfully when saving (in onSaveInstanceState) and restoring (onCreate), when switching orientations:

保存:

    int savedPosition = list.getFirstVisiblePosition();
    View firstVisibleView = list.getChildAt(0);
    int savedListTop = (firstVisibleView == null) ? 0 : firstVisibleView.getTop();

恢复:

   if (savedPosition >= 0) { //initialized to -1
      list.setSelectionFromTop(savedPosition, savedListTop);
    }

这将precisely保存Y位置。那么,它在一段时间错过几个像素每一次。

This will precisely save the Y position. Well, it misses by a few pixels every once in a while.

阅读全文

相关推荐

最新文章