ListView OnItemClickListener Not Responding?
Asked Answered
A

12

45

I've looked everywhere for a solution to this, but I can't figure out how to implement it. My OnItemClickListener was disabled somehow on my ListView rows, because I have an ImageButton in the row layout, which takes over the focus. There have been numerous questions I've found, but none of them have gotten me anywhere.

I've checked this question, but I couldn't really make heads or tails of it. I just need a way to get the rows clickable so that I can detect when a row is pressed. Long press and focus work fine.

Alibi answered 3/3, 2010 at 0:17 Comment(0)
S
60

Instead of an OnItemClickListener, add an OnClickListener to each of your views returned from your adapter. You'll need to use setItemsCanFocus setting up your list:

ListView list = (ListView) findViewById(R.id.myList);
list.setAdapter(new DoubleClickAdapter(this));
list.setItemsCanFocus(true);

and then in your Adapter's getView, this will yield a clickable row. The button is assumed to be in the inflated xml.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = View.inflate(context, R.layout.cell, null);
    view.setClickable(true);
    view.setFocusable(true);
    view.setBackgroundResource(android.R.drawable.menuitem_background);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(context).setTitle("touched").show();
        }

    });
    return view;
}
Stortz answered 3/3, 2010 at 7:1 Comment(7)
Don't forget to recycle views! Use the convertView, do not inflate a new one every time.Anele
I had basically done that, though I feel like I had problems doing something. I'll try it out again, maybe it was just a stupid mistake.Alibi
Okay, can anyone explain why the ListView controls aren't working as advertised? I've had this same issue too and this solution works though the response time on touch is not good.Calkins
Old question I know but you can also create the onclicklistner in the "newView" method of your adapter instead. This will save this process being called every time the user scrolls the listThermaesthesia
This is not an ideal solution if you still want your whole row to be clickable, you should use https://mcmap.net/q/331862/-listview-onitemclicklistener-not-respondingLittlest
this does not work if i do it in newView() of an extended CursorAdaptor, instead of in a getView(). What's wrong?Haase
I have been stuck on this for 6 hours now, and I really need help. https://mcmap.net/q/332388/-why-can-39-t-i-remove-an-item/…Breeching
E
53

set your ImageButton's attribute:

android:focusable="false"

Because AbsListView.onTouchEvent check child.hasFocusable().

Eartha answered 23/12, 2010 at 12:37 Comment(5)
Works great for TextViews. Not so much for Buttons.Temple
If you use android:inputType for textview, you should remove it also.Theft
Much better option that the answer with the award. Register a single listener, and deal with it. Less memory to boot.Cholecystotomy
I have been stuck on this for 6 hours now, and I really need help. https://mcmap.net/q/332388/-why-can-39-t-i-remove-an-item/…Breeching
Adding another finding - Dont use "View" widget, use ImageView or as applicableDoubs
F
10

I've tested the following solution on SDK levels 8 and 16.

In getView()

setFocusable(false);
setClickable(false);

rather than setting them true in the Adapter's getView() does what I think the original question wanted, and means that an OnItemClickListener gets called, provided that an OnClickListener is not set in getView().

I'm assuming that anything you can do in an View's OnClickListener you can do just as easily in a ListView's OnItemClickListener. (setOnClickListener on a View implicitly sets the view to be clickable, which prevents the ListView's corresponding OnItemClickListener getting called, apparently.)

The behaviour is as one would expect, in terms of the ImageButton's visual state when the item is pressed or rolled over.

The solution is a slight illusion, in that it is the list item that's being pressed not the ImageButton itself, so if the button doesn't occupy whole list item, clicking somewhere else in the item will still make the button's drawable state reflect the click. Same for focus. That might be a price worth paying.

Fawcette answered 19/9, 2012 at 23:2 Comment(3)
If your ListView row has multiple clickable items, you can probably achieve exactly what you want by setting android:descendantFocusability="blocksDescendants" on the top level list item layout, and not setting an OnClickListener on the view that you return from getView(). Setting OnClickListeners on the individual items will still work.Fawcette
This solution also worked for me. In my case I was not getting clicked event in OnItemClickListener when RadioButton was clicked.Preparative
I have been stuck on this for 6 hours now, and I really need help. https://mcmap.net/q/332388/-why-can-39-t-i-remove-an-item/…Breeching
B
7

This will definitely work. Add this to the layout definition.

android:descendantFocusability="blocksDescendants" 

Found the solution here

Birthmark answered 11/10, 2013 at 19:28 Comment(0)
H
5

One alternative to setting an OnClickListener for every view is to NOT use an ImageButton - use an ImageView instead. The ImageView can still send events to an OnClickListener and won't take over the focus.

Hyposthenia answered 14/9, 2010 at 21:59 Comment(0)
C
5

best way to do is this:

  android:focusable="false"
  android:focusableInTouchMode="false"

set these properties for that Imagebutton and try. I

Circassian answered 7/4, 2013 at 7:4 Comment(0)
C
4

For my version of this problem, the issue was that I had set my TextView object to android:inputType="textMultiLine". When I removed this line the issue of the list not being clickable was gone. Looks like a nasty little bug.

Also, I'm still able to use the android:minLines/android:maxLines properties with no problem, so it's not a big issue. Just not the solution I expected.

Calkins answered 11/6, 2010 at 2:25 Comment(1)
I have been stuck on this for 6 hours now, and I really need help. https://mcmap.net/q/332388/-why-can-39-t-i-remove-an-item/…Breeching
E
1

As an alternative solution which worked for me you can try to extend your adapter from BaseAdapter (iso implementing ListAdapter interface)

Erythrism answered 14/2, 2012 at 18:41 Comment(0)
C
1

The following line solved the issue in my project:

<TextView ... android:textIsSelectable="false" />
Conversational answered 4/12, 2015 at 15:59 Comment(0)
I
0

Put This code ImageView nextpage= (ImageView)findViewById(R.id.btnEdit); instead of ImageButton . now the list item is active

Integument answered 3/2, 2014 at 13:14 Comment(0)
W
0

I have sub-classed ImageButton and setFocusable="false" in layout definition didn't work for me. It solved calling setFocusable(false) in constructor of subclass.

Wateriness answered 15/3, 2014 at 14:59 Comment(0)
E
-1

Using a ScrollView can prevent the onItemClickListener from receiving the input.

Hope this helps anyone.

Episcopalism answered 28/10, 2014 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.