KingJA / SwitchButton

A smart switchable button,support multiple tabs. (多项选择器,切换按钮)
383 stars 76 forks source link

Does not support Android TV focus #9

Open tmm1 opened 6 years ago

tmm1 commented 6 years ago

The SwitchButton renders to screen as expected, but it is not possible to use DPAD remote to navigate or select the buttons.

KingJA commented 6 years ago

@tmm1 Could you show me the codes about DPAD?

tmm1 commented 6 years ago

Sure https://developer.android.com/training/tv/start/navigation.html

tmm1 commented 6 years ago

For SwitchButton I think you need onKeyDown https://developer.android.com/training/game-controllers/controller-input.html#dpad

And also use setOnFocusChangeListener to change the style on focus, so user can tell it is active

tmm1 commented 6 years ago

The code for handling DPAD is not too complicated. I used kotlin subclass, but it should be easy to add the same thing to the java base class.

class MySwitchMultiButton(context: Context, attributeSet: AttributeSet): SwitchMultiButton(context, attributeSet) {
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KEYCODE_DPAD_LEFT -> {
                if (selectedTab > 0) {
                    selectedTab -= 1
                    return true
                }
            }
            KEYCODE_DPAD_RIGHT -> {
                val field = SwitchMultiButton::class.java.declaredFields.find { it.name == "mTabNum" }!!
                field.isAccessible = true
                if (selectedTab < field.getInt(this)-1) {
                    selectedTab += 1
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}

I think onDraw should also check isFocused() and change the style/color so the user knows the switch button is focused on the screen.

KingJA commented 6 years ago

@tmm1 Hey gay, I don't know how to test TV Application by remote. I have created a branch (v1.1.6_tv) base on your codes. It will be thankful if you could test it.

tmm1 commented 6 years ago

FYI, In Android Studio you should be able to make TV emulator and use on-screen remote dpad.