diff options
Diffstat (limited to 'library/gingerbread/src/com/android/setupwizardlib/items')
-rw-r--r-- | library/gingerbread/src/com/android/setupwizardlib/items/ExpandableSwitchItem.java | 239 | ||||
-rw-r--r-- | library/gingerbread/src/com/android/setupwizardlib/items/SwitchItem.java | 178 |
2 files changed, 198 insertions, 219 deletions
diff --git a/library/gingerbread/src/com/android/setupwizardlib/items/ExpandableSwitchItem.java b/library/gingerbread/src/com/android/setupwizardlib/items/ExpandableSwitchItem.java index 71d1bb6..4b9347f 100644 --- a/library/gingerbread/src/com/android/setupwizardlib/items/ExpandableSwitchItem.java +++ b/library/gingerbread/src/com/android/setupwizardlib/items/ExpandableSwitchItem.java @@ -28,7 +28,6 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; - import com.android.setupwizardlib.R; import com.android.setupwizardlib.view.CheckableLinearLayout; @@ -37,140 +36,130 @@ import com.android.setupwizardlib.view.CheckableLinearLayout; * summary, and when that is clicked, will expand to show a longer summary. The end (right for LTR) * side is a switch which can be toggled by the user. * - * Note: It is highly recommended to use this item with recycler view rather than list view, because - * list view draws the touch ripple effect on top of the item, rather than letting the item handle - * it. Therefore you might see a double-ripple, one for the expandable area and one for the entire - * list item, when using this in list view. + * <p>Note: It is highly recommended to use this item with recycler view rather than list view, + * because list view draws the touch ripple effect on top of the item, rather than letting the item + * handle it. Therefore you might see a double-ripple, one for the expandable area and one for the + * entire list item, when using this in list view. */ public class ExpandableSwitchItem extends SwitchItem - implements OnCheckedChangeListener, OnClickListener { - - private CharSequence mCollapsedSummary; - private CharSequence mExpandedSummary; - private boolean mIsExpanded = false; - - public ExpandableSwitchItem() { - super(); - } - - public ExpandableSwitchItem(Context context, AttributeSet attrs) { - super(context, attrs); - final TypedArray a = - context.obtainStyledAttributes(attrs, R.styleable.SuwExpandableSwitchItem); - mCollapsedSummary = a.getText(R.styleable.SuwExpandableSwitchItem_suwCollapsedSummary); - mExpandedSummary = a.getText(R.styleable.SuwExpandableSwitchItem_suwExpandedSummary); - a.recycle(); - } - - @Override - protected int getDefaultLayoutResource() { - return R.layout.suw_items_expandable_switch; + implements OnCheckedChangeListener, OnClickListener { + + private CharSequence collapsedSummary; + private CharSequence expandedSummary; + private boolean isExpanded = false; + + public ExpandableSwitchItem() { + super(); + } + + public ExpandableSwitchItem(Context context, AttributeSet attrs) { + super(context, attrs); + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwExpandableSwitchItem); + collapsedSummary = a.getText(R.styleable.SuwExpandableSwitchItem_suwCollapsedSummary); + expandedSummary = a.getText(R.styleable.SuwExpandableSwitchItem_suwExpandedSummary); + a.recycle(); + } + + @Override + protected int getDefaultLayoutResource() { + return R.layout.suw_items_expandable_switch; + } + + @Override + public CharSequence getSummary() { + return isExpanded ? getExpandedSummary() : getCollapsedSummary(); + } + + /** @return True if the item is currently expanded. */ + public boolean isExpanded() { + return isExpanded; + } + + /** Sets whether the item should be expanded. */ + public void setExpanded(boolean expanded) { + if (isExpanded == expanded) { + return; } - - @Override - public CharSequence getSummary() { - return mIsExpanded ? getExpandedSummary() : getCollapsedSummary(); + isExpanded = expanded; + notifyItemChanged(); + } + + /** @return The summary shown when in collapsed state. */ + public CharSequence getCollapsedSummary() { + return collapsedSummary; + } + + /** + * Sets the summary text shown when the item is collapsed. Corresponds to the {@code + * app:suwCollapsedSummary} XML attribute. + */ + public void setCollapsedSummary(CharSequence collapsedSummary) { + this.collapsedSummary = collapsedSummary; + if (!isExpanded()) { + notifyChanged(); } - - /** - * @return True if the item is currently expanded. - */ - public boolean isExpanded() { - return mIsExpanded; + } + + /** @return The summary shown when in expanded state. */ + public CharSequence getExpandedSummary() { + return expandedSummary; + } + + /** + * Sets the summary text shown when the item is expanded. Corresponds to the {@code + * app:suwExpandedSummary} XML attribute. + */ + public void setExpandedSummary(CharSequence expandedSummary) { + this.expandedSummary = expandedSummary; + if (isExpanded()) { + notifyChanged(); } + } - /** - * Sets whether the item should be expanded. - */ - public void setExpanded(boolean expanded) { - if (mIsExpanded == expanded) { - return; - } - mIsExpanded = expanded; - notifyItemChanged(); - } + @Override + public void onBindView(View view) { + // TODO: If it is possible to detect, log a warning if this is being used with ListView. + super.onBindView(view); + View content = view.findViewById(R.id.suw_items_expandable_switch_content); + content.setOnClickListener(this); - /** - * @return The summary shown when in collapsed state. - */ - public CharSequence getCollapsedSummary() { - return mCollapsedSummary; + if (content instanceof CheckableLinearLayout) { + ((CheckableLinearLayout) content).setChecked(isExpanded()); } - /** - * Sets the summary text shown when the item is collapsed. Corresponds to the - * {@code app:suwCollapsedSummary} XML attribute. - */ - public void setCollapsedSummary(CharSequence collapsedSummary) { - mCollapsedSummary = collapsedSummary; - if (!isExpanded()) { - notifyChanged(); + tintCompoundDrawables(view); + + // Expandable switch item has focusability on the expandable layout on the left, and the + // switch on the right, but not the item itself. + view.setFocusable(false); + } + + @Override + public void onClick(View v) { + setExpanded(!isExpanded()); + } + + // Tint the expand arrow with the text color + private void tintCompoundDrawables(View view) { + final TypedArray a = + view.getContext().obtainStyledAttributes(new int[] {android.R.attr.textColorPrimary}); + final ColorStateList tintColor = a.getColorStateList(0); + a.recycle(); + + if (tintColor != null) { + TextView titleView = (TextView) view.findViewById(R.id.suw_items_title); + for (Drawable drawable : titleView.getCompoundDrawables()) { + if (drawable != null) { + drawable.setColorFilter(tintColor.getDefaultColor(), Mode.SRC_IN); } - } - - /** - * @return The summary shown when in expanded state. - */ - public CharSequence getExpandedSummary() { - return mExpandedSummary; - } - - /** - * Sets the summary text shown when the item is expanded. Corresponds to the - * {@code app:suwExpandedSummary} XML attribute. - */ - public void setExpandedSummary(CharSequence expandedSummary) { - mExpandedSummary = expandedSummary; - if (isExpanded()) { - notifyChanged(); - } - } - - @Override - public void onBindView(View view) { - // TODO: If it is possible to detect, log a warning if this is being used with ListView. - super.onBindView(view); - View content = view.findViewById(R.id.suw_items_expandable_switch_content); - content.setOnClickListener(this); - - if (content instanceof CheckableLinearLayout) { - ((CheckableLinearLayout) content).setChecked(isExpanded()); - } - - tintCompoundDrawables(view); - - // Expandable switch item has focusability on the expandable layout on the left, and the - // switch on the right, but not the item itself. - view.setFocusable(false); - } - - @Override - public void onClick(View v) { - setExpanded(!isExpanded()); - } - - // Tint the expand arrow with the text color - private void tintCompoundDrawables(View view) { - final TypedArray a = view.getContext() - .obtainStyledAttributes(new int[] {android.R.attr.textColorPrimary}); - final ColorStateList tintColor = a.getColorStateList(0); - a.recycle(); - - if (tintColor != null) { - TextView titleView = (TextView) view.findViewById(R.id.suw_items_title); - for (Drawable drawable : titleView.getCompoundDrawables()) { - if (drawable != null) { - drawable.setColorFilter(tintColor.getDefaultColor(), Mode.SRC_IN); - } - } - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { - for (Drawable drawable : titleView.getCompoundDrawablesRelative()) { - if (drawable != null) { - drawable.setColorFilter(tintColor.getDefaultColor(), Mode.SRC_IN); - } - } - } - + } + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + for (Drawable drawable : titleView.getCompoundDrawablesRelative()) { + if (drawable != null) { + drawable.setColorFilter(tintColor.getDefaultColor(), Mode.SRC_IN); + } } + } } + } } 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); } + } } |