Android的ListView的y位置 [英] Android ListView y position

查看:89
本文介绍了Android的ListView的y位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像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.

这篇关于Android的ListView的y位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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