Android:从 ListView 访问子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:06:25  来源:igfitidea点击:

Android: Access child views from a ListView

androidandroid-listview

提问by lacker

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'sand then use getTop(), but I can't figure out how to get a child view of a ListView.

我需要找出使用ListView. 似乎我应该获取TextView之一然后使用getTop(),但我不知道如何获得ListView.

Update:The children of the ViewGroupdo 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 ViewGroupand doesn't necessarily have anything to do with the position in the list that the ListViewuses.

更新:的子ViewGroup项与列表中的项目不一一对应,对于 a ListView。相反,ViewGroup的孩子只对应那些现在可见的视图。因此getChildAt(),对 内部的索引进行操作,ViewGroup并且不一定与ListView使用的列表中的位置有任何关系。

回答by Joe

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

请参阅:Android ListView:获取可见项目的数据索引并结合上面Feet的部分答案,可以为您提供类似的信息:

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

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

好处是您不会迭代 ListView 的子项,这可能会影响性能。

回答by Kalimah

This code is easier to use:

此代码更易于使用:

 View rowView = listView.getChildAt(viewIndex);//The item number in the List View
    if(rowView != null)
        {
           // Your code here
        }

回答by Feet

A quick search of the docs for the ListView class has turned up getChildCount() and getChildAt() methods inherited from ViewGroup. Can you iterate through them using these? I'm not sure but it's worth a try.

快速搜索 ListView 类的文档,发现了从 ViewGroup 继承的 getChildCount() 和 getChildAt() 方法。你能用这些迭代它们吗?我不确定,但值得一试。

Found it here

在这里找到

回答by Wes

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
        View v;
        int count = parent.getChildCount();
        v = parent.getChildAt(position);
        parent.requestChildFocus(v, view);
        v.setBackground(res.getDrawable(R.drawable.transparent_button));
        for (int i = 0; i < count; i++) {
            if (i != position) {
                v = parent.getChildAt(i);
                v.setBackground(res.getDrawable(R.drawable.not_clicked));
            }
        }
    }
});

Basically, create two Drawables- one that is transparent, and another that is the desired color. Request focus at the clicked position (int positionas defined) and change the color of the said row. Then walk through the parent ListView, and change all other rows accordingly. This accounts for when a user clicks on the listviewmultiple times. This is done with a custom layout for each row in the ListView. (Very simple, just create a new layout file with a TextView- do not set focusable or clickable!).

基本上,创建两个Drawables- 一个是透明的,另一个是所需的颜色。在单击的位置(int position如定义)请求焦点并更改所述行的颜色。然后遍历 parent ListView,并相应地更改所有其他行。这说明了用户何时点击listview多次。这是通过为ListView. (非常简单,只需创建一个带有TextView- 不要设置可聚焦或可点击的新布局文件!)。

No custom adapter required - use ArrayAdapter

无需自定义适配器 - 使用 ArrayAdapter

回答by Milaaaad

int position = 0;
listview.setItemChecked(position, true);
View wantedView = adapter.getView(position, null, listview);

回答by jasonhudgins

This assumes you know the position of the element in the ListView :

这假设您知道元素在 ListView 中的位置:

  View element = listView.getListAdapter().getView(position, null, null);

Then you should be able to call getLeft() and getTop() to determine the elements on screen position.

然后您应该能够调用 getLeft() 和 getTop() 来确定屏幕上的元素位置。