Skip to content

Commit

Permalink
[NavDrawer][a11y] Fix wrong item selected after click
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 616214272
(cherry picked from commit 5bf6831)
  • Loading branch information
paulfthomas authored and hunterstich committed Mar 21, 2024
1 parent d61cffd commit a3af20a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void handleOnBackPressed() {
};

private DrawerLayout drawerLayout;
private MaterialSwitch autoCloseSwitch;

@NonNull
@Override
Expand Down Expand Up @@ -91,13 +92,13 @@ public void onDrawerClosed(@NonNull View drawerView) {
view.findViewById(R.id.show_end_drawer_gravity)
.setOnClickListener(v -> drawerLayout.openDrawer(navigationViewEnd));

MaterialSwitch materialSwitch = view.findViewById(R.id.bold_text_switch);
materialSwitch.setChecked(true);
materialSwitch.setOnCheckedChangeListener(
MaterialSwitch boldTextSwitch = view.findViewById(R.id.bold_text_switch);
boldTextSwitch.setOnCheckedChangeListener(
(buttonView, isChecked) -> {
navigationViewStart.setItemTextAppearanceActiveBoldEnabled(isChecked);
navigationViewEnd.setItemTextAppearanceActiveBoldEnabled(isChecked);
});
autoCloseSwitch = view.findViewById(R.id.auto_close_switch);

drawerLayout.post(
() -> {
Expand All @@ -115,7 +116,9 @@ private void initNavigationView(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
menuItem -> {
navigationView.setCheckedItem(menuItem);
drawerLayout.closeDrawer(navigationView);
if (autoCloseSwitch.isChecked()) {
drawerLayout.closeDrawer(navigationView);
}
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@
android:id="@+id/bold_text_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginStart="12dp" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:labelFor="@+id/auto_close_switch"
android:text="@string/cat_navigationdrawer_label_auto_close" />
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/auto_close_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginStart="12dp" />
</LinearLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Open End Drawer
</string>
<string name="cat_navigationdrawer_label_active_bold" translatable="false">Bold active label</string>
<string name="cat_navigationdrawer_label_auto_close" translatable="false">Close drawer on selection</string>
<string name="cat_navigationdrawer_header_mail">Mail</string>
<string name="cat_navigationdrawer_header_labels">Labels</string>
<string name="cat_navigationdrawer_label">Label</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.CheckedTextView;
import android.widget.FrameLayout;
Expand Down Expand Up @@ -173,6 +174,10 @@ private void setActionView(@Nullable View actionView) {
(FrameLayout)
((ViewStub) findViewById(R.id.design_menu_item_action_area_stub)).inflate();
}
// Make sure to remove the existing parent if the View is reused
if (actionView.getParent() != null) {
((ViewGroup) actionView.getParent()).removeView(actionView);
}
actionArea.removeAllViews();
actionArea.addView(actionView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class NavigationMenuPresenter implements MenuPresenter {

/** Padding for separators between items */
int paddingSeparator;

private int overScrollMode = -1;

@Override
Expand All @@ -125,6 +126,9 @@ public MenuView getMenuView(ViewGroup root) {
new NavigationMenuViewAccessibilityDelegate(menuView));
if (adapter == null) {
adapter = new NavigationMenuAdapter();
// Prevent recreating all the Views when notifyDataSetChanged() is called causing issues
// with the a11y reader (see b/112931425)
adapter.setHasStableIds(true);
}
if (overScrollMode != -1) {
menuView.setOverScrollMode(overScrollMode);
Expand Down Expand Up @@ -375,7 +379,7 @@ public int getSubheaderInsetEnd() {
return this.subheaderInsetEnd;
}

public void setSubheaderInsetEnd(@Px int subheaderInsetEnd) {
public void setSubheaderInsetEnd(@Px int subheaderInsetEnd) {
this.subheaderInsetEnd = subheaderInsetEnd;
updateMenuView(false);
}
Expand Down

0 comments on commit a3af20a

Please sign in to comment.