Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView OnItemClickListener Not Responding?

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.

like image 627
Kleptine Avatar asked Mar 03 '10 00:03

Kleptine


6 Answers

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;
}
like image 189
jqpubliq Avatar answered Oct 24 '22 08:10

jqpubliq


set your ImageButton's attribute:

android:focusable="false"

Because AbsListView.onTouchEvent check child.hasFocusable().

like image 45
meizilp Avatar answered Oct 24 '22 07:10

meizilp


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.

like image 10
JulianSymes Avatar answered Oct 24 '22 08:10

JulianSymes


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

android:descendantFocusability="blocksDescendants" 

Found the solution here

like image 7
Kapil Raju Avatar answered Oct 24 '22 08:10

Kapil Raju


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.

like image 5
humbleCoder Avatar answered Oct 24 '22 08:10

humbleCoder


best way to do is this:

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

set these properties for that Imagebutton and try. I

like image 5
silentkratos Avatar answered Oct 24 '22 08:10

silentkratos