業務場景
興高采烈地前去一周一次的需求大會。為了更加精準的推送,需要采集用戶信息,于是乎產品設計了如下界面:
沒想到,在發版本的前一天,突然覺得采集粒度不夠細,希望將4個選項增加為6個。面對這突如其來,猝不及防的需求變化,設計和研發組都極力反對。
對于設計來說,不僅僅是加兩張圖,若沿用之前的布局設計,屏幕就放不下6個選項,所以需要重新設計布局。經過設計小姐姐的加班努力,最終設計圖改成這樣:
對于開發來說。。。
單選按鈕有兩個標題?
兩個標題還是不同顏色?
選中之后標題居然要變顏色?
不怕不怕,別說明天就要發版本,就是今天晚上發也可以。因為我自定義了一個單選控件,這次界面的改動,只需要換2個布局文件。(公司鼓勵擁抱變化的價值觀,對于開發來說寫出“擁抱變化”的代碼就是最好的回應)
如何定義單選按鈕這個抽象?
在原生抽象中,單選控件包含兩個概念:
-
單選組
RadioGroup
-
單選按鈕
RadioButton
原生抽象的局限性在于:RadioGroup
和RadioButton
是父子關系,即RadioGroup必須是一個明確的ViewGroup
類型,這樣就約束了RadioButton的布局方式。
如果單選組不是一個View
,是不是就可以解放這層約束?
對于這個問題的答案留一個懸念,拋開單選組,先來看看單選按鈕是一個怎么樣的抽象。
單選按鈕應該包含如下基本特性:
- 是一個View,且可點擊
- 有兩種狀態(選中、未選中),且對應不同的視圖
只需要繼承View,并利用View.isSelected()
就能實現這兩個特性。代碼如下:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
public abstract class Selector extends FrameLayout implements View.OnClickListener {
public Selector(Context context) {
super(context);
initView(context, null);
}
public Selector(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public Selector(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
//實現特性1:可點擊
this.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//實現特性2:點擊后改變選中狀態
boolean isSelect = switchSelector();
}
//反轉選中狀態
public boolean switchSelector() {
boolean isSelect = this.isSelected();
this.setSelected(!isSelect);
return !isSelect;
}
}
為滿足業務場景,需要新增一些附加特性:
- 可自定義按鈕內元素相對布局
附加特性會隨著業務需求變化而變化,所以應該由Selector
提供能力,而讓其子類來實現。
- 雖然這次業務場景中,單選按鈕元素的布局是:圖片在上,文字在下。下次換了咋辦?所以定義元素布局應該作為一個抽象函數交給
Selector
子類實現。 - 為了實現選中的漸變效果,
Selector
需提供選中的時機。 - 雖然
Selector
的子類可以定義不同的元素布局,但都必須包含一些基本元素,比如標題、圖片、標簽名。將這些元素及其屬性抽象成控件自定義屬性。代碼如下:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
public abstract class Selector extends FrameLayout implements View.OnClickListener {
//單選按鈕唯一標示符
private String tag;
public Selector(Context context) {
super(context);
initView(context, null);
}
public Selector(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public Selector(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
//將子類自定義View作為孩子添加進來
View view = onCreateView();
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(view, params);
this.setOnClickListener(this);
//讀取自定義屬性
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Selector);
String text = typedArray.getString(R.styleable.Selector_text);
int iconResId = typedArray.getResourceId(R.styleable.Selector_img, 0);
int selectorResId = typedArray.getResourceId(R.styleable.Selector_indicator, 0);
int textColor = typedArray.getColor(R.styleable.Selector_text_color, Color.parseColor("#FF222222"));
int textSize = typedArray.getInteger(R.styleable.Selector_text_size, 15);
tag = typedArray.getString(R.styleable.Selector_tag);
//將屬性傳遞給孩子
onBindView(text, iconResId, selectorResId, textColor, textSize);
typedArray.recycle();
}
}
//父類讀取自定義屬性后通過該函數傳遞給子類
protected abstract void onBindView(String text, int iconResId, int indicatorResId, int textColorResId, int textSize);
//子類實現該函數以定義單選按鈕元素布局
protected abstract View onCreateView();
public String getTag() {
return tag;
}
@Override
public void onClick(View v) {
boolean isSelect = switchSelector();
}
public boolean switchSelector() {
boolean isSelect = this.isSelected();
this.setSelected(!isSelect);
onSwitchSelected(!isSelect);
return !isSelect;
}
//選中時機
protected abstract void onSwitchSelected(boolean isSelect);
}
自定義屬性src/main/res/values/attrs.xml
如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Selector">
<!--單選按鈕標題-->
<attr name="text" format="string" />
<!--單選按鈕圖片-->
<attr name="img" format="reference" />
<!--單選按鈕選中效果-->
<attr name="indicator" format="reference" />
<!--單選按鈕標題字體大小-->
<attr name="text_size" format="integer" />
<!--單選按鈕字體顏色-->
<attr name="text_color" format="color" />
<!--單選按鈕標簽-->
<attr name="tag" format="string" />
</declare-styleable>
</resources>
因為Selector
是抽象類,所以必須由子類實現它的抽象,下面的代碼即是demo中年齡單選按鈕的實現:
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import taylor.com.selector2.Selector;
public class AgeSelector extends Selector {
private TextView tvTitle;
private ImageView ivIcon;
private ImageView ivSelector;
private ValueAnimator valueAnimator;
public AgeSelector(Context context) {
super(context);
}
public AgeSelector(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AgeSelector(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onBindView(String text, int iconResId, int indicatorResId, int textColorResId, int textSize) {
//在這里將自定義布局中的控件和自定義屬性值綁定
if (tvTitle != null) {
tvTitle.setText(text);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
tvTitle.setTextColor(textColorResId);
}
if (ivIcon != null) {
ivIcon.setImageResource(iconResId);
}
if (ivSelector != null) {
ivSelector.setImageResource(indicatorResId);
ivSelector.setAlpha(0);
}
}
@Override
protected View onCreateView() {
//在這里定義你想要的布局
View view = LayoutInflater.from(this.getContext()).inflate(R.layout.selector, null);
tvTitle = view.findViewById(R.id.tv_title);
ivIcon = view.findViewById(R.id.iv_icon);
ivSelector = view.findViewById(R.id.iv_selector);
return view;
}
@Override
protected void onSwitchSelected(boolean isSelect) {
//單選按鈕狀態變化時做動畫
if (isSelect) {
playSelectedAnimation();
} else {
playUnselectedAnimation();
}
}
private void playUnselectedAnimation() {
if (ivSelector == null) {
return;
}
if (valueAnimator != null) {
valueAnimator.reverse();
}
}
private void playSelectedAnimation() {
if (ivSelector == null) {
return;
}
valueAnimator = ValueAnimator.ofInt(0, 255);
valueAnimator.setDuration(800);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ivSelector.setAlpha((int) animation.getAnimatedValue());
}
});
valueAnimator.start();
}
}
其中單選按鈕的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_selector"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/tv_title"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintVertical_weight="122" />
<ImageView
android:id="@+id/iv_icon"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.026"
app:layout_constraintWidth_percent=".81" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_horizontal|bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_selector"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintVertical_weight="28" />
</android.support.constraint.ConstraintLayout>
如何定義單選組這個抽象?
等等,好像有點不太對勁!如果運行上述代碼,你會發現每個Selector
都運行良好(選中狀態發生變化時有漸變動畫),但多個Selector
可以同時被選中,他們并沒有實現互斥選中。。。
定神一想,發現原因是Selector
這個抽象只關心自己的選中狀態,它并不知道其他Selector
的狀態。
所以原生控件需要RadioGroup
這個角色,它作為父親,了解每個孩子的動向!
但我們不想要一個ViewGroup
類型的父親,因為它管的太多,孩子不能隨意布局,局限性大。
那就造一個看不見的父親!其實父親做的事情不就是“在一個孩子選中的時候,通知另一個孩子取消選中”嗎?
有了思路動手就干,代碼如下:
import java.util.HashSet;
import java.util.Set;
public class SelectorGroup {
//處于同一組的單選按鈕都被保存在這個Set中
private Set<Selector> selectors = new HashSet<>();
public void addSelector(Selector selector) {
selectors.add(selector);
}
public void setSelected(String tag) {
for (Selector s : selectors) {
if (s.getTag().equals(tag)) {
s.switchSelector();
}
}
}
//當一個按鈕選中時,遍歷其他按鈕并取消他們的選中狀態
public void setSelected(Selector selector) {
cancelPreSelector(selector);
}
private void cancelPreSelector(Selector selector) {
for (Selector s : selectors) {
if (!s.equals(selector) && s.isSelected()) {
s.switchSelector();
}
}
}
public Selector getSelected() {
for (Selector s : selectors) {
if (s.isSelected()) {
return s;
}
}
return null;
}
public void clear() {
if (selectors != null) {
selectors.clear();
}
}
}
為了保證單選組中單選按鈕的唯一性,用Set
作為容器,單選按鈕需要實現equals()
和hashCode
以供Set
進行散列定位。完整版的單選按鈕代碼如下:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
public abstract class Selector extends FrameLayout implements View.OnClickListener {
private OnSelectorStateListener stateListener;
private String tag;
private SelectorGroup selectorGroup;
public Selector(Context context) {
super(context);
initView(context, null);
}
public Selector(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public Selector(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
View view = onCreateView();
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(view, params);
this.setOnClickListener(this);
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Selector);
String text = typedArray.getString(R.styleable.Selector_text);
int iconResId = typedArray.getResourceId(R.styleable.Selector_img, 0);
int selectorResId = typedArray.getResourceId(R.styleable.Selector_indicator, 0);
int textColor = typedArray.getColor(R.styleable.Selector_text_color, Color.parseColor("#FF222222"));
int textSize = typedArray.getInteger(R.styleable.Selector_text_size, 15);
tag = typedArray.getString(R.styleable.Selector_tag);
onBindView(text, iconResId, selectorResId, textColor, textSize);
typedArray.recycle();
}
}
public Selector setSelectorGroup(SelectorGroup selectorGroup) {
this.selectorGroup = selectorGroup;
selectorGroup.addSelector(this);
return this;
}
protected abstract void onBindView(String text, int iconResId, int indicatorResId, int textColorResId, int textSize);
protected abstract View onCreateView();
public String getTag() {
return tag;
}
public Selector setOnSelectorStateListener(OnSelectorStateListener stateListener) {
this.stateListener = stateListener;
return this;
}
@Override
public void onClick(View v) {
boolean isSelect = switchSelector();
//單選按鈕將選中狀態告訴單選組
if (selectorGroup != null) {
selectorGroup.setSelected(this);
}
if (stateListener != null) {
stateListener.onStateChange(this, isSelect);
}
}
public boolean switchSelector() {
boolean isSelect = this.isSelected();
this.setSelected(!isSelect);
onSwitchSelected(!isSelect);
return !isSelect;
}
protected abstract void onSwitchSelected(boolean isSelect);
//利用tag生成哈希碼,遂每個單選按鈕的tag需保證唯一
@Override
public int hashCode() {
return this.tag.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Selector) {
return ((Selector) obj).tag.equals(this.tag);
}
return false;
}
public interface OnSelectorStateListener {
void onStateChange(Selector selector, boolean isSelect);
}
}
現在就可以像這樣使用自定義單選按鈕了:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import taylor.com.selector2.Selector;
import taylor.com.selector2.SelectorGroup;
public class MainActivity extends AppCompatActivity implements Selector.OnSelectorStateListener {
private SelectorGroup selectorGroup = new SelectorGroup();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
Selector teenageSelector = findViewById(R.id.selector_10);
Selector manSelector = findViewById(R.id.selector_20);
Selector oldManSelector = findViewById(R.id.selector_30);
teenageSelector.setOnSelectorStateListener(this).setSelectorGroup(selectorGroup);
manSelector.setOnSelectorStateListener(this).setSelectorGroup(selectorGroup);
oldManSelector.setOnSelectorStateListener(this).setSelectorGroup(selectorGroup);
}
@Override
public void onStateChange(Selector selector, boolean isSelect) {
String tag = selector.getTag();
if (isSelect) {
Toast.makeText(this, tag + " is selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, tag + " is unselected", Toast.LENGTH_SHORT).show();
}
}
}
其中布局文件如下,你可以任布局多個單選按鈕:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.Guideline
android:id="@+id/gl_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".5" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Selector age"
android:textSize="30sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<taylor.com.selector.AgeSelector
android:id="@+id/selector_10"
android:layout_width="0dp"
android:layout_height="0dp"
app:img="@mipmap/teenage"
app:indicator="@drawable/age_selctor_shape"
app:layout_constraintBottom_toTopOf="@id/gl_center"
app:layout_constraintDimensionRatio="122:150"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent=".338"
app:tag="teenage"
app:text="teenage"
app:text_color="#FF222222"
app:text_size="16" />
<taylor.com.selector.AgeSelector
android:id="@+id/selector_20"
android:layout_width="0dp"
android:layout_height="0dp"
app:img="@mipmap/man"
app:indicator="@drawable/age_selctor_shape"
app:layout_constraintDimensionRatio="122:150"
app:layout_constraintEnd_toStartOf="@id/selector_30"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/gl_center"
app:layout_constraintWidth_percent=".338"
app:tag="man"
app:text="man"
app:text_color="#FF222222"
app:text_size="16" />
<taylor.com.selector.AgeSelector
android:id="@+id/selector_30"
android:layout_width="0dp"
android:layout_height="0dp"
app:img="@mipmap/old_man"
app:indicator="@drawable/age_selctor_shape"
app:layout_constraintDimensionRatio="122:150"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toEndOf="@id/selector_20"
app:layout_constraintTop_toBottomOf="@id/gl_center"
app:layout_constraintWidth_percent=".338"
app:tag="old man"
app:text="old man"
app:text_color="#FF222222"
app:text_size="16" />
</android.support.constraint.ConstraintLayout>
更多
除了能快速響應需求變化外,Selector
還可以實現更多自定義效果。如下圖是個三選一單選組件,選項分居兩行形成三角形,且帶有漸變選中效果。
-
原生控件
RadioButton
的局限- 不能自定義按鈕選中動畫效果
- 不能自定義按鈕相對布局
RadioGroup
繼承自LinearLayout
,所以RadioButton
的排列方式只能是橫向或縱向一字排開。
用本文中的
Selector
就可以輕而易舉的實現這個效果。