在 android- 列表视图中仅找出完全可见的列表项
Find out only completely visible list items in android- list view
我想找出与 ListView's
项相关的位置或 ID:仅那些在屏幕上完全可见的项。
使用 listview.getFirstVisibleposition
和 listview.getLastVisibleposition
会考虑部分列表项。
我还没有尝试过这个,但我相信这里有一些框架可以帮助您找到您正在寻找的东西(至少这是我首先尝试的)
- 如您所述,您应该使用
ListView.getLastVisiblePosition()
从列表视图中获取最后一个可见位置
- 然后您可以使用
ListView.getChildAt(position)
访问表示此位置的视图
- 您现在有了对视图的引用,您可以调用
View.getLocationOnScreen(location)
和 View.getHeight()
的组合
- 同时在 ListView 上调用
View.getLocationOnScreen(location)
和 View.getHeight()
。如果完全可见,View
的 y + height
应小于或等于 ListView
的 y + height
。
我遵循了 Rich 建议的有点类似的方法,以满足我的要求,即每次滚动列表视图时在屏幕上获取完全可见的项目。
这就是我所做的
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Loop to get tids of all completely visible List View's item scrolled on screen
for (int listItemIndex = 0; listItemIndex <= getListView().getLastVisiblePosition() - getListView().getFirstVisiblePosition(); listItemIndex++) {
View listItem = getListView().getChildAt(listItemIndex);
TextView tvNewPostLabel = (TextView) listItem.findViewById(R.id.tvNewPostLabel);
if (tvNewPostLabel != null && tvNewPostLabel.getVisibility() == View.VISIBLE) {
int listTid = (int) tvNewPostLabel.getTag();
if (listItem.getBottom() < getListView().getHeight()) {//If List View's item is not partially visible
listItemTids.add(listTid);
}
}
}
}
我想找出与 ListView's
项相关的位置或 ID:仅那些在屏幕上完全可见的项。
使用 listview.getFirstVisibleposition
和 listview.getLastVisibleposition
会考虑部分列表项。
我还没有尝试过这个,但我相信这里有一些框架可以帮助您找到您正在寻找的东西(至少这是我首先尝试的)
- 如您所述,您应该使用
ListView.getLastVisiblePosition()
从列表视图中获取最后一个可见位置
- 然后您可以使用
ListView.getChildAt(position)
访问表示此位置的视图
- 您现在有了对视图的引用,您可以调用
View.getLocationOnScreen(location)
和View.getHeight()
的组合
- 同时在 ListView 上调用
View.getLocationOnScreen(location)
和View.getHeight()
。如果完全可见,View
的y + height
应小于或等于ListView
的y + height
。
我遵循了 Rich 建议的有点类似的方法,以满足我的要求,即每次滚动列表视图时在屏幕上获取完全可见的项目。
这就是我所做的
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Loop to get tids of all completely visible List View's item scrolled on screen
for (int listItemIndex = 0; listItemIndex <= getListView().getLastVisiblePosition() - getListView().getFirstVisiblePosition(); listItemIndex++) {
View listItem = getListView().getChildAt(listItemIndex);
TextView tvNewPostLabel = (TextView) listItem.findViewById(R.id.tvNewPostLabel);
if (tvNewPostLabel != null && tvNewPostLabel.getVisibility() == View.VISIBLE) {
int listTid = (int) tvNewPostLabel.getTag();
if (listItem.getBottom() < getListView().getHeight()) {//If List View's item is not partially visible
listItemTids.add(listTid);
}
}
}
}