Skip to content

Commit 20f92df

Browse files
imhappikendrickumstattd
authored andcommitted
[NavigationDrawer] Notify each adapter item of a potential change in case there are any item changes
Resolves #4168 PiperOrigin-RevId: 646154192
1 parent 5371290 commit 20f92df

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

lib/java/com/google/android/material/internal/NavigationMenuItemView.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public NavigationMenuItemView(
102102
LayoutInflater.from(context).inflate(R.layout.design_navigation_menu_item, this, true);
103103
setIconSize(context.getResources().getDimensionPixelSize(R.dimen.design_navigation_icon_size));
104104
textView = findViewById(R.id.design_menu_item_text);
105-
textView.setDuplicateParentStateEnabled(true);
106105
ViewCompat.setAccessibilityDelegate(textView, accessibilityDelegate);
107106
}
108107

lib/java/com/google/android/material/internal/NavigationMenuPresenter.java

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ public MenuView getMenuView(ViewGroup root) {
142142
return menuView;
143143
}
144144

145+
private void updateAllTextMenuItems() {
146+
if (adapter != null) {
147+
adapter.updateAllTextMenuItems();
148+
}
149+
}
150+
151+
private void updateAllSubHeaderMenuItems() {
152+
if (adapter != null) {
153+
adapter.updateAllSubHeaderMenuItems();
154+
}
155+
}
156+
157+
private void updateAllDividerMenuItems() {
158+
if (adapter != null) {
159+
adapter.updateAllDividerMenuItems();
160+
}
161+
}
162+
145163
@Override
146164
public void updateMenuView(boolean cleared) {
147165
if (adapter != null) {
@@ -271,12 +289,12 @@ public View getHeaderView(int index) {
271289

272290
public void setSubheaderColor(@Nullable ColorStateList subheaderColor) {
273291
this.subheaderColor = subheaderColor;
274-
updateMenuView(false);
292+
updateAllSubHeaderMenuItems();
275293
}
276294

277295
public void setSubheaderTextAppearance(@StyleRes int resId) {
278296
subheaderTextAppearance = resId;
279-
updateMenuView(false);
297+
updateAllSubHeaderMenuItems();
280298
}
281299

282300
@Nullable
@@ -286,7 +304,7 @@ public ColorStateList getItemTintList() {
286304

287305
public void setItemIconTintList(@Nullable ColorStateList tint) {
288306
iconTintList = tint;
289-
updateMenuView(false);
307+
updateAllTextMenuItems();
290308
}
291309

292310
@Nullable
@@ -296,17 +314,17 @@ public ColorStateList getItemTextColor() {
296314

297315
public void setItemTextColor(@Nullable ColorStateList textColor) {
298316
this.textColor = textColor;
299-
updateMenuView(false);
317+
updateAllTextMenuItems();
300318
}
301319

302320
public void setItemTextAppearance(@StyleRes int resId) {
303321
textAppearance = resId;
304-
updateMenuView(false);
322+
updateAllTextMenuItems();
305323
}
306324

307325
public void setItemTextAppearanceActiveBoldEnabled(boolean isBold) {
308326
textAppearanceActiveBoldEnabled = isBold;
309-
updateMenuView(false);
327+
updateAllTextMenuItems();
310328
}
311329

312330
@Nullable
@@ -316,12 +334,12 @@ public Drawable getItemBackground() {
316334

317335
public void setItemBackground(@Nullable Drawable itemBackground) {
318336
this.itemBackground = itemBackground;
319-
updateMenuView(false);
337+
updateAllTextMenuItems();
320338
}
321339

322340
public void setItemForeground(@Nullable RippleDrawable itemForeground) {
323341
this.itemForeground = itemForeground;
324-
updateMenuView(false);
342+
updateAllTextMenuItems();
325343
}
326344

327345
public int getItemHorizontalPadding() {
@@ -330,7 +348,7 @@ public int getItemHorizontalPadding() {
330348

331349
public void setItemHorizontalPadding(int itemHorizontalPadding) {
332350
this.itemHorizontalPadding = itemHorizontalPadding;
333-
updateMenuView(false);
351+
updateAllTextMenuItems();
334352
}
335353

336354
@Px
@@ -340,7 +358,7 @@ public int getItemVerticalPadding() {
340358

341359
public void setItemVerticalPadding(@Px int itemVerticalPadding) {
342360
this.itemVerticalPadding = itemVerticalPadding;
343-
updateMenuView(false);
361+
updateAllTextMenuItems();
344362
}
345363

346364
@Px
@@ -350,7 +368,7 @@ public int getDividerInsetStart() {
350368

351369
public void setDividerInsetStart(@Px int dividerInsetStart) {
352370
this.dividerInsetStart = dividerInsetStart;
353-
updateMenuView(false);
371+
updateAllDividerMenuItems();
354372
}
355373

356374
@Px
@@ -360,7 +378,7 @@ public int getDividerInsetEnd() {
360378

361379
public void setDividerInsetEnd(@Px int dividerInsetEnd) {
362380
this.dividerInsetEnd = dividerInsetEnd;
363-
updateMenuView(false);
381+
updateAllDividerMenuItems();
364382
}
365383

366384
@Px
@@ -370,7 +388,7 @@ public int getSubheaderInsetStart() {
370388

371389
public void setSubheaderInsetStart(@Px int subheaderInsetStart) {
372390
this.subheaderInsetStart = subheaderInsetStart;
373-
updateMenuView(false);
391+
updateAllSubHeaderMenuItems();
374392
}
375393

376394
@Px
@@ -380,7 +398,7 @@ public int getSubheaderInsetEnd() {
380398

381399
public void setSubheaderInsetEnd(@Px int subheaderInsetEnd) {
382400
this.subheaderInsetEnd = subheaderInsetEnd;
383-
updateMenuView(false);
401+
updateAllSubHeaderMenuItems();
384402
}
385403

386404
public int getItemIconPadding() {
@@ -389,12 +407,12 @@ public int getItemIconPadding() {
389407

390408
public void setItemIconPadding(int itemIconPadding) {
391409
this.itemIconPadding = itemIconPadding;
392-
updateMenuView(false);
410+
updateAllTextMenuItems();
393411
}
394412

395413
public void setItemMaxLines(int itemMaxLines) {
396414
this.itemMaxLines = itemMaxLines;
397-
updateMenuView(false);
415+
updateAllTextMenuItems();
398416
}
399417

400418
public int getItemMaxLines() {
@@ -405,7 +423,7 @@ public void setItemIconSize(@Dimension int itemIconSize) {
405423
if (this.itemIconSize != itemIconSize) {
406424
this.itemIconSize = itemIconSize;
407425
hasCustomItemIconSize = true;
408-
updateMenuView(false);
426+
updateAllTextMenuItems();
409427
}
410428
}
411429

@@ -684,8 +702,13 @@ public void onViewRecycled(ViewHolder holder) {
684702
}
685703

686704
public void update() {
705+
int prevItemSize = items.size();
687706
prepareMenuItems();
688707
notifyDataSetChanged();
708+
// If there were no structural changes, update the items due to the adapter having stable ids.
709+
if (prevItemSize == items.size()) {
710+
notifyItemRangeChanged(0, items.size());
711+
}
689712
}
690713

691714
/**
@@ -865,6 +888,32 @@ int getRowCount() {
865888
}
866889
return itemCount;
867890
}
891+
892+
private void updateAllTextMenuItems() {
893+
for (int i = 0; i < items.size(); i++) {
894+
if (items.get(i) instanceof NavigationMenuTextItem
895+
&& getItemViewType(i) == VIEW_TYPE_NORMAL) {
896+
notifyItemChanged(i);
897+
}
898+
}
899+
}
900+
901+
private void updateAllSubHeaderMenuItems() {
902+
for (int i = 0; i < items.size(); i++) {
903+
if (items.get(i) instanceof NavigationMenuTextItem
904+
&& getItemViewType(i) == VIEW_TYPE_SUBHEADER) {
905+
notifyItemChanged(i);
906+
}
907+
}
908+
}
909+
910+
private void updateAllDividerMenuItems() {
911+
for (int i = 0; i < items.size(); i++) {
912+
if (items.get(i) instanceof NavigationMenuSeparatorItem) {
913+
notifyItemChanged(i);
914+
}
915+
}
916+
}
868917
}
869918

870919
/** Unified data model for all sorts of navigation menu items. */

lib/java/com/google/android/material/internal/res/layout/design_navigation_menu_item.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
android:drawablePadding="@dimen/design_navigation_icon_padding"
2525
android:gravity="center_vertical|start"
2626
android:maxLines="1"
27+
android:duplicateParentState="true"
2728
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>
2829

2930
<ViewStub

0 commit comments

Comments
 (0)