Frage

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

});

When I click on the <ImageButton> it works fine, but when I click on <ListView> it does not work.

How can I solve this problem?

War es hilfreich?

Lösung

ImageButton takes focus when you click on list item

Add the below

android:descendantFocusability="blocksDescendants" 

to the root element in custome.xml

Andere Tipps

You will use this code and it will ask to cast the object you just put the casting after it will fine....

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

});

add android:clickable="true" in the textview and the imagebutton also

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top