【问题标题】:ImageButton click event works, but ListView click event does notImageButton 点击​​事件有效,但 ListView 点击事件无效
【发布时间】:2014-04-02 11:36:28
【问题描述】:

ListView:

<ListView android:id="@+id/list11" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:layout_weight="1" 
          android:cacheColorHint="#00000000"> 
</ListView>

custome.xml:

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

    <TextView
        android:id="@+id/txt_groupname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_weight="3"
        android:text="asdasd"
        android:textColor="@color/TextColor" />

    <ImageButton
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/delete_gp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

Activity.java:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long itemValue = listView.getItemIdAtPosition(position);
        Log.e("==>", "" + itemValue);
    }

});

当我点击&lt;ImageButton&gt; 时它工作正常,但当我点击&lt;ListView&gt; 时它不起作用。

我该如何解决这个问题?

【问题讨论】:

  • 我的 Listview 是:-
  • 展示你的baseadapter。
  • @Go2 尝试 android:descendantFocusability="blocksDescendants" 作为custome.xml 中的根元素
  • @Raghunandan 如果我们将所有孩子都保留为android:focusable="false" 是否需要。? :-/
  • @SilentKiller 自己试试 ImageButton

标签: android xml listview android-listview onitemclicklistener


【解决方案1】:

ImageButton 在您单击列表项时获得焦点

添加以下内容

android:descendantFocusability="blocksDescendants" 

custome.xml中的根元素

【讨论】:

  • @Go2 还有什么返回listView.getItemIdAtPosition(position)。您需要将其转换为正确的类型。数据类型是什么?
  • @Go2 将其转换为 long 更好
【解决方案2】:

您将使用此代码,它会要求您投射您刚刚进行投射的对象,然后就可以了......

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long itemValue = parent.getItemIdAtPosition(position);
        Log.e("==>", "" + itemValue);
    }

});

【讨论】:

    【解决方案3】:

    在 textview 和 imagebutton 中添加 android:clickable="true"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多