简体   繁体   English

获取ListView中滚动条的位置(而不是位置项)

[英]Get the position of scrollbars(not the position item) in a ListView

I am implementing a timeline UI, which is basically a ListView . 我正在实现一个时间轴UI,它基本上是一个ListView The tricky part is when the user scrolls the ListView , it will display a text box(shows the time slot, say 9:00 am ) next to the scrollbar. 棘手的部分是当用户滚动ListView ,它将在滚动条旁边显示一个文本框(显示时隙,例如9:00 am)。 It is on the left side, adjacent and will move with the scrollbar. 它在左侧,相邻并随滚动条一起移动。

I am using the OnScrollListener to detect the scroll event. 我正在使用OnScrollListener来检测滚动事件。 But I don't know how to get the exact position of the scroll bar (top, mid point, bottom), so that I can put the text box at the right place. 但是我不知道如何获取滚动条的确切位置(顶部,中点,底部),以便将文本框放在正确的位置。 There are many posts about getting the position of the top item but seems not for the scrollbar itself. 有很多关于获取顶部项目位置的文章,但似乎不适用于滚动条本身。

Any thoughts? 有什么想法吗?

BTW, sorry for not attaching a screenshot of the UI. 顺便说一句,很抱歉没有附加用户界面的屏幕截图。 Stackoverflow doesn't allow me to do that since I am a new user. 由于我是新用户,所以Stackoverflow不允许我这样做。

But I don't know how to get the exact position of the scroll bar (top, mid point, bottom), so that I can put the text box at the right place 但是我不知道如何获取滚动条的确切位置(顶部,中点,底部),以便将文本框放在正确的位置

You probably want something like the Path application. 您可能想要类似Path应用程序的东西。 This tutorial it what you're looking for(probably, if you didn't see it yet). 本教程是您要寻找的(可能,如果您尚未看到的话)。

Basically, the ListView has some methods regarding the scrollbars position(also size) that are used but those methods are not available from outside as they are declared as protected , so you need to extend the ListView class. 基本上, ListView有一些使用的有关滚动条位置(也包括大小)的方法,但是这些方法被声明为protected ,因此无法从外部使用,因此您需要扩展ListView类。 Those methods are ListView.computeVerticalScrollExtent() , ListView.computeVerticalScrollOffset() and ListView.computeVerticalScrollRange() . 这些方法是ListView.computeVerticalScrollExtent()ListView.computeVerticalScrollOffset()ListView.computeVerticalScrollRange()

Using those methods you should be able to figure where the scrollbars are placed at a particular moment in your listener. 使用这些方法,您应该能够确定滚动条在侦听器中特定时刻的放置位置。

getListView().getLastVisiblePosition() getListView()。getLastVisiblePosition()

listview.post(new Runnable() {
    public void run() {
        listview.getLastVisiblePosition();
    }
});

View.getScrollY() View.getScrollY()

getScrollY : Return the scrolled top position of this view. getScrollY :返回此视图的滚动顶部位置。 This is the top edge of the displayed part of your view. 这是视图显示部分的顶部边缘。 You do not need to draw any pixels above it, since those are outside of the frame of your view on screen. 您无需在其上方绘制任何像素,因为这些像素不在屏幕视图框架之内。

Returns The top edge of the displayed part of your view, in pixels. 返回值视图的显示部分的顶部边缘,以像素为单位。

Also refer : http://eliasbland.wordpress.com/2011/07/28/how-to-save-the-position-of-a-scrollview-when-the-orientation-changes-in-android/ 另请参阅: http : //eliasbland.wordpress.com/2011/07/28/how-to-save-the-position-of-a-scrollview-when-the-orientation-changes-in-android/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM