TextView and Button in each row and onListItemClick()

后端 未结 2 1512
故里飘歌
故里飘歌 2020-12-03 11:55

I have a ListView with some elements on it. Each row has a TextView and a Button. It looks like this:

| Some text in a row (Button) |

Now, w

相关标签:
2条回答
  • 2020-12-03 12:18

    The challange is that the ListView and the button fight for focus. Typically, only one of the two can receive focus (and thus be clicked). In your cause, the button is the focusable one.

    To adjust this, you can play with the descendantFocusability property of the ListView.

    What are you trying to accomplish by having a button within a ListView item? Do you want something different to happen when you click on the button, vs when you click on the listview item outside the button?

    0 讨论(0)
  • 2020-12-03 12:23

    add the property focusable="false" to your TextView ::

    <TextView
    ...
    ...
            android:focusable="false"
            />
    

    and probably you will need to do the same to the other elements inside your ListView.


    Programatically we can use the method setFocusable():

    v.findViewById(R.id.my_button).setFocusable(false); 
    

    setFocusable(): Setting this to false will also ensure that this view is not focusable in touch mode.

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