ListView点击事件无效

时间:2014-04-02 11:36:28

标签: android xml listview android-listview onitemclicklistener

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

<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" />

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);
    }

});

当我点击按钮然后工作正常但是当点击列表视图然后它不工作。所以任何解决方案..谢谢adavance ..

3 个答案:

答案 0 :(得分:2)

单击列表项

时,

ImageButton会获得焦点

添加以下

android:descendantFocusability="blocksDescendants" 

custome.xml

中的根元素

答案 1 :(得分:0)

您将使用此代码,它将要求在您完成之后投射您刚刚放置的对象.......

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

});

答案 2 :(得分:0)

添加android:clickable =&#34; true&#34;在textview和imagebutton中也是