7

I have a ListView with custom list adapter. It has OnItemClickListener and OnItemLongClickListner which used to work fine. After then, I had to put a button in the layout of list item and the item click and long click listener stopped working. Here is my sample code:

ListView lv=(ListView)findViewbyId(R.id.listview); 
lv.setAdapter(listviewadapter);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
            // My code
            }
});

This used to work fine before adding the button in the layout of list item:

<?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="wrap_content"
    android:orientation="horizontal" 
    android:padding="2dp"
    >
    <TextView
        android:id="@+id/symbol_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2.5"
        android:layout_gravity="left"
        />
    <TextView
        android:id="@+id/ltp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/change_in_perc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/volume"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        />

    <ImageButton
        android:id="@+id/chart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0.5"
        android:src="@drawable/charts"
        android:contentDescription="Chart Link"
        />
</LinearLayout>

I tried changing inline listener to implementing onItemLongClickListener on activity but no success till now. Thanks.

Sourabh
  • 1,757
  • 6
  • 21
  • 43
  • This might be the case. i am trying it. But if that would be taking the focus, shouldn't the click event of button be triggered when I click the list view? – Sourabh Jan 05 '14 at 09:54
  • 2
    This works.. Please add this as your answer so that I can accept it. – Sourabh Jan 05 '14 at 09:55

2 Answers2

13

Your image button is probably taking focus when you click on list item.

So Add this

android:descendantFocusability="blocksDescendants" 

to your root element

http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

Constant    Value   Description
beforeDescendants   0    The ViewGroup will get focus before any of its descendants.
afterDescendants    1    The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants   2    The ViewGroup will block its descendants from receiving focus.
This corresponds to the global attribute resource symbol descendantFocusability.
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    Thanks a lot. Accepting this answer as soon as stackoverflow allows. :) – Sourabh Jan 05 '14 at 09:58
  • Please help me. I have been stuck on this for the last 7 hours now, and it is such a simple question. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – Ruchir Baronia Jan 31 '16 at 03:07
0

did you set the android:longClickable="true" in the xml and the listview.setLongClickable(true); in the code ?

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • Please help me. I have been stuck on this for the last 7 hours now, and it is such a simple question. http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – Ruchir Baronia Jan 31 '16 at 03:07