您的位置:首页 > 其它

Item的click事件和Item里的button等子控件的click事件冲突的解决办法

2014-11-27 09:25 344 查看
 在listview中的listitem设置事件响应,如果listitem中有button控件,这时候listitem就不会捕获到点击事件,而默认的是listitem中的button会捕获点击事件,也就是说item的点击事件被button按钮屏蔽了,解决的方法如下:

一、设置Tag

 1、在listitem中初始化button的时候,给该button添加一个setTag方法,将此时的索引值传进去,然后在button的onclick事件中调用view的getTag方法,即可将listitem的索引读出来,代码如下:

    tagButton.setTag(position); 此处的tagButton就是定义的button,Position是view里边的位置。

 2、初始化button的时候通过setTag方法传入一个item的索引值

     private OnClickListener tagButtonOnClick = new OnClickListener() {    

           public void onClick(View v) {  

             final int index = (Integer) v.getTag();  

      }

    }

 index就是点击button所在Item中的位置,通过这个位置就可以得到Item中的值。

二、修改xml属性

    在ListView的item的xml配置文件的根节点添加属性

android:descendantFocusability="blocksDescendants",

并且,在要添加事件的子控件(如button)的属性里添加android:focusable="false"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐