Android : Multiple Actions on a List View - Focus Issue

前端 未结 6 974
醉酒成梦
醉酒成梦 2021-02-06 02:55

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 informati

相关标签:
6条回答
  • 2021-02-06 03:12

    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.

    0 讨论(0)
  • 2021-02-06 03:24

    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?

    0 讨论(0)
  • 2021-02-06 03:24

    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.

    0 讨论(0)
  • 2021-02-06 03:30

    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

    0 讨论(0)
  • 2021-02-06 03:30

    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.

    0 讨论(0)
  • 2021-02-06 03:31

    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?

    0 讨论(0)
提交回复
热议问题