Skip to content

Commit aa79a04

Browse files
committed
Fix a bug of Switch.
- When switches are used in ListView, frequently OnCheckListener.onClick() is not called.
1 parent 414a969 commit aa79a04

File tree

1 file changed

+7
-6
lines changed
  • MaterialDesign/src/com/gc/materialdesign/views

1 file changed

+7
-6
lines changed

MaterialDesign/src/com/gc/materialdesign/views/Switch.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ public boolean onTouchEvent(MotionEvent event) {
9191
x = (x < ball.xIni) ? ball.xIni : x;
9292
x = (x > ball.xFin) ? ball.xFin : x;
9393
if (x > ball.xCen) {
94-
check = true;
94+
eventCheck = true;
9595
} else {
96-
check = false;
96+
eventCheck = false;
9797
}
9898
ViewHelper.setX(ball, x);
9999
ball.changeBackground();
@@ -106,7 +106,7 @@ public boolean onTouchEvent(MotionEvent event) {
106106
press = false;
107107
isLastTouch = false;
108108
if (eventCheck != check) {
109-
eventCheck = check;
109+
check = eventCheck;
110110
if (onCheckListener != null)
111111
onCheckListener.onCheck(check);
112112
}
@@ -130,7 +130,7 @@ protected void onDraw(Canvas canvas) {
130130
Canvas temp = new Canvas(bitmap);
131131
Paint paint = new Paint();
132132
paint.setAntiAlias(true);
133-
paint.setColor((check) ? backgroundColor : Color.parseColor("#B0B0B0"));
133+
paint.setColor((eventCheck) ? backgroundColor : Color.parseColor("#B0B0B0"));
134134
paint.setStrokeWidth(Utils.dpToPx(2, getResources()));
135135
temp.drawLine(getHeight() / 2, getHeight() / 2, getWidth()
136136
- getHeight() / 2, getHeight() / 2, paint);
@@ -196,6 +196,7 @@ public void setBackgroundColor(int color) {
196196
public void setChecked(boolean check) {
197197
invalidate();
198198
this.check = check;
199+
this.eventCheck = check;
199200
ball.animateCheck();
200201
}
201202

@@ -213,7 +214,7 @@ public Ball(Context context) {
213214
}
214215

215216
public void changeBackground() {
216-
if (check) {
217+
if (eventCheck) {
217218
setBackgroundResource(R.drawable.background_checkbox);
218219
LayerDrawable layer = (LayerDrawable) getBackground();
219220
GradientDrawable shape = (GradientDrawable) layer
@@ -227,7 +228,7 @@ public void changeBackground() {
227228
public void animateCheck() {
228229
changeBackground();
229230
ObjectAnimator objectAnimator;
230-
if (check) {
231+
if (eventCheck) {
231232
objectAnimator = ObjectAnimator.ofFloat(this, "x", ball.xFin);
232233

233234
} else {

0 commit comments

Comments
 (0)