From eb6d26b6a4e264d73e0e23588dfc2a93864ad7f7 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 23 Sep 2019 13:50:51 +0700 Subject: [PATCH] Focus drawer when it opens It is still buggy because of NavigationView (why the hell is NavigationMenuView marked as focusable?) but at least initial opening works as intended --- .../newpipe/views/FocusAwareDrawerLayout.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/views/FocusAwareDrawerLayout.java b/app/src/main/java/org/schabi/newpipe/views/FocusAwareDrawerLayout.java index 0e8097dbe..2354427a3 100644 --- a/app/src/main/java/org/schabi/newpipe/views/FocusAwareDrawerLayout.java +++ b/app/src/main/java/org/schabi/newpipe/views/FocusAwareDrawerLayout.java @@ -17,8 +17,10 @@ */ package org.schabi.newpipe.views; +import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; +import android.view.Gravity; import android.view.View; import androidx.annotation.NonNull; @@ -66,4 +68,35 @@ public final class FocusAwareDrawerLayout extends DrawerLayout { content.addFocusables(views, direction, focusableMode); } } + + // this override isn't strictly necessary, but it is helpful when DrawerLayout isn't the topmost + // view in hierarchy (such as when system or builtin appcompat ActionBar is used) + @Override + @SuppressLint("RtlHardcoded") + public void openDrawer(@NonNull View drawerView, boolean animate) { + super.openDrawer(drawerView, animate); + + LayoutParams params = (LayoutParams) drawerView.getLayoutParams(); + + int gravity = GravityCompat.getAbsoluteGravity(params.gravity, ViewCompat.getLayoutDirection(this)); + + int direction = 0; + + switch (gravity) { + case Gravity.LEFT: + direction = FOCUS_LEFT; + break; + case Gravity.RIGHT: + direction = FOCUS_RIGHT; + break; + case Gravity.TOP: + direction = FOCUS_UP; + break; + case Gravity.BOTTOM: + direction = FOCUS_DOWN; + break; + } + + drawerView.requestFocus(direction); + } }