32
32
import android .support .v4 .view .animation .FastOutSlowInInterpolator ;
33
33
import android .support .v4 .view .animation .LinearOutSlowInInterpolator ;
34
34
import android .util .AttributeSet ;
35
+ import android .util .Log ;
35
36
import android .view .View ;
36
37
import android .view .ViewGroup ;
37
38
import android .view .animation .AccelerateDecelerateInterpolator ;
@@ -266,7 +267,6 @@ public void updateViews() {
266
267
267
268
attrs .setTextColor (b , textColorOnSelection , hasTextColorOnSelection );
268
269
attrs .setTintColor (b , drawableTintOnSelection , hasDrawableTintOnSelection );
269
- // attrs.setTypeface(b, b.getTypeface());
270
270
}
271
271
272
272
setBackgroundColor (mainGroup , selectorBackgroundDrawable , selectorColor );
@@ -373,36 +373,36 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
373
373
}
374
374
}
375
375
376
+
377
+ ArrayList <View > ripples = new ArrayList <>();
378
+
376
379
private void initForeground (final int pos ) {
377
380
ButtonAttributes attrs = btnAttrs .get (pos );
378
381
379
382
/**
380
383
* Ripple
381
384
* **/
382
385
View rippleView = new View (getContext ());
383
- btnAttrs . get ( pos ) .setRippleView (rippleView );
386
+ attrs .setRippleView (rippleView );
384
387
rippleView .setLayoutParams (new LinearLayout .LayoutParams (attrs .getWidth (), 0 , attrs .getWeight ()));
385
388
386
389
rippleContainer .addView (rippleView );
387
390
388
391
rippleView .setOnClickListener (new OnClickListener () {
389
392
@ Override
390
393
public void onClick (View v ) {
391
- toggle (pos , animateSelectorDuration , true );
394
+ if (clickable && enabled )
395
+ toggle (pos , animateSelectorDuration , true );
392
396
}
393
397
});
394
398
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 );
404
402
}
405
403
404
+ ripples .add (rippleView );
405
+
406
406
/**
407
407
* Divider
408
408
* **/
@@ -414,6 +414,23 @@ else if (ripple)
414
414
dividerContainer .addView (dividerView );
415
415
}
416
416
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
+
417
434
private void setContainerAttrs () {
418
435
if (isInEditMode ())
419
436
mainGroup .setBackgroundColor (backgroundColor );
@@ -423,7 +440,7 @@ private void setContainerAttrs() {
423
440
424
441
private int selectorColor , animateSelector , animateSelectorDuration , position , backgroundColor , dividerColor , drawableTintOnSelection , textColorOnSelection , dividerSize , rippleColor , dividerPadding , dividerRadius , shadowMargin , shadowMarginTop , shadowMarginBottom , shadowMarginLeft , shadowMarginRight , borderSize , borderColor ;
425
442
private float shadowElevation , radius ;
426
- private boolean shadow , ripple , hasRippleColor , hasDivider , hasDrawableTintOnSelection , hasTextColorOnSelection ;
443
+ private boolean clickable , enabled , shadow , ripple , hasRippleColor , hasDivider , hasDrawableTintOnSelection , hasTextColorOnSelection ;
427
444
428
445
private Drawable backgroundDrawable , selectorBackgroundDrawable , dividerBackgroundDrawable ;
429
446
@@ -470,6 +487,14 @@ private void getAttributes(AttributeSet attrs) {
470
487
selectorBackgroundDrawable = typedArray .getDrawable (R .styleable .SegmentedButtonGroup_sbg_selectorBackgroundDrawable );
471
488
dividerBackgroundDrawable = typedArray .getDrawable (R .styleable .SegmentedButtonGroup_sbg_dividerBackgroundDrawable );
472
489
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
+
473
498
typedArray .recycle ();
474
499
}
475
500
@@ -899,6 +924,35 @@ public int getMargin() {
899
924
return margin ;
900
925
}
901
926
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
+
902
956
@ Override
903
957
public Parcelable onSaveInstanceState () {
904
958
Bundle bundle = new Bundle ();
@@ -916,5 +970,4 @@ public void onRestoreInstanceState(Parcelable state) {
916
970
}
917
971
super .onRestoreInstanceState (state );
918
972
}
919
-
920
973
}
0 commit comments