0

I'm trying to play around with some Kotlin and Anko (more familiar with iOS) and taking from their example, there is this code:

internal open class TextListWithCheckboxItem(val text: String = "") : ListItem {
protected inline fun createTextView(ui: AnkoContext<ListItemAdapter>, init: TextView.() -> Unit) = ui.apply {
    textView {
        id = android.R.id.text1
        text = "Text list item" // default text (for the preview)
        isClickable = true
        setOnClickListener {
              Log.d("test", "message")
        }
        init()
    }

    checkBox {
        id = View.generateViewId()
        setOnClickListener {
            Log.d("hi", "bye")
        }
        init()
    }
}.view

My row appears how I want with a checkbox and textview. But I want to bind an action to the row selection not the checkbox selection. Putting a log message in both, I see that I get a log message when the row is selected which flips the checkbox. It does not, however, log my "test:message" from the textView click handler. Is there a way to get around this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Crystal
  • 28,460
  • 62
  • 219
  • 393

1 Answers1

0

Apparently your issue has been addressed here. As the checkbox is consuming all the focus of ListItem you should set the CheckBox's focusable flag to false:

checkBox {
    focusable = View.NOT_FOCUSABLE
}

Unfortunately setFocusable call requires at least API 26, but you could define view .xml and inflate the view manually as described here:

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

Alternatively you could try setting a onTouchListener returning false which means the touch event will be passed to underlying views.

Let me know if it works ;)

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33