Android的ListView控件:获取可见项目的数据索引控件、索引、项目、数据

由网友(忆梦无尘)分享简介:我有一个Android 的ListView 以创建一个 SimpleAdapter 已在它更多的项目比适合在屏幕上。之后,名单已滚动,我需要在第一个可见项目的列表中的数据模型的位置。 I have an Android ListView created with a SimpleAdapter that has mo...

我有一个Android 的ListView 以创建一个 SimpleAdapter 已在它更多的项目比适合在屏幕上。之后,名单已滚动,我需要在第一个可见项目的列表中的数据模型的位置。

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().

适配器有一些功能在里面,如 getItemId(位置),看上去很有用;然而, 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 :)

阅读全文

相关推荐

最新文章