@@ -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}
0 commit comments