如何获得在列表视图排位置(在屏幕上) [英] How to get location (on screen) of row in listview

查看:119
本文介绍了如何获得在列表视图排位置(在屏幕上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一些手势的检测code,这样我可以检测到一排在我的列表视图(这是坐落在的FrameLayout)已被偷走。

I have implemented some gesture detection code so that I can detect when a row in my listview (which is situated in a FrameLayout) has been swiped.

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        int itemId = MainActivity.this.getListView().pointToPosition((int) e1.getX(),(int) e1.getY());
        Offer order = (Offer)MainActivity.this.getListView().getAdapter().getItem(itemId);
        View v = MainActivity.this.getListView().getChildAt(itemId);
    }
}

我要显示一个视图在刷卡行的顶部与该行一组上下文相关的选项。

I want to display a view over the top of the swiped row with a set of context sensitive options for that row.

我的问题是,以下方法:

My problems is that the following methods:

v.getTop()
v.getBottom()

返回查看已滚动只是之前正确的价值观。我大概可以做一些计算摸出使用滚动位置等补偿,但我也注意到,我只得到价值时,我刷一排就是在屏幕上开始显示。如果我向下滚动列表,然后轻扫行(这不是原本关闭屏幕),则这些方法将返回空值。

return correct values only before the view has been scrolled. I could probably do some calculation to work out offsets using scroll positions etc but I also noticed that I only get values when I swipe a row that is visible on screen to begin with. If I scroll down the list and then swipe a row (that was not originally off-screen) then these methods return null values.

下面的方法似乎从同样的问题遭受...

The methods below seem to suffer from the same problem...

v.getLocationOnScreen(loc)
v.getLocationInWindow(loc)

最后,我希望找到可见的ListView的顶部,已经刷卡的行之间的距离。然后,我会用这个距离将视图添加到父的FrameLayout与适当的高度填充(以便它涵盖了刷卡行)。

Ultimately, I am looking to find the distance between the top of the visible listView and the row that has been swiped. I will then use this distance to add a view to the parent FrameLayout with the appropriate height padding (so that it covers the swiped row).

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

有两种不同的索引的ListView项目的方式,按位置的调节器,或者在屏幕上可见的地方。一个项目总是会在适配器在同一位置(除非你改变的列表)。该指数为view.getChildAt()将取决于什么是显示在屏幕上有所不同。

There are two different ways of indexing ListView items, by position in the adapter, or by visible place on the screen. An item will always be at the same position within the adapter (unless you alter the list). The index into view.getChildAt() will vary depending on what is visible on the screen.

由pointToPosition被返回的位置是在适配器内的位置。如果你想要得到的物理视图,你需要考虑如果视图滚动的。试试这个:

The position being returned by pointToPosition is the position within the adapter. If you want to get the physical view, you need to account for if the view is scrolled. Try this:

int adapterIndex = MainActivity.this.getListView().pointToPosition((int) e1.getX(),(int) e1.getY());
int firstViewItemIndex = MainActivity.this.getListView().getFirstVisiblePosition();
int viewIndex = adapterIndex - firstViewItemIndex;
View v = MainActivity.this.getListView().getChildAt(viewIndex);

这篇关于如何获得在列表视图排位置(在屏幕上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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