-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckableFloatingActionButton.java
93 lines (74 loc) · 2.64 KB
/
CheckableFloatingActionButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package io.errorlab.widget;
import android.content.Context;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.Checkable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class CheckableFloatingActionButton extends FloatingActionButton implements Checkable {
@SuppressWarnings("PublicInnerClass")
@FunctionalInterface
public interface OnCheckedChangeListener {
void onCheckedChanged(@NonNull FloatingActionButton fabView, boolean isChecked);
}
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked, };
private boolean mChecked;
private OnCheckedChangeListener mOnCheckedChangeListener;
public CheckableFloatingActionButton(@NonNull Context ctx) {
this(ctx, null);
}
public CheckableFloatingActionButton(@NonNull Context ctx, @Nullable AttributeSet attrs) {
this(ctx, attrs, 0);
}
public CheckableFloatingActionButton(@NonNull Context ctx, @Nullable AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
}
@Override
protected void onRestoreInstanceState(@Nullable Parcelable state) {
if (!(state instanceof CheckedSavedState)) {
super.onRestoreInstanceState(state);
return;
}
CheckedSavedState ss = (CheckedSavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setChecked(ss.checked);
}
@NonNull
@Override
protected Parcelable onSaveInstanceState() {
CheckedSavedState result = new CheckedSavedState(super.onSaveInstanceState());
result.checked = mChecked;
return result;
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void setChecked(boolean checked) {
if (checked != mChecked) {
mChecked = checked;
refreshDrawableState();
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, checked);
}
}
}
@NonNull
@Override
public int[] onCreateDrawableState(int extraSpace) {
int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (mChecked) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
}