Android Row becomes Unclickable with Button

前端 未结 4 2019
时光取名叫无心
时光取名叫无心 2020-12-01 03:23

I have a listView, where each row has a button in the row layout. However, this seems to make the row itself unclickable. How can I make both the button and row clickable? <

相关标签:
4条回答
  • 2020-12-01 03:50

    Whenever I see posts concerning the android:focusable and android:clickable attributes, I always see them being both set to the same value at once. I figured there must be a reason if they are two separate attributes instead of being one.

    It turns out that a much better way of achieving your desired behavior is to set

    android:focusable="false" 
    

    or

    yourButton.setFocusable(false)
    

    on the Button in your View. Once you do that, you'll be both able to set an OnClickListener on the Button, and a click on the row will fire the onListItemClick() method in your OnItemClickListener.

    0 讨论(0)
  • 2020-12-01 03:56

    You need to set itemsCanFocus on the list like this:

        mList.setItemsCanFocus(true);
    

    To make the button clickable. Then you will need to use your own adapter and in getView return a view which is Clickable and focusable. You will also lose the default highlight states so you need to put them back in with the background resource. So do this:

        view.setClickable(true);
        view.setFocusable(true);
        view.setBackgroundResource(android.R.drawable.menuitem_background);
    

    to your view before returning your view.

    0 讨论(0)
  • 2020-12-01 04:00

    Unfortunately I don't think that is possible. You ListView row can either have focusable widgets, like a button, or be clickable, not both. See link.

    0 讨论(0)
  • 2020-12-01 04:01

    Try to set your widgets to non clickable and non focusable in xml,the click on items will work normally and also the click on button will work normally.

    android:clickable="false" 
    android:focusable="false"
    

    Hope this helps.

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