listview或gridview中item的点击事件与控件点击事件冲突的解决

本文介绍了解决ListView中item点击事件与内部控件点击事件发生冲突的方法。提供了两种有效方案:一是通过设置父View的descendantFocusability属性为blocksDescendants;二是将子控件的focusable属性设置为false。

listview中item的点击事件与控件点击事件冲突的解决:
遇到的问题:listview条目点击事件无法处理事件。listview里的控件抢先收到了事件。
方式1:设置包含所有控件的父view的 descendantFocusability属性。android:descendantFocusability=”blocksDescendants”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"   
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="80dp"    
        android:layout_height="wrap_content" />

</LinearLayout>

2.方式二。设置 子控件的android:focusable=”false” 属性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="80dp"    
        android:focusable="false"
        android:layout_height="wrap_content" />

</LinearLayout>

总结来说,是事件分发与消费问题。由于时间限制,先列出解决方案。以后完善分析。

在单击 GridView 控件中的按钮时,将引发 RowCommand 事件。 GridView 控件具有内置功能,用于进行编辑、删除和分页等操作。 还可以添加按钮并使用 RowCommand 事件向控件添加自定义功能。 可以通过下面的方式向 GridView 控件添加自定义功能: 向 GridView 控件添加 ButtonField 字段。 向 GridView 控件中的模板添加 Button、LinkButton ImageButton 控件。 可以使用事件参数的 CommandName 属性在事件处理程序方法中标识按钮的功能。 如果使用的是 ButtonField TemplateField 对象,则还可以使用 CommandArgument 属性来标识当前行。 使用的是 ButtonField 对象时,CommandArgument 属性自动设置为行索引。 使用的是 TemplateField 对象时,控件不会自动设置 CommandArgument 属性。 在这种情况下,如果必须在事件处理程序中确定行索引,则可以使用数据绑定表达式将该按钮的 CommandArgument 属性设置为行索引。 响应 GridView 控件中的按钮事件 将按钮的 CommandName 属性设置为标识其功能的字符串,如“打印”“复制”。 如果使用的是 TemplateField 对象并且必须在事件处理程序方法中访问行索引,则将按钮的 CommandArgument 属性设置为标识当前行的表达式。 下面的示例演示如何将 TemplateField 列中某个按钮的 CommandArgument 属性设置为当前行索引。 在该示例中,该列包含一个显示购物车的 Button 控件。 VBC#C++F#JScript 复制不支持该语言没有可用的代码示例。 VBC#C++F#JScript 复制 <asp:Button ID="AddButton" runat="server" CommandName="AddToCart" CommandArgument="" Text="Add to Cart" /> 为 GridView 控件的 RowCommand 事件创建一个方法。 在该方法中,执行下列操作: 检查事件参数对象的 CommandName 属性来查看传入什么字符串。 如果需要,使用 CommandArgument 属性检索包含该按钮的行的索引。 为用户单击的按钮执行相应的逻辑。 下面的示例演示响应 GridView 控件中的按钮单击的方法。 在该示例中,TemplateField 列中的按钮发送命令“AddToCart”。 RowCommand 事件处理程序确定被单击的按钮。 如果被单击的是购物车按钮,则代码执行相应的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值