@@ -77,7 +77,7 @@ class EndCompoundLayout extends LinearLayout {
7777 private OnLongClickListener errorIconOnLongClickListener ;
7878
7979 @ NonNull private final CheckableImageButton endIconView ;
80- private final SparseArray < EndIconDelegate > endIconDelegates = new SparseArray <>() ;
80+ private final EndIconDelegates endIconDelegates ;
8181 @ EndIconMode private int endIconMode = END_ICON_NONE ;
8282 private final LinkedHashSet <OnEndIconChangedListener > endIconChangedListeners =
8383 new LinkedHashSet <>();
@@ -111,11 +111,12 @@ class EndCompoundLayout extends LinearLayout {
111111 LayoutInflater layoutInflater = LayoutInflater .from (getContext ());
112112 errorIconView = createIconView (this , layoutInflater , R .id .text_input_error_icon );
113113 endIconView = createIconView (endIconFrame , layoutInflater , R .id .text_input_end_icon );
114+ endIconDelegates = new EndIconDelegates (this , a );
114115
115116 suffixTextView = new AppCompatTextView (getContext ());
116117
117118 initErrorIconView (a );
118- initEndIconDelegates (a );
119+ initEndIconView (a );
119120 initSuffixTextView (a );
120121
121122 endIconFrame .addView (endIconView );
@@ -162,21 +163,7 @@ private void initErrorIconView(TintTypedArray a) {
162163 errorIconView .setFocusable (false );
163164 }
164165
165- private void initEndIconDelegates (TintTypedArray a ) {
166- int endIconDrawableId = a .getResourceId (R .styleable .TextInputLayout_endIconDrawable , 0 );
167- endIconDelegates .append (END_ICON_CUSTOM , new CustomEndIconDelegate (this , endIconDrawableId ));
168- endIconDelegates .append (END_ICON_NONE , new NoEndIconDelegate (this ));
169- endIconDelegates .append (
170- END_ICON_PASSWORD_TOGGLE ,
171- new PasswordToggleEndIconDelegate (
172- this ,
173- endIconDrawableId == 0
174- ? a .getResourceId (R .styleable .TextInputLayout_passwordToggleDrawable , 0 )
175- : endIconDrawableId ));
176- endIconDelegates .append (
177- END_ICON_CLEAR_TEXT , new ClearTextEndIconDelegate (this , endIconDrawableId ));
178- endIconDelegates .append (
179- END_ICON_DROPDOWN_MENU , new DropdownMenuEndIconDelegate (this , endIconDrawableId ));
166+ private void initEndIconView (TintTypedArray a ) {
180167 // Set up the end icon if any.
181168 if (!a .hasValue (R .styleable .TextInputLayout_passwordToggleEnabled )) {
182169 // Default tint for any end icon or value specified by user
@@ -278,8 +265,7 @@ CheckableImageButton getEndIconView() {
278265 }
279266
280267 EndIconDelegate getEndIconDelegate () {
281- EndIconDelegate endIconDelegate = endIconDelegates .get (endIconMode );
282- return endIconDelegate != null ? endIconDelegate : endIconDelegates .get (END_ICON_NONE );
268+ return endIconDelegates .get (endIconMode );
283269 }
284270
285271 @ EndIconMode
@@ -621,4 +607,46 @@ private void tintEndIconOnError(boolean tintEndIconOnError) {
621607 applyIconTint (textInputLayout , endIconView , endIconTintList , endIconTintMode );
622608 }
623609 }
610+
611+ private static class EndIconDelegates {
612+ private final SparseArray <EndIconDelegate > delegates = new SparseArray <>();
613+
614+ private final EndCompoundLayout endLayout ;
615+ private final int endIconDrawableId ;
616+ private final int passwordIconDrawableId ;
617+
618+ EndIconDelegates (EndCompoundLayout endLayout , TintTypedArray a ) {
619+ this .endLayout = endLayout ;
620+ endIconDrawableId = a .getResourceId (R .styleable .TextInputLayout_endIconDrawable , 0 );
621+ passwordIconDrawableId =
622+ a .getResourceId (R .styleable .TextInputLayout_passwordToggleDrawable , 0 );
623+ }
624+
625+ EndIconDelegate get (@ EndIconMode int endIconMode ) {
626+ EndIconDelegate delegate = delegates .get (endIconMode );
627+ if (delegate == null ) {
628+ delegate = create (endIconMode );
629+ delegates .append (endIconMode , delegate );
630+ }
631+ return delegate ;
632+ }
633+
634+ private EndIconDelegate create (@ EndIconMode int endIconMode ) {
635+ switch (endIconMode ) {
636+ case END_ICON_PASSWORD_TOGGLE :
637+ return new PasswordToggleEndIconDelegate (
638+ endLayout , endIconDrawableId == 0 ? passwordIconDrawableId : endIconDrawableId );
639+ case END_ICON_CLEAR_TEXT :
640+ return new ClearTextEndIconDelegate (endLayout , endIconDrawableId );
641+ case END_ICON_DROPDOWN_MENU :
642+ return new DropdownMenuEndIconDelegate (endLayout , endIconDrawableId );
643+ case END_ICON_CUSTOM :
644+ return new CustomEndIconDelegate (endLayout , endIconDrawableId );
645+ case END_ICON_NONE :
646+ return new NoEndIconDelegate (endLayout );
647+ default :
648+ throw new IllegalArgumentException ("Invalid end icon mode: " + endIconMode );
649+ }
650+ }
651+ }
624652}
0 commit comments