Browse Source

[공통][Bug] custom switch wrapper
- style은 xml 에서 지정해줘야 함

hyodong.min 7 years ago
parent
commit
d505f5767a

+ 70 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/OnOffSwitch.java

@@ -0,0 +1,70 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view.custom;
+
+import android.content.Context;
+import android.support.annotation.Nullable;
+import android.util.AttributeSet;
+import android.widget.CompoundButton;
+import android.widget.Switch;
+
+import kr.co.zumo.app.R;
+import kr.co.zumo.app.lifeplus.view.fragment.setting.SwitchTrackTextDrawable;
+
+/**
+ * OnOffSwitch
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 10. 24.]   [최초 작성]
+ * @since 2018. 10. 24.
+ */
+public class OnOffSwitch extends Switch {
+
+  protected CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
+
+  public OnOffSwitch(Context context) {
+//    super(context);
+    super(context, null, R.style.OnOffSwitch);
+    init();
+  }
+
+  public OnOffSwitch(Context context, AttributeSet attrs) {
+    super(context, attrs, R.style.OnOffSwitch);
+    init();
+  }
+
+  public OnOffSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
+    super(context, attrs, R.style.OnOffSwitch);
+    init();
+  }
+
+  private void init() {
+    setTrackDrawable(new SwitchTrackTextDrawable(getContext(), R.string.on, R.string.off, false));
+
+    super.setOnCheckedChangeListener((compoundButton, isChecked) -> {
+      performOnCheckedChanged(compoundButton, isChecked);
+
+      if (isChecked) {
+        setTrackDrawable(new SwitchTrackTextDrawable(getContext(), R.string.on, R.string.off, true));
+      }
+      else {
+        setTrackDrawable(new SwitchTrackTextDrawable(getContext(), R.string.on, R.string.off, false));
+      }
+    });
+  }
+
+  private void performOnCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
+    if (null != onCheckedChangeListener) {
+      onCheckedChangeListener.onCheckedChanged(compoundButton, isChecked);
+    }
+  }
+
+  @Override
+  public void setOnCheckedChangeListener(@Nullable OnCheckedChangeListener listener) {
+    onCheckedChangeListener = listener;
+  }
+}

+ 1 - 11
app/src/main/java/kr/co/zumo/app/lifeplus/view/fragment/setting/DefaultSettingViewHolder.java

@@ -35,18 +35,8 @@ public class DefaultSettingViewHolder extends SettingViewHolder {
     imageViewAccordion = itemView.findViewById(R.id.image_view_setting_menu_accordion);
 
     switchPinOnOff = itemView.findViewById(R.id.switch_pin_on_off);
-    switchPinOnOff.setTrackDrawable(new SwitchTrackTextDrawable(context, R.string.on, R.string.off, false));
+    switchPinOnOff.setOnCheckedChangeListener((buttonView, isChecked) -> {
 
-    switchPinOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-      @Override
-      public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
-        if (isChecked) {
-          switchPinOnOff.setTrackDrawable(new SwitchTrackTextDrawable(context, R.string.on, R.string.off, true));
-        }
-        else {
-          switchPinOnOff.setTrackDrawable(new SwitchTrackTextDrawable(context, R.string.on, R.string.off, false));
-        }
-      }
     });
   }
 

+ 3 - 5
app/src/main/res/layout/setting_custom_menu1.xml

@@ -126,17 +126,15 @@
         android:layout_marginTop="0dp"
         android:paddingRight="25dp">
 
-        <Switch
+        <kr.co.zumo.app.lifeplus.view.custom.OnOffSwitch
           android:id="@+id/switch_pin_on_off"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentEnd="true"
           android:layout_marginTop="48dp"
           android:layout_marginBottom="28dp"
-          android:switchMinWidth="46dp"
-          android:thumb="@drawable/switch_thumb_selector"
-          android:background="@drawable/switch_track_selector"
-        />
+          style="@style/OnOffSwitch"
+          />
 
       </RelativeLayout>
     </RelativeLayout>

+ 6 - 0
app/src/main/res/values/styles.xml

@@ -63,4 +63,10 @@
     <item name="android:gravity">center</item>
     <item name="android:lineSpacingExtra">9sp</item>
   </style>
+
+  <style name="OnOffSwitch" parent="@style/Widget.AppCompat.CompoundButton.Switch">
+    <item name="android:switchMinWidth">46dp</item>
+    <item name="android:thumb">@drawable/switch_thumb_selector</item>
+    <item name="android:background">@drawable/switch_track_selector</item>
+  </style>
 </resources>