是否有一个很好的理由,一个ListView头占用的位置?很好、有一个、理由、位置

2023-09-06 02:29:30 作者:上屋顶撩汉

我刚刚加入到我的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).