mardi 7 juillet 2020

KeyEvent of D-pad for Android TV

In activity I have TabLayout, ViewPager and menu button (hamburger) in toolbar.

With arrows on my keyboard I am moving from tabs to view pager items without problems by default but I cannot achieve focus on toolbar menu button.

First of all I can't listen when KeyEvent.KEYCODE_DPAD_UP was pressed and I'm wondering why.

My code:

viewPager = findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
// Передаём ViewPager в TabLayout
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

tabLayout.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if ((keyEvent.getAction() == KeyEvent.ACTION_UP) && (i == KeyEvent.KEYCODE_DPAD_UP)) {
            toolbar.setFocusable(true);
            toolbar.setFocusableInTouchMode(true);
            tabLayout.setNextFocusUpId(R.id.toolbar);
            Log.d(TAG, "KEYCODE_DPAD_UP");
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            tabLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        }
        return false;
    }
});

What is wrong with my if-statement?

Aucun commentaire:

Enregistrer un commentaire