19
19
import io .material .catalog .R ;
20
20
21
21
import android .os .Bundle ;
22
+ import androidx .appcompat .widget .PopupMenu ;
22
23
import android .view .LayoutInflater ;
24
+ import android .view .Menu ;
23
25
import android .view .View ;
24
26
import android .view .ViewGroup ;
25
27
import androidx .annotation .LayoutRes ;
34
36
public class ChipGroupDemoFragment extends DemoFragment {
35
37
36
38
private MaterialSwitch singleSelectionSwitch ;
37
- private MaterialSwitch selectionRequiredSwitch ;
38
39
39
40
@ Nullable
40
41
@ Override
@@ -43,7 +44,7 @@ public View onCreateDemoView(
43
44
View view = layoutInflater .inflate (getChipGroupContent (), viewGroup , false /* attachToRoot */ );
44
45
45
46
singleSelectionSwitch = view .findViewById (R .id .single_selection );
46
- selectionRequiredSwitch = view .findViewById (R .id .selection_required );
47
+ MaterialSwitch selectionRequiredSwitch = view .findViewById (R .id .selection_required );
47
48
48
49
ChipGroup reflowGroup = view .findViewById (R .id .reflow_group );
49
50
ChipGroup scrollGroup = view .findViewById (R .id .scroll_group );
@@ -53,28 +54,28 @@ public View onCreateDemoView(
53
54
reflowGroup .setSingleSelection (isChecked );
54
55
scrollGroup .setSingleSelection (isChecked );
55
56
56
- initChipGroup (reflowGroup );
57
- initChipGroup (scrollGroup );
57
+ initChipGroup (reflowGroup , false );
58
+ initChipGroup (scrollGroup , true );
58
59
});
59
60
60
61
selectionRequiredSwitch .setOnCheckedChangeListener (
61
62
(buttonView , isChecked ) -> {
62
63
reflowGroup .setSelectionRequired (isChecked );
63
64
scrollGroup .setSelectionRequired (isChecked );
64
65
65
- initChipGroup (reflowGroup );
66
- initChipGroup (scrollGroup );
66
+ initChipGroup (reflowGroup , false );
67
+ initChipGroup (scrollGroup , true );
67
68
});
68
69
69
- initChipGroup (reflowGroup );
70
- initChipGroup (scrollGroup );
70
+ initChipGroup (reflowGroup , false );
71
+ initChipGroup (scrollGroup , true );
71
72
72
73
FloatingActionButton fab = view .findViewById (R .id .cat_chip_group_refresh );
73
74
fab .setOnClickListener (
74
75
v -> {
75
76
// Reload the chip group UI.
76
- initChipGroup (reflowGroup );
77
- initChipGroup (scrollGroup );
77
+ initChipGroup (reflowGroup , false );
78
+ initChipGroup (scrollGroup , true );
78
79
});
79
80
return view ;
80
81
}
@@ -91,15 +92,39 @@ protected int getChipGroupItem(boolean singleSelection) {
91
92
: R .layout .cat_chip_group_item_filter ;
92
93
}
93
94
94
- private void initChipGroup (ChipGroup chipGroup ) {
95
+ private void initChipGroup (ChipGroup chipGroup , boolean shouldShowAll ) {
95
96
chipGroup .removeAllViews ();
96
97
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
+
97
112
boolean singleSelection = singleSelectionSwitch .isChecked ();
98
113
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
+
100
124
Chip chip =
101
125
(Chip ) getLayoutInflater ().inflate (getChipGroupItem (singleSelection ), chipGroup , false );
102
- chip .setText (text );
126
+ chip .setId (i );
127
+ chip .setText (textArray [i ]);
103
128
chip .setCloseIconVisible (true );
104
129
chip .setOnCloseIconClickListener (v -> chipGroup .removeView (chip ));
105
130
chipGroup .addView (chip );
0 commit comments