Skip to content

Commit 1fe726f

Browse files
paulfthomaspekingme
authored andcommitted
[ChipGroup][a11y] Follow up
PiperOrigin-RevId: 758334270
1 parent 43f2b90 commit 1fe726f

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

catalog/java/io/material/catalog/chip/ChipGroupDemoFragment.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public View onCreateDemoView(
7474
fab.setOnClickListener(
7575
v -> {
7676
// Reload the chip group UI.
77+
reflowGroup.setSingleLine(true);
7778
initChipGroup(reflowGroup, false);
7879
initChipGroup(scrollGroup, true);
7980
});
@@ -92,42 +93,54 @@ protected int getChipGroupItem(boolean singleSelection) {
9293
: R.layout.cat_chip_group_item_filter;
9394
}
9495

95-
private void initChipGroup(ChipGroup chipGroup, boolean shouldShowAll) {
96+
private void initChipGroup(ChipGroup chipGroup, boolean showMenu) {
9697
chipGroup.removeAllViews();
9798

99+
Chip viewAllChip = new Chip(chipGroup.getContext());
100+
viewAllChip.setText(viewAllChip.getResources().getString(R.string.cat_chip_text_all));
101+
viewAllChip.setChipIconResource(R.drawable.ic_drawer_menu_open_24px);
102+
viewAllChip.setChipIconTint(viewAllChip.getTextColors());
103+
viewAllChip.setChipIconVisible(true);
104+
chipGroup.addView(viewAllChip);
105+
98106
PopupMenu menu;
99-
if (shouldShowAll) {
100-
Chip viewAllChip = new Chip(chipGroup.getContext());
101-
viewAllChip.setText(viewAllChip.getResources().getString(R.string.cat_chip_text_all));
102-
viewAllChip.setChipIconResource(R.drawable.ic_drawer_menu_open_24px);
103-
viewAllChip.setChipIconTint(viewAllChip.getTextColors());
104-
viewAllChip.setChipIconVisible(true);
107+
if (showMenu) {
105108
menu = new PopupMenu(viewAllChip.getContext(), viewAllChip);
106109
viewAllChip.setOnClickListener(v -> menu.show());
107-
chipGroup.addView(viewAllChip);
108110
} else {
109111
menu = null;
112+
viewAllChip.setOnClickListener(
113+
v -> {
114+
if (chipGroup.isSingleLine()) {
115+
chipGroup.setSingleLine(false);
116+
initChipGroup(chipGroup, false);
117+
}
118+
});
110119
}
111120

112121
boolean singleSelection = singleSelectionSwitch.isChecked();
113122
String[] textArray = getResources().getStringArray(R.array.cat_chip_group_text_array);
114123
for (int i = 0; i < textArray.length; i++) {
124+
Chip chip =
125+
(Chip) getLayoutInflater().inflate(getChipGroupItem(singleSelection), chipGroup, false);
126+
chip.setId(i);
127+
chip.setText(textArray[i]);
128+
chip.setCloseIconVisible(false);
129+
chipGroup.addView(chip);
130+
115131
if (menu != null) {
116132
menu.getMenu().add(Menu.NONE, i, i, textArray[i]);
133+
menu.getMenu().setGroupCheckable(Menu.NONE, true, singleSelection);
117134
menu.setOnMenuItemClickListener(
118135
menuItem -> {
119-
chipGroup.check(menuItem.getItemId());
136+
Chip targetChip = (Chip) chipGroup.getChildAt(menuItem.getOrder() + 1);
137+
targetChip.setChecked(!targetChip.isChecked());
138+
menuItem.setChecked(targetChip.isChecked());
120139
return true;
121140
});
141+
chip.setOnCheckedChangeListener(
142+
(buttonView, isChecked) -> menu.getMenu().getItem(chip.getId()).setChecked(isChecked));
122143
}
123-
124-
Chip chip =
125-
(Chip) getLayoutInflater().inflate(getChipGroupItem(singleSelection), chipGroup, false);
126-
chip.setId(i);
127-
chip.setText(textArray[i]);
128-
chip.setCloseIconVisible(true);
129-
chip.setOnCloseIconClickListener(v -> chipGroup.removeView(chip));
130-
chipGroup.addView(chip);
131144
}
132145
}
133146
}

catalog/java/io/material/catalog/chip/ChipRecyclerviewDemoFragment.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ public void onBindViewHolder(MyViewHolder holder, int position) {
111111
for (int i = 1; i < getItemCount(); i++) {
112112
menu.getMenu().add(Menu.NONE, i, i, text + " " + i);
113113
}
114+
menu.getMenu().setGroupCheckable(Menu.NONE, true, false);
114115
menu.setOnMenuItemClickListener(
115116
menuItem -> {
116117
int id = menuItem.getItemId();
117118
if (!checkedChipId.remove(id)) {
118119
checkedChipId.add(id);
120+
menuItem.setChecked(true);
121+
} else {
122+
menuItem.setChecked(false);
119123
}
120124
notifyItemChanged(id);
121125
return true;
@@ -125,6 +129,9 @@ public void onBindViewHolder(MyViewHolder holder, int position) {
125129
} else {
126130
chip.setChecked(checkedChipId.contains(position));
127131
chip.setText(text + " " + position);
132+
chip.setOnCheckedChangeListener(
133+
(buttonView, isChecked) ->
134+
menu.getMenu().getItem((Integer) chip.getTag() - 1).setChecked(isChecked));
128135
}
129136
}
130137

catalog/java/io/material/catalog/chip/res/layout/cat_chip_group_fragment.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
app:srcCompat="@drawable/ic_refresh_black_24px"
4444
app:fabSize="mini"/>
4545

46-
<FrameLayout
47-
android:layout_width="match_parent"
48-
android:layout_height="wrap_content">
49-
5046
<ScrollView
5147
android:layout_width="match_parent"
5248
android:layout_height="wrap_content"
53-
android:layout_gravity="center_vertical">
49+
android:layout_gravity="center_vertical"
50+
android:layout_marginTop="32dp">
51+
5452
<LinearLayout
5553
android:layout_width="match_parent"
5654
android:layout_height="wrap_content"
@@ -68,7 +66,8 @@
6866
android:id="@+id/reflow_group"
6967
android:layout_width="match_parent"
7068
android:layout_height="wrap_content"
71-
android:layout_margin="16dp"/>
69+
android:layout_margin="16dp"
70+
app:singleLine="true"/>
7271

7372
<!-- Scrolling chip group. -->
7473
<TextView
@@ -81,11 +80,10 @@
8180
android:layout_margin="16dp">
8281
<com.google.android.material.chip.ChipGroup
8382
android:id="@+id/scroll_group"
84-
android:layout_width="match_parent"
83+
android:layout_width="wrap_content"
8584
android:layout_height="wrap_content"
8685
app:singleLine="true"/>
8786
</HorizontalScrollView>
8887
</LinearLayout>
8988
</ScrollView>
90-
</FrameLayout>
9189
</LinearLayout>

catalog/java/io/material/catalog/chip/res/layout/cat_chip_group_item_filter.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
<com.google.android.material.chip.Chip
1919
xmlns:android="http://schemas.android.com/apk/res/android"
20-
xmlns:app="http://schemas.android.com/apk/res-auto"
2120
style="@style/Widget.Material3.Chip.Filter"
2221
android:layout_width="wrap_content"
2322
android:layout_height="wrap_content"/>

0 commit comments

Comments
 (0)