如何从 Android ListView 获取第一个可见视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3110927/
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
How to get the first visible View from an Android ListView
提问by Janusz
Is there a way to get the first visible View out of the ListView in Android?
有没有办法从Android的ListView中获取第一个可见的View?
I can get the data that backs the first View in the Adapter but it seems I can't get the first View in ListView.
我可以获取支持 Adapter 中第一个 View 的数据,但似乎无法获取 ListView 中的第一个 View。
I want to change the first visible view after a scroll action finished. I know that I should not save references to the view.
我想在滚动操作完成后更改第一个可见视图。我知道我不应该保存对视图的引用。
回答by Fedor
Actually ListView items are just children of ListView. So first visible ListView item is:
实际上 ListView 项只是 ListView 的子项。所以第一个可见的 ListView 项目是:
listView.getChildAt(0)
回答by Austin Wagner
ListView has a function getFirstVisiblePosition
so to get the first visible view, the code would be:
ListView 有一个函数,getFirstVisiblePosition
所以要获得第一个可见视图,代码将是:
listView.getChildAt(listView.getFirstVisiblePosition());
listView.getChildAt(listView.getFirstVisiblePosition());
回答by Vijay C
Indeed listView.getChildAt(listView.getFirstVisiblePosition())
gives the first visible item,
BUT it could be half visiblelist item.
确实listView.getChildAt(listView.getFirstVisiblePosition())
给出了第一个可见项,
但它可能是一半可见的列表项。
To get first completely visible list item,
要获得第一个完全可见的列表项,
if (listView.getChildAt(0).getTop() < 0) {
int firstCompletelyVisiblePos = listView.getFirstVisiblePosition() + 1;
}
回答by Enyby
Object item = listView.getItemAtPosition(listView.getFirstVisiblePosition());
For first completely visible list item:
对于第一个完全可见的列表项:
int pos = listView.getFirstVisiblePosition();
if (listView.getChildCount() > 1 && listView.getChildAt(0).getTop() < 0) pos++;
Object item = listView.getItemAtPosition(pos);
回答by surya
You can use the following code:
您可以使用以下代码:
for (int i = 0; i <= conversationListView.getLastVisiblePosition() - conversationListView.getFirstVisiblePosition(); i++) {
View listItem = conversationListView.getChildAt(i);
}
回答by Sanal
listView.scrollBy(0, -40);
listView.scrollBy(0, -40);
This works very well
这很好用