Android : Multiple Actions on a List View - Focus

2020-05-27 04:57发布

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.

6条回答
我命由我不由天
2楼-- · 2020-05-27 05:37

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?

查看更多
Emotional °昔
3楼-- · 2020-05-27 05:39

I tried this to be able to click on the buttons but it didn't work for me android:focusable="false" android:focusableInTouchMode="false"

so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it. after that you can add buttons to the layout and each button will be clickable.

查看更多
孤傲高冷的网名
4楼-- · 2020-05-27 05:43

I would like to thank BoD for its hint on removing the focusable state to the button(s), it saved my day.

But for information, since the button is no more focusable - state_focused on < selector > xml - , its design won't display anymore to the user.

Those buttons will still get the state pressed though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !

Keep that in mind, it could not be a good solution for your own case, but it does work well.

查看更多
萌系小妹纸
5楼-- · 2020-05-27 05:45

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

查看更多
趁早两清
6楼-- · 2020-05-27 05:56

It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?

查看更多
甜甜的少女心
7楼-- · 2020-05-27 05:58

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.

查看更多
登录 后发表回答