2
2
3
3
import com .gc .materialdesign .R ;
4
4
import com .gc .materialdesign .utils .Utils ;
5
+ import com .gc .materialdesign .views .CheckBox .OnCheckListener ;
5
6
import com .nineoldandroids .animation .ObjectAnimator ;
6
7
import com .nineoldandroids .view .ViewHelper ;
7
8
21
22
22
23
public class Switch extends CustomView {
23
24
24
- final static String ANDROIDXML = "http://schemas.android.com/apk/res/android" ;
25
25
26
26
int backgroundColor = Color .parseColor ("#4CAF50" );
27
27
28
28
Ball ball ;
29
29
30
30
boolean check = false ;
31
+ boolean eventCheck = false ;
31
32
boolean press = false ;
33
+
34
+ OnCheckListener onCheckListener ;
32
35
33
36
public Switch (Context context , AttributeSet attrs ) {
34
37
super (context , attrs );
@@ -67,7 +70,7 @@ protected void setAttributes(AttributeSet attrs){
67
70
}
68
71
69
72
check = attrs .getAttributeBooleanValue (MATERIALDESIGNXML ,"check" , false );
70
-
73
+ eventCheck = check ;
71
74
ball = new Ball (getContext ());
72
75
RelativeLayout .LayoutParams params = new LayoutParams (Utils .dpToPx (20 , getResources ()),Utils .dpToPx (20 , getResources ()));
73
76
params .addRule (RelativeLayout .CENTER_VERTICAL , RelativeLayout .TRUE );
@@ -87,7 +90,6 @@ public boolean onTouchEvent(MotionEvent event) {
87
90
x = (x > ball .xFin )? ball .xFin : x ;
88
91
if (x > ball .xCen ){
89
92
check = true ;
90
-
91
93
}else {
92
94
check = false ;
93
95
}
@@ -102,10 +104,13 @@ public boolean onTouchEvent(MotionEvent event) {
102
104
else if (event .getAction () == MotionEvent .ACTION_UP ) {
103
105
press = false ;
104
106
isLastTouch = false ;
107
+ if (eventCheck != check ){
108
+ eventCheck = check ;
109
+ if (onCheckListener != null )
110
+ onCheckListener .onCheck (check );
111
+ }
105
112
if ((event .getX ()<= getWidth () && event .getX () >= 0 ) &&
106
113
(event .getY ()<= getHeight () && event .getY () >= 0 )){
107
- // check = !check;
108
- // ball.changeBackground();
109
114
ball .animateCheck ();
110
115
}
111
116
}
@@ -218,5 +223,13 @@ public void animateCheck(){
218
223
219
224
220
225
}
226
+
227
+ public void setOncheckListener (OnCheckListener onCheckListener ){
228
+ this .onCheckListener = onCheckListener ;
229
+ }
230
+
231
+ public interface OnCheckListener {
232
+ public void onCheck (boolean check );
233
+ }
221
234
222
235
}
0 commit comments