Android的:从一个ListView访问子视图 [英] Android: Access child views from a ListView

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

问题描述

我需要找出一个元件的像素位置在一个已经使用的ListView 显示的列表。这似乎是我应该得到的一个在 TextView中的,然后使用共达(),但我想不出如何让一个子视图一个的ListView

I need to find out the pixel position of one element in a list that's been displayed using a ListView. It seems like I should get one of the TextView's and then use getTop(), but I can't figure out how to get a child view of a ListView.

更新:的孩子的ViewGroup 不符合1比1,在列表中的项目,为的ListView 。相反,的ViewGroup 的孩子对应只有那些可见,现在的看法。因此, getChildAt()运行在一个索引这是内部的的ViewGroup ,不一定有什么用在该的ListView 使用列表中的位置。

Update: The children of the ViewGroup do not correspond 1-to-1 with the items in the list, for a ListView. Instead, the ViewGroup's children correspond to only those views that are visible right now. So getChildAt() operates on an index that's internal to the ViewGroup and doesn't necessarily have anything to do with the position in the list that the ListView uses.

推荐答案

请参阅:<一href="http://stackoverflow.com/questions/2001760/android-listview-get-data-index-of-visible-item/2002413#2002413">Android ListView控件:可见项获取数据的索引 并与脚的回答部分结合上面,可以给你这样的:

See: Android ListView: get data index of visible item and combine with part of Feet's answer above, can give you something like:

int wantedPosition = 10; // Whatever position you're looking for
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
  Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
  return;
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
View wantedView = listView.getChildAt(wantedChild);

这样做的好处是,你是不是遍历ListView的孩子,这可能需要一个性能损失。

The benefit is that you aren't iterating over the ListView's children, which could take a performance hit.

这篇关于Android的:从一个ListView访问子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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