summaryrefslogtreecommitdiff
path: root/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java
diff options
context:
space:
mode:
Diffstat (limited to 'library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java')
-rw-r--r--library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java178
1 files changed, 84 insertions, 94 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java b/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java
index 8d828ac..56fa742 100644
--- a/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java
+++ b/library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java
@@ -18,12 +18,10 @@ package com.android.setupwizardlib.items;
import android.content.Context;
import android.content.res.TypedArray;
+import androidx.appcompat.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
-
-import androidx.appcompat.widget.SwitchCompat;
-
import com.android.setupwizardlib.R;
/**
@@ -34,101 +32,93 @@ import com.android.setupwizardlib.R;
*/
public class SwitchItem extends Item implements CompoundButton.OnCheckedChangeListener {
- /**
- * Listener for check state changes of this switch item.
- */
- public interface OnCheckedChangeListener {
-
- /**
- * Callback when checked state of a {@link SwitchItem} is changed.
- *
- * @see #setOnCheckedChangeListener(OnCheckedChangeListener)
- */
- void onCheckedChange(SwitchItem item, boolean isChecked);
- }
-
- private boolean mChecked = false;
- private OnCheckedChangeListener mListener;
+ /** Listener for check state changes of this switch item. */
+ public interface OnCheckedChangeListener {
/**
- * Creates a default switch item.
- */
- public SwitchItem() {
- super();
- }
-
- /**
- * Creates a switch item. This constructor is used for inflation from XML.
+ * Callback when checked state of a {@link SwitchItem} is changed.
*
- * @param context The context which this item is inflated in.
- * @param attrs The XML attributes defined on the item.
+ * @see #setOnCheckedChangeListener(OnCheckedChangeListener)
*/
- public SwitchItem(Context context, AttributeSet attrs) {
- super(context, attrs);
- final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem);
- mChecked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false);
- a.recycle();
+ void onCheckedChange(SwitchItem item, boolean isChecked);
+ }
+
+ private boolean checked = false;
+ private OnCheckedChangeListener listener;
+
+ /** Creates a default switch item. */
+ public SwitchItem() {
+ super();
+ }
+
+ /**
+ * Creates a switch item. This constructor is used for inflation from XML.
+ *
+ * @param context The context which this item is inflated in.
+ * @param attrs The XML attributes defined on the item.
+ */
+ public SwitchItem(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwSwitchItem);
+ checked = a.getBoolean(R.styleable.SuwSwitchItem_android_checked, false);
+ a.recycle();
+ }
+
+ /** Sets whether this item should be checked. */
+ public void setChecked(boolean checked) {
+ if (this.checked != checked) {
+ this.checked = checked;
+ notifyItemChanged();
+ if (listener != null) {
+ listener.onCheckedChange(this, checked);
+ }
}
-
- /**
- * Sets whether this item should be checked.
- */
- public void setChecked(boolean checked) {
- if (mChecked != checked) {
- mChecked = checked;
- notifyItemChanged();
- if (mListener != null) {
- mListener.onCheckedChange(this, checked);
- }
- }
- }
-
- /**
- * @return True if this switch item is currently checked.
- */
- public boolean isChecked() {
- return mChecked;
- }
-
- @Override
- protected int getDefaultLayoutResource() {
- return R.layout.suw_items_switch;
- }
-
- /**
- * Toggle the checked state of the switch, without invalidating the entire item.
- *
- * @param view The root view of this item, typically from the argument of onItemClick.
- */
- public void toggle(View view) {
- mChecked = !mChecked;
- final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
- switchView.setChecked(mChecked);
- }
-
- @Override
- public void onBindView(View view) {
- super.onBindView(view);
- final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
- switchView.setOnCheckedChangeListener(null);
- switchView.setChecked(mChecked);
- switchView.setOnCheckedChangeListener(this);
- switchView.setEnabled(isEnabled());
- }
-
- /**
- * Sets a listener to listen for changes in checked state. This listener is invoked in both
- * user toggling the switch and calls to {@link #setChecked(boolean)}.
- */
- public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
- mListener = listener;
- }
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- mChecked = isChecked;
- if (mListener != null) {
- mListener.onCheckedChange(this, isChecked);
- }
+ }
+
+ /** @return True if this switch item is currently checked. */
+ public boolean isChecked() {
+ return checked;
+ }
+
+ @Override
+ protected int getDefaultLayoutResource() {
+ return R.layout.suw_items_switch;
+ }
+
+ /**
+ * Toggle the checked state of the switch, without invalidating the entire item.
+ *
+ * @param view The root view of this item, typically from the argument of onItemClick.
+ */
+ public void toggle(View view) {
+ checked = !checked;
+ final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
+ switchView.setChecked(checked);
+ }
+
+ @Override
+ public void onBindView(View view) {
+ super.onBindView(view);
+ final SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.suw_items_switch);
+ switchView.setOnCheckedChangeListener(null);
+ switchView.setChecked(checked);
+ switchView.setOnCheckedChangeListener(this);
+ switchView.setEnabled(isEnabled());
+ }
+
+ /**
+ * Sets a listener to listen for changes in checked state. This listener is invoked in both user
+ * toggling the switch and calls to {@link #setChecked(boolean)}.
+ */
+ public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
+ this.listener = listener;
+ }
+
+ @Override
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+ checked = isChecked;
+ if (listener != null) {
+ listener.onCheckedChange(this, isChecked);
}
+ }
}