Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Multiple Actions on a List View - Focus Issue

I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).

If you can look @ DroidRecord, they have a similar layout as I am looking to achive.

Thanks!

Chris.

like image 858
Chrispix Avatar asked Feb 01 '09 05:02

Chrispix


3 Answers

As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:

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

See also How to fire onListItemClick in Listactivity with buttons in list?

like image 161
BoD Avatar answered Nov 18 '22 00:11

BoD


Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).

ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.

like image 30
Hatchmaster Avatar answered Nov 18 '22 00:11

Hatchmaster


what is your question? How to add a button to a list row?

Very simple, just as you expect it will be added to the row's layout.

Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me

Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?

Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.

Cheers

like image 4
Mariano Kamp Avatar answered Nov 18 '22 01:11

Mariano Kamp