views:

1147

answers:

3

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.

+5  A: 

Instead of an OnItemClickListener, add an OnClick 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;
}
jqpubliq
Don't forget to recycle views! Use the convertView, do not inflate a new one every time.
Romain Guy
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.
GuyNoir
Yup, it worked.
GuyNoir
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.
greg7gkb
A: 

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.

-g

greg7gkb
A: 

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.

humbleCoder