3232import android .support .v4 .view .animation .FastOutSlowInInterpolator ;
3333import android .support .v4 .view .animation .LinearOutSlowInInterpolator ;
3434import android .util .AttributeSet ;
35+ import android .util .Log ;
3536import android .view .View ;
3637import android .view .ViewGroup ;
3738import android .view .animation .AccelerateDecelerateInterpolator ;
@@ -266,7 +267,6 @@ public void updateViews() {
266267
267268 attrs .setTextColor (b , textColorOnSelection , hasTextColorOnSelection );
268269 attrs .setTintColor (b , drawableTintOnSelection , hasDrawableTintOnSelection );
269- // attrs.setTypeface(b, b.getTypeface());
270270 }
271271
272272 setBackgroundColor (mainGroup , selectorBackgroundDrawable , selectorColor );
@@ -373,36 +373,36 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
373373 }
374374 }
375375
376+
377+ ArrayList <View > ripples = new ArrayList <>();
378+
376379 private void initForeground (final int pos ) {
377380 ButtonAttributes attrs = btnAttrs .get (pos );
378381
379382 /**
380383 * Ripple
381384 * **/
382385 View rippleView = new View (getContext ());
383- btnAttrs . get ( pos ) .setRippleView (rippleView );
386+ attrs .setRippleView (rippleView );
384387 rippleView .setLayoutParams (new LinearLayout .LayoutParams (attrs .getWidth (), 0 , attrs .getWeight ()));
385388
386389 rippleContainer .addView (rippleView );
387390
388391 rippleView .setOnClickListener (new OnClickListener () {
389392 @ Override
390393 public void onClick (View v ) {
391- toggle (pos , animateSelectorDuration , true );
394+ if (clickable && enabled )
395+ toggle (pos , animateSelectorDuration , true );
392396 }
393397 });
394398
395- if (hasRippleColor )
396- Util .setRipple (rippleView , rippleColor );
397- else if (ripple )
398- Util .setSelectableItemBackground (getContext (), rippleView );
399- else {
400- for (Button button : buttons ) {
401- if (button instanceof SegmentedButton && ((SegmentedButton ) button ).hasRipple ())
402- Util .setRipple (rippleView , ((SegmentedButton ) button ).getRippleColor ());
403- }
399+ setRipple (rippleView , enabled && clickable );
400+ if (!enabled ) {
401+ setEnabledColor (enabled );
404402 }
405403
404+ ripples .add (rippleView );
405+
406406 /**
407407 * Divider
408408 * **/
@@ -414,6 +414,23 @@ else if (ripple)
414414 dividerContainer .addView (dividerView );
415415 }
416416
417+ private void setRipple (View v , boolean isClickable ) {
418+ if (isClickable ) {
419+ if (hasRippleColor )
420+ Util .setRipple (v , rippleColor );
421+ else if (ripple )
422+ Util .setSelectableItemBackground (getContext (), v );
423+ else {
424+ for (Button button : buttons ) {
425+ if (button instanceof SegmentedButton && ((SegmentedButton ) button ).hasRipple ())
426+ Util .setRipple (v , ((SegmentedButton ) button ).getRippleColor ());
427+ }
428+ }
429+ } else {
430+ Util .setBackground (v , null );
431+ }
432+ }
433+
417434 private void setContainerAttrs () {
418435 if (isInEditMode ())
419436 mainGroup .setBackgroundColor (backgroundColor );
@@ -423,7 +440,7 @@ private void setContainerAttrs() {
423440
424441 private int selectorColor , animateSelector , animateSelectorDuration , position , backgroundColor , dividerColor , drawableTintOnSelection , textColorOnSelection , dividerSize , rippleColor , dividerPadding , dividerRadius , shadowMargin , shadowMarginTop , shadowMarginBottom , shadowMarginLeft , shadowMarginRight , borderSize , borderColor ;
425442 private float shadowElevation , radius ;
426- private boolean shadow , ripple , hasRippleColor , hasDivider , hasDrawableTintOnSelection , hasTextColorOnSelection ;
443+ private boolean clickable , enabled , shadow , ripple , hasRippleColor , hasDivider , hasDrawableTintOnSelection , hasTextColorOnSelection ;
427444
428445 private Drawable backgroundDrawable , selectorBackgroundDrawable , dividerBackgroundDrawable ;
429446
@@ -470,6 +487,14 @@ private void getAttributes(AttributeSet attrs) {
470487 selectorBackgroundDrawable = typedArray .getDrawable (R .styleable .SegmentedButtonGroup_sbg_selectorBackgroundDrawable );
471488 dividerBackgroundDrawable = typedArray .getDrawable (R .styleable .SegmentedButtonGroup_sbg_dividerBackgroundDrawable );
472489
490+ enabled = typedArray .getBoolean (R .styleable .SegmentedButtonGroup_sbg_enabled , true );
491+
492+ try {
493+ clickable = typedArray .getBoolean (R .styleable .SegmentedButtonGroup_android_clickable , true );
494+ } catch (Exception ex ) {
495+ Log .d ("SegmentedButtonGroup" , ex .toString ());
496+ }
497+
473498 typedArray .recycle ();
474499 }
475500
@@ -899,6 +924,35 @@ public int getMargin() {
899924 return margin ;
900925 }
901926
927+ private void setRippleState (boolean state ) {
928+ for (View v : ripples ) {
929+ setRipple (v , state );
930+ }
931+ }
932+
933+ private void setEnabledColor (boolean enabled ) {
934+ float alpha = 1f ;
935+ if (!enabled )
936+ alpha = 0.5f ;
937+
938+ mainGroup .setAlpha (alpha );
939+ leftGroup .setAlpha (alpha );
940+ rightGroup .setAlpha (alpha );
941+ }
942+
943+ @ Override
944+ public void setEnabled (boolean enabled ) {
945+ this .enabled = enabled ;
946+ setRippleState (enabled );
947+ // setEnabledColor(enabled); // TODO
948+ }
949+
950+ @ Override
951+ public void setClickable (boolean clickable ) {
952+ this .clickable = clickable ;
953+ setRippleState (clickable );
954+ }
955+
902956 @ Override
903957 public Parcelable onSaveInstanceState () {
904958 Bundle bundle = new Bundle ();
@@ -916,5 +970,4 @@ public void onRestoreInstanceState(Parcelable state) {
916970 }
917971 super .onRestoreInstanceState (state );
918972 }
919-
920973}
0 commit comments