当前位置: 首页 > news >正文

请人代做谷歌外贸网站/百度网址大全电脑版旧版本

请人代做谷歌外贸网站,百度网址大全电脑版旧版本,aspcms网站无法打开,网站建设需要的企业秦子帅明确目标,每天进步一点点.....作者 | 唐子玄地址 | juejin.im/post/5c6a31425188256283254aa0业务场景兴高采烈地前去一周一次的需求大会。为了更加精准的推送,需要采集用户信息,于是乎产品设计了如下界面:没想到&#xf…

c3ac33eb677e5ba13739558d26748767.png

秦子帅明确目标,每天进步一点点.....9163b533ecdcfaaede28c7f5d4797160.png
作者 |  唐子玄地址 |  juejin.im/post/5c6a31425188256283254aa0
业务场景兴高采烈地前去一周一次的需求大会。为了更加精准的推送,需要采集用户信息,于是乎产品设计了如下界面:84739afa689e67436ffa318abc9b8d24.png没想到,在发版本的前一天,突然觉得采集粒度不够细,希望将4个选项增加为6个。面对这突如其来,猝不及防的需求变化,设计和研发组都极力反对。对于设计来说,不仅仅是加两张图,若沿用之前的布局设计,屏幕就放不下6个选项,所以需要重新设计布局。经过设计小姐姐的加班努力,最终设计图改成这样:9bf5a198d5140dea71adc346a1317d4e.png对于开发来说。。。单选按钮有两个标题?两个标题还是不同颜色?选中之后标题居然要变颜色?不怕不怕,别说明天就要发版本,就是今天晚上发也可以。因为我自定义了一个单选控件,这次界面的改动,只需要换2个布局文件。(公司鼓励拥抱变化的价值观,对于开发来说写出“拥抱变化”的代码就是最好的回应)如何定义单选按钮这个抽象?在原生抽象中,单选控件包含两个概念:
  1. 单选组RadioGroup
  2. 单选按钮RadioButton
原生抽象的局限性在于RadioGroupRadioButton是父子关系,即RadioGroup必须是一个明确的ViewGroup类型,这样就约束了RadioButton的布局方式。如果单选组不是一个View,是不是就可以解放这层约束?对于这个问题的答案留一个悬念,抛开单选组,先来看看单选按钮是一个怎么样的抽象。单选按钮应该包含如下基本特性:
  1. 是一个View,且可点击
  2. 有两种状态(选中、未选中),且对应不同的视图
只需要继承View,并利用View.isSelected()就能实现这两个特性。代码如下:
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);
    }@Overridepublic void onClick(View v) {//实现特性2:点击后改变选中状态boolean isSelect = switchSelector();
    }//反转选中状态public boolean switchSelector() {boolean isSelect = this.isSelected();this.setSelected(!isSelect);return !isSelect;
    }
}
为满足业务场景,需要新增附加特性:可自定义按钮内元素相对布局附加特性会随着业务需求变化而变化,可以用模版方法模式将这层变化封装起来:由Selector定义初始化算法框架,将真正界面初始化延后到子类进行。
  • 虽然这次业务场景中,单选按钮元素的布局是:图片在上,文字在下。下次换了咋办?所以定义元素布局应该作为一个抽象函数交给Selector子类实现。
  • 为了实现选中的渐变效果,Selector需提供选项变更的时机。
  • 按钮包含一些基本的属性,比如按钮名称,按钮图标,将这些属性写成自定义属性并传递给子类解析,代码如下:
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) {//读取自定义属性if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Selector);int tagResId = typedArray.getResourceId(R.styleable.Selector_tag, 0);
            tag = context.getString(tagResId);//将自定义属性传递给子类
            onObtainAttrs(typedArray);
            typedArray.recycle();
        } else {
            tag = “default tag”;
        }//构建按钮视图
        View view = onCreateView();
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);this.addView(view, params);this.setOnClickListener(this);
    }public void onObtainAttrs(TypedArray typedArray) {}//子类实现该函数以定义单选按钮元素布局protected abstract View onCreateView();@Overridepublic 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);
}
因为Selector是抽象类,所以必须由子类实现它的抽象,下面的代码即是demo中年龄单选按钮的实现:
public class AgeSelector extends Selector {//声明按钮包含的控件private TextView tvTitle;private ImageView ivIcon;private ImageView ivSelector;private ValueAnimator valueAnimator;@Overridepublic void onObtainAttrs(TypedArray typedArray) {//解析自定义属性
        text = typedArray.getString(R.styleable.Selector_text);
        iconResId = typedArray.getResourceId(R.styleable.Selector_img, 0);
        indicatorResId = typedArray.getResourceId(R.styleable.Selector_indicator, 0);
        textColor = typedArray.getColor(R.styleable.Selector_text_color, Color.parseColor("#FF222222"));
        textSize = typedArray.getInteger(R.styleable.Selector_text_size, 15);
    }private void onBindView(String text, int iconResId, int indicatorResId, int textColor, int textSize) {//将自定义属性绑定到控件if (tvTitle != null) {
            tvTitle.setText(text);
            tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
            tvTitle.setTextColor(textColor);
        }if (ivIcon != null) {
            ivIcon.setImageResource(iconResId);
        }if (ivSelector != null) {
            ivSelector.setImageResource(indicatorResId);
            ivSelector.setAlpha(0);
        }
    }@Overrideprotected View onCreateView() {//构建自定义按钮布局
        View view = LayoutInflater.from(this.getContext()).inflate(R.layout.age_selector, null);
        tvTitle = view.findViewById(R.id.tv_title);
        ivIcon = view.findViewById(R.id.iv_icon);
        ivSelector = view.findViewById(R.id.iv_selector);
        onBindView(text, iconResId, indicatorResId, textColor, textSize);return view;
    }@Overrideprotected 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() {@Overridepublic 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"><ImageViewandroid: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" /><ImageViewandroid: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" /><TextViewandroid: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类型的父亲,因为它管的太多,孩子不能随意布局,局限性大。那就造一个看不见的父亲!其实父亲做的事情不就是 “在一个孩子选中的时候,通知另一个孩子取消选中”吗?有了思路动手就干,代码如下:
public class SelectorGroup {//用于保存上次选中按钮private HashMap selectorMap = new HashMap<>();//获取上次选中按钮public Selector getPreSelector(String groupTag) {return selectorMap.get(groupTag);
    }//取消上次选中按钮的选中状态private void cancelPreSelector(Selector selector) {
        String groupTag = selector.getGroupTag();
        Selector preSelector = getPreSelector(groupTag);if (preSelector != null) {
            preSelector.setSelected(false);
        }
    }//当单选组按钮被点击时,点击事件会通过这个方法传递给单选组void onSelectorClick(Selector selector) {
        selector.setSelected(true);
        cancelPreSelector(selector);//将这次选中按钮保存在map中
        selectorMap.put(selector.getGroupTag(), selector);
    }
}
为了让SelectorGroup统一管理按钮点击后的选中和取消状态变更,需要将按钮的点击事件传递给它,遂修改单选按钮代码如下:
public abstract class Selector extends FrameLayout implements View.OnClickListener {@Overridepublic void onClick(View v) {//传递点击事件给SelectorGroup,它会调用setSelected()来统一管理按钮选中和取消状态if (selectorGroup != null) {
            selectorGroup.onSelectorClick(this);
        }
    }@Overridepublic void setSelected(boolean selected) {boolean isPreSelected = isSelected();super.setSelected(selected);if (isPreSelected != selected) {
            onSwitchSelected(selected);
        }
    }public boolean switchSelector() {boolean isSelect = this.isSelected();this.setSelected(!isSelect);//当选项变更时
        onSwitchSelected(!isSelect);return !isSelect;
    }//选项变更protected abstract void onSwitchSelected(boolean isSelect);
}
现在就可以像这样使用自定义单选按钮了:
public class MainActivity extends AppCompatActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }private void initView() {
        SelectorGroup singleGroup = new SelectorGroup();
        singleGroup.setChoiceMode(SelectorGroup.MODE_SINGLE_CHOICE);
        singleGroup.setStateListener(new SingleChoiceListener());
        ((Selector) findViewById(R.id.single10)).setGroup("single", singleGroup);
        ((Selector) findViewById(R.id.single20)).setGroup("single", singleGroup);
        ((Selector) findViewById(R.id.single30)).setGroup("single", singleGroup);
    }private class SingleChoiceListener implements SelectorGroup.StateListener {@Overridepublic void onStateChange(String tag, boolean isSelected) {
            Toast.makeText(MainActivity.this, tag + " is selected", Toast.LENGTH_SHORT).show();
        }
    }
}
更多除了能快速响应需求变化外,Selector还可以实现更多自定义效果。如下图是个三选一单选组件,选项分居两行形成三角形,且带有渐变选中效果。

---END---

33ca62a384f81ec19b68fa2f4750650b.png

 创作不易,点个“在看5ead4ab0b15bd01ce9cc5e6125557f5c.gif
http://www.jmfq.cn/news/5059531.html

相关文章:

  • 上海网站开发定制/深圳关键词优化
  • 做nba直播网站好/汕头网站建设方案优化
  • 机械网站模板/品牌策划公司介绍
  • 推荐一些能打开的网站/新闻稿发布平台
  • 做商城网站要什么手续费/百度小说app下载
  • 营销的网站/软文发布网站
  • 手机网站域名怎么解析/网络公司网页设计
  • scala做网站/广州谷歌seo公司
  • 泰安建设银行网站/如何在百度发布文章
  • 九江县建设规划局网站/扬州seo推广
  • 资讯网站手机网站模板/深圳网站优化
  • 天津商城网站建设/南昌seo搜索优化
  • php与 wordpress/郑州seo课程
  • wordpress系列文章实现/李江seo
  • 网站建设图片怎么动/推销网站
  • net网站开发找那家/网站seo关键词排名推广
  • 求推荐做ppt的网站/微博上如何做网站推广
  • 静态网站开发课程网/朔州seo
  • 个人网站的制作实验报告/湘潭营销型网站建设
  • 网站制作 南通/2024年3月新冠高峰
  • 龙岗网站建设推广报价/免费网站优化排名
  • 建设银行网站入口/上海app开发公司
  • 什么网站可以接单做设计方案/域名大全
  • 公司网站简介怎么做/全国疫情又严重了
  • 去越南做网站/百度网址大全免费下载
  • 青岛网站建设优化质量可靠/站外推广
  • nas服务器 做网站/市场营销案例100例
  • wordpress+评论+验证码/seo策划
  • 动态网站 编辑软件/企业网站seo贵不贵
  • 济阳县做网站公司/怎么弄一个自己的网址