转载地址:http://blog.iamzsx.me/show.html?id=147001

我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget.AdapterView.OnItemClickListener。本文主要在于对OnItemClickListener的position和id参数做详细的解释,我相信有些人在这上面走了些弯路。

先来看一下官方的文档
position The position of the view in the adapter.
id The row id of the item that was clicked.
而这两行字并没有解释清楚position和id的区别。另外,我们还有个Adapter的getView方法。

public abstract View getView (int position, View convertView, ViewGroup parent)

这里也有一个position。
 
初步接触ListView的同学,一般会直接继承ArrayAdapter,然后(比如我),就想当然的认为OnItemClick的position和getView的position是一样的啊。于是我们就getItem(position)来获取相应的数据。
 
那么这段代码有没有错呢?如果有错的话,在什么情况会出错呢?
第一个问题的答案是,当我们为ListView添加headerView或者footerView之后,这段代码就不一定是我们想要的了。
 
出现问题的原因在于,当我们为ListView添加headerView或者footerView之后,ListView在setAdapter时,做了一些事情,这导致,Adapter和OnItemClickListener中的position含义发生了变化。
 
我们可以来看看ListView中setAdapter的实现
437  public void setAdapter(ListAdapter adapter) {
438 if (mAdapter != null && mDataSetObserver != null) {
439 mAdapter.unregisterDataSetObserver(mDataSetObserver);
440 }
442 resetList();
443 mRecycler.clear();
445 if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
446 mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
447 } else {
448 mAdapter = adapter;
449 }
可以看出,如果这个ListView存在headerView或者footerView的话,那么会在我们传入的adapter外面在封装一层HeaderViewListAdapter,这是一个专门用来自动处理headerView和footerView的adapter。在ListView中,本身不区分headerView,footerView。ListView可以理解成是只负责管理一组View的数组的UI(ViewGroup),headerView和footerView都委托给HeaderViewListAdapter来处理。(从这里也可以看到为什么API文档中提到,addFooterView和addHeaderView要在setAdapter函数之前调用,如果在之后调用,那么就不会生成HeaderViewListAdapter,从而导致显示不出headerView和footerView)。
 
回到开头的问题,position和id有啥区别。为此,我们找一下position和id是怎么传进来的。
OnItemClickListener在android.widget.AdapterView的public boolean performItemClick(View view, int position, long id)函数中被调用。
performItemClick在android.widget.AbsListView.PerformClick.run() 中被调用
2497  private class PerformClick extends WindowRunnnable implements Runnable {
2498 int mClickMotionPosition;
2500 public void run() {
2501 // The data has changed since we posted this action in the event queue,
2502 // bail out before bad things happen
2503 if (mDataChanged) return;
2505 final ListAdapter adapter = mAdapter;
2506 final int motionPosition = mClickMotionPosition;
2507 if (adapter != null && mItemCount > 0 &&
2508 motionPosition != INVALID_POSITION &&
2509 motionPosition < adapter.getCount() && sameWindow()) {
2510 final View view = getChildAt(motionPosition - mFirstPosition);
2511 // If there is no view, something bad happened (the view scrolled off the
2512 // screen, etc.) and we should cancel the click
2513 if (view != null) {
2514 performItemClick(view, motionPosition, adapter.getItemId(motionPosition));
2515 }
2516 }
2517 }
2518 }
可以看到,position事实上就是ListView中被点击的view的位置。注意,在ListView中是不负责处理headerView和footViewer的,所以,这个位置应该是这个被点击的view在数组[所有的headerView,用户添加的view,所有的footerView]中的位置(请自行参考HeaderViewListAdapter的getView实现)。而id是来自于adapter.getItemId(position)。
 
对于ArrayAdapter的getItemId函数,实现就是return position。id和position是一致的。
然而,对于HeaderViewListAdapter

 
188  public long getItemId(int position) {
189 int numHeaders = getHeadersCount();
190 if (mAdapter != null && position >= numHeaders) {
191 int adjPosition = position - numHeaders;
192 int adapterCount = mAdapter.getCount();
193 if (adjPosition < adapterCount) {
194 return mAdapter.getItemId(adjPosition);
195 }
196 }
197 return -1;
198 }
 
实现逻辑是,如果position指向了headerView或footerView,那么返回-1,否则,将返回在用户view数组的位置。
也就是说
id=position-headerView的个数(id < headerviewer的个数+用户view的个数),否则=-1
因此,OnItemClickListener的正确实现如下:
void onItemClick(AdapterViewparent, View view, int position, long id){
if(id<) {
// 点击的是headerView或者footerView
return;
}
int realPosition=(int)id;
T item=getItem(realPosition);
// 响应代码
}
 

OnItemClickListener 的参数详解(转)的更多相关文章

  1. Nginx主配置参数详解,Nginx配置网站

    1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...

  2. iptables参数详解

    iptables参数详解 搬运工:尹正杰 注:此片文章来源于linux社区. Iptalbes 是用来设置.维护和检查Linux内核的IP包过滤规则的. 可以定义不同的表,每个表都包含几个内部的链,也 ...

  3. chattr的常用参数详解

    chattr的常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在实际生产环境中,有的运维工程师不得不和开发和测试打交道,在我们公司最常见的就是部署接口.每天每个人部署的 ...

  4. mha配置参数详解

    mha配置参数详解: 参数名字 是否必须 参数作用域 默认值 示例 hostname Yes Local Only - hostname=mysql_server1, hostname=192.168 ...

  5. $.ajax()方法所有参数详解;$.get(),$.post(),$.getJSON(),$.ajax()详解

    [一]$.ajax()所有参数详解 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注 ...

  6. linux PHP 编译安装参数详解

    linux PHP 编译安装参数详解 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...

  7. 【转】jqGrid 各种参数 详解

      [原文]http://www.cnblogs.com/younggun/archive/2012/08/27/2657922.htmljqGrid 各种参数 详解 JQGrid JQGrid是一个 ...

  8. HTML滚动字幕代码参数详解及Js间隔滚动代码

    html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...

  9. mysql5.6主从参数详解

    mysql5.6的主从相当的不错,增加了不少参数,提升了主从同步的安全和效率,以下是mysql5.6主从参数详解. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

随机推荐

  1. lintcode:两个数的和

    题目 两数之和 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是1到n,不 ...

  2. lintcode:哈希函数

    题目: 哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  3. 为什么重写equals方法还要重写hashcode方法?

    我们都知道Java语言是完全面向对象的,在java中,所有的对象都是继承于Object类.Ojbect类中有两个方法equals.hashCode,这两个方法都是用来比较两个对象是否相等的. 在未重写 ...

  4. Sina App Engine(SAE)入门教程(1)

    此教程只针对刚接触SAE的小白用户,资深码农.高手请绕道.首先还是一个经典的实例,hello sae. 创建应用 在注册完账号之后,需要到 http://sae.sina.com.cn/?m=myap ...

  5. pinyin4j

    最近在倒腾与搜索相关的拼音检查技术,顺便看了一下中文转拼音开源插件pinyin4j的源码,参考资料:http://blog.csdn.net/hfhwfw/archive/2010/11/23/603 ...

  6. 新装的win7 64位系统上装了IE11,想调试网页的时候,按F12,工具会出来,但是没法正常使用,出现空白。

    Windows专区开了一帖,没人应.这边再开一帖,看看各位遇到过没.如题,新装的win7 64位系统上装了IE11,想调试网页的时候,按F12,工具会出来,但是没法正常使用.尤其是想切换文档模式,只能 ...

  7. chrome控制台小技巧

    对于大多数开发人员来说,chrome控制台最常用的命令就是 console.log()了,然后还有一些其他类似的命令,如: console.info()   提示信息 console.error() ...

  8. maximum-gap(经过了提示)

    下面的分桶个数做的不太好,原来的解法是用的 int gap = (big - small) / vlen; if (gap == 0) { gap = 1; } 下面是现在的Java解法: packa ...

  9. objcopy

    objcopy objcopy [options] infile [outfile] Copy the contents of the input object file to another fil ...

  10. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...