Android ListView:获取可见项的数据索引 [英] Android ListView: get data index of visible item

查看:25
本文介绍了Android ListView:获取可见项的数据索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 SimpleAdapter 创建的 Android ListView,其中包含的项目多于屏幕尺寸.滚动列表后,我需要获取列表中第一个可见项在数据模型中的位置.

I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need to get the position in the data model of the first visible item in the list.

基本上我想要一个类似的函数:listView.getChildAt(0).getPositionInDataModel().

Basically I want a function like: listView.getChildAt(0).getPositionInDataModel().

Adapter 里面有一些函数,比如 getItemId(position) 看起来很有用;但是, SimpleAdapter 实现只返回传入的位置,而不是我希望的行 ID.

Adapter has a few functions in it, like getItemId(position) that looked useful; however, the SimpleAdapter implementation just returns the passed in position, not a row id like I'd hoped.

一个蛮力解决方案是在索引 0 处获取视图,并将其与适配器中每个项目的视图进行比较.但是,似乎没有一种简单的方法可以从适配器获取特定位置的视图.

A brute force solution would be to get the View at index 0, and compare it to the view for each item in the adapter. However, there doesn't seem to be an easy way to get the view for a particular position from the adapter.

有人有什么想法吗?

推荐答案

这很容易.只需使用 ListView.getFirstVisiblePosition() + indexYouWant.例如,要获取 ListView 中显示的第二个孩子在适配器中的位置,只需使用 getFirstVisiblePosition() + 1.

It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.

不需要上面回复中显示的所有可怕的东西:)

No need for all the scary stuff shown in the reply above :)

这篇关于Android ListView:获取可见项的数据索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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