安卓列表视图y位置[英] Android ListView y position

本文是小编为大家收集整理的关于安卓列表视图y位置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

似乎listView不会以同样的方式公开其y-position.换句话说:我需要记住ListView被滚动的确切位置并在返回活动时设置它.

只是为了澄清:我不需要选定的项目......这非常直接.我需要在ListView上恢复精确的像素-Wise y位置.

谢谢!

推荐答案

我已成功使用此保存(在onSaveInstanceTate)并恢复(OnCreate)时,在切换方向时:

保存:

    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);
    }

这将精确地保存Y位置.好吧,它偶尔每次错过几个像素.

本文地址:https://www.itbaoku.cn/post/102709.html

问题描述

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.

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.

Thanks!

推荐答案

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

Saving:

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

Restoring:

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

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