是否有一个很好的理由,一个ListView头占用的位置? [英] Is there a good reason for a ListView header taking up a position?

查看:267
本文介绍了是否有一个很好的理由,一个ListView头占用的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚加入到我的ListView一个头,我不得不改变了一堆code,因为头本质上变成了位置0(意思是我的CursorAdapter的游标指数不与列表indicies排队它们是关闭的1现在)。为什么?这似乎有点傻了我。

I've just added a header to my ListView and I have to change a bunch of code because the header essentially becomes position 0 (Meaning the Cursor indices of my CursorAdapter do not line up with the indicies of the list. They are off by 1 now). Why? This seems a bit silly to me.

我能想出的唯一理由是,开发人员可能希望访问头。精细。提供类似getListView()。getHeader()。

The only reason I can come up with is that a developer may want to access the header. Fine. Provide something like getListView().getHeader().

推荐答案

由于某种原因,位置(从 onItemClick )被捆绑在了的ListView项目的数量不是适配器),包括页眉和页脚。如果设定了 OnItemClickListener 你应该通过调用检索点击项目 listView.getItemAtPosition(位置)而不是 adapter.getItem(位置)

For some reason the position (from the onItemClick) is tied up with the number of items in the ListView (not the adapter), including headers and footers. When you set an OnItemClickListener you should retrieve the clicked item by calling listView.getItemAtPosition(position) instead of adapter.getItem(position).

其实,你应该始终使用 getItemAtPosition ,因为这种方式并不重要,如果你的的ListView 有头和页脚,如果你添加新的标题,你不会需要改变你的code。

In fact, you should always use the getItemAtPosition, because that way not matter if your ListView has headers and footers, and if you add new headers you won't need to change your code.

如果你不想让你的头是可选的,你应该添加这样:​​ listView.addHeaderView(headerView,空,假的)

And if you don't want your header to be selectable, you should add it in this way: listView.addHeaderView(headerView, null, false).

这篇关于是否有一个很好的理由,一个ListView头占用的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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