Skip to content

Commit 8e33421

Browse files
paulfthomasleticiarossi
authored andcommitted
[ChipGroup][a11y] Add a show all Chip for a11y
Since scrolling is not ideal for our a11y users, a Chip showing a full list has been added to the ChipGroup Catalog demos, allowing for selection without scroll. PiperOrigin-RevId: 750328856
1 parent 781d344 commit 8e33421

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import io.material.catalog.R;
2020

2121
import android.os.Bundle;
22+
import androidx.appcompat.widget.PopupMenu;
2223
import android.view.LayoutInflater;
24+
import android.view.Menu;
2325
import android.view.View;
2426
import android.view.ViewGroup;
2527
import androidx.annotation.LayoutRes;
@@ -34,7 +36,6 @@
3436
public class ChipGroupDemoFragment extends DemoFragment {
3537

3638
private MaterialSwitch singleSelectionSwitch;
37-
private MaterialSwitch selectionRequiredSwitch;
3839

3940
@Nullable
4041
@Override
@@ -43,7 +44,7 @@ public View onCreateDemoView(
4344
View view = layoutInflater.inflate(getChipGroupContent(), viewGroup, false /* attachToRoot */);
4445

4546
singleSelectionSwitch = view.findViewById(R.id.single_selection);
46-
selectionRequiredSwitch = view.findViewById(R.id.selection_required);
47+
MaterialSwitch selectionRequiredSwitch = view.findViewById(R.id.selection_required);
4748

4849
ChipGroup reflowGroup = view.findViewById(R.id.reflow_group);
4950
ChipGroup scrollGroup = view.findViewById(R.id.scroll_group);
@@ -53,28 +54,28 @@ public View onCreateDemoView(
5354
reflowGroup.setSingleSelection(isChecked);
5455
scrollGroup.setSingleSelection(isChecked);
5556

56-
initChipGroup(reflowGroup);
57-
initChipGroup(scrollGroup);
57+
initChipGroup(reflowGroup, false);
58+
initChipGroup(scrollGroup, true);
5859
});
5960

6061
selectionRequiredSwitch.setOnCheckedChangeListener(
6162
(buttonView, isChecked) -> {
6263
reflowGroup.setSelectionRequired(isChecked);
6364
scrollGroup.setSelectionRequired(isChecked);
6465

65-
initChipGroup(reflowGroup);
66-
initChipGroup(scrollGroup);
66+
initChipGroup(reflowGroup, false);
67+
initChipGroup(scrollGroup, true);
6768
});
6869

69-
initChipGroup(reflowGroup);
70-
initChipGroup(scrollGroup);
70+
initChipGroup(reflowGroup, false);
71+
initChipGroup(scrollGroup, true);
7172

7273
FloatingActionButton fab = view.findViewById(R.id.cat_chip_group_refresh);
7374
fab.setOnClickListener(
7475
v -> {
7576
// Reload the chip group UI.
76-
initChipGroup(reflowGroup);
77-
initChipGroup(scrollGroup);
77+
initChipGroup(reflowGroup, false);
78+
initChipGroup(scrollGroup, true);
7879
});
7980
return view;
8081
}
@@ -91,15 +92,39 @@ protected int getChipGroupItem(boolean singleSelection) {
9192
: R.layout.cat_chip_group_item_filter;
9293
}
9394

94-
private void initChipGroup(ChipGroup chipGroup) {
95+
private void initChipGroup(ChipGroup chipGroup, boolean shouldShowAll) {
9596
chipGroup.removeAllViews();
9697

98+
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);
105+
menu = new PopupMenu(viewAllChip.getContext(), viewAllChip);
106+
viewAllChip.setOnClickListener(v -> menu.show());
107+
chipGroup.addView(viewAllChip);
108+
} else {
109+
menu = null;
110+
}
111+
97112
boolean singleSelection = singleSelectionSwitch.isChecked();
98113
String[] textArray = getResources().getStringArray(R.array.cat_chip_group_text_array);
99-
for (String text : textArray) {
114+
for (int i = 0; i < textArray.length; i++) {
115+
if (menu != null) {
116+
menu.getMenu().add(Menu.NONE, i, i, textArray[i]);
117+
menu.setOnMenuItemClickListener(
118+
menuItem -> {
119+
chipGroup.check(menuItem.getItemId());
120+
return true;
121+
});
122+
}
123+
100124
Chip chip =
101125
(Chip) getLayoutInflater().inflate(getChipGroupItem(singleSelection), chipGroup, false);
102-
chip.setText(text);
126+
chip.setId(i);
127+
chip.setText(textArray[i]);
103128
chip.setCloseIconVisible(true);
104129
chip.setOnCloseIconClickListener(v -> chipGroup.removeView(chip));
105130
chipGroup.addView(chip);

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import android.os.Bundle;
2222
import androidx.recyclerview.widget.LinearLayoutManager;
23+
import androidx.appcompat.widget.PopupMenu;
2324
import androidx.recyclerview.widget.RecyclerView;
2425
import android.view.LayoutInflater;
26+
import android.view.Menu;
2527
import android.view.View;
2628
import android.view.ViewGroup;
2729
import androidx.annotation.LayoutRes;
@@ -42,8 +44,7 @@ public class ChipRecyclerviewDemoFragment extends DemoFragment {
4244
@Override
4345
public View onCreateDemoView(
4446
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
45-
View view = layoutInflater.inflate(getChipContent(), viewGroup, false /* attachToRoot
46-
*/);
47+
View view = layoutInflater.inflate(getChipContent(), viewGroup, false /* attachToRoot */);
4748

4849
recyclerView = view.findViewById(R.id.chip_recyclerview_parent);
4950
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
@@ -58,6 +59,10 @@ public View onCreateDemoView(
5859
/** A RecyclerView adapter that displays the checkable chips. */
5960
public static class ChipAdapter extends RecyclerView.Adapter<ChipAdapter.MyViewHolder> {
6061
private final Set<Integer> checkedChipId = new HashSet<>(getItemCount());
62+
private PopupMenu menu;
63+
64+
private static final int VIEW_TYPE_NORMAL = 0;
65+
private static final int VIEW_TYPE_ALL = 1;
6166

6267
/** Provide a reference to the views for each data item. */
6368
public static class MyViewHolder extends RecyclerView.ViewHolder {
@@ -91,15 +96,51 @@ public ChipAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewTyp
9196

9297
@Override
9398
public void onBindViewHolder(MyViewHolder holder, int position) {
94-
holder.chip.setTag(position);
95-
holder.chip.setChecked(checkedChipId.contains(position));
96-
holder.chip.setText(
97-
holder.chip.getResources().getString(R.string.cat_chip_text) + " " + position);
99+
Chip chip = holder.chip;
100+
String text = chip.getResources().getString(R.string.cat_chip_text);
101+
chip.setTag(position);
102+
if (getItemViewType(position) == VIEW_TYPE_ALL) {
103+
chip.setCheckable(false);
104+
chip.setText(chip.getResources().getString(R.string.cat_chip_text_all));
105+
chip.setChipIconResource(R.drawable.ic_drawer_menu_open_24px);
106+
chip.setChipIconTint(chip.getTextColors());
107+
chip.setChipIconVisible(true);
108+
109+
if (menu == null) {
110+
menu = new PopupMenu(chip.getContext(), chip);
111+
for (int i = 1; i < getItemCount(); i++) {
112+
menu.getMenu().add(Menu.NONE, i, i, text + " " + i);
113+
}
114+
menu.setOnMenuItemClickListener(
115+
menuItem -> {
116+
int id = menuItem.getItemId();
117+
if (!checkedChipId.remove(id)) {
118+
checkedChipId.add(id);
119+
}
120+
notifyItemChanged(id);
121+
return true;
122+
});
123+
}
124+
chip.setOnClickListener(v -> menu.show());
125+
} else {
126+
chip.setChecked(checkedChipId.contains(position));
127+
chip.setText(text + " " + position);
128+
}
98129
}
99130

100131
@Override
101132
public int getItemCount() {
102-
return 30;
133+
return 31;
134+
}
135+
136+
@Override
137+
public long getItemId(int position) {
138+
return position;
139+
}
140+
141+
@Override
142+
public int getItemViewType(int position) {
143+
return position == 0 ? VIEW_TYPE_ALL : VIEW_TYPE_NORMAL;
103144
}
104145
}
105146

catalog/java/io/material/catalog/chip/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
action.
2525
</string>
2626

27+
<string name="cat_chip_text_all">Show All</string>
2728
<string name="cat_chip_text">Hello, World!</string>
2829
<string name="cat_chip_text_to_truncate">Hello, very very very very very very very very big World!</string>
2930
<string name="cat_chip_long_text_switch_label">Long text</string>

0 commit comments

Comments
 (0)