Android仿ios条件选择器pickerview

最近怎么老写View,可能写view比较方便,写其它东西还要抽时间整理总结,写View就直接封完写出来就行。

准备国庆放假,无心工作,那就写篇简单实用一点的文章,总不能白白浪费了时间。

一.需求

有时候ios端会用到条件选择器,好像是那边自带的,而android这边是没有的,但是为了两端统一,没办法,只能我们去迁就他们了(你让一个有自带的去写自定义是基本不可能的事)。
最经典的是我们有选择地址的需求,比如美团这里的:

Android仿ios条件选择器pickerview
image.png

这个android是原生是没有的,只有能选择日期的。那怎么办?自定义,好像略难,那就用三方的吧。

https://github.com/Bigkoo/Android-PickerView

Android仿ios条件选择器pickerview
image.png

我找了很多,就觉得这个库是做得比较好,比较完整的,而且也一直有在维护,还是比较推荐,使用起来也比较方便。项目里有很清晰的文档,建议看之前先浏览过文档。

二.效果展示

我使用的效果:

Android仿ios条件选择器pickerview
image.png

我还是顺便把源码也浏览了下。发现这里有3个比较重要的类,这个之后会简单的介绍:
(1)WheelView
(2)条件选择的WheelOptions,我感觉这个类的封装有点vm的意思
(3)最外层封装的OptionsPickerView

三.扩展

如果只是为了选择地址的话直接用它封装好的就行,但是有时候可能会需要用到其它的布局或需求,那我们就要在它原有的功能上进行扩展,比如说我写的这个时间段的现在,直接用是没有的,需要自己扩展。

Android仿ios条件选择器pickerview
image.png

而要进行扩展的话,就要先浏览源码看看它内部怎么写的。

可以从调用的地方找到OptionsPickerView类

OptionsPickerView pvOptions1 = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                // todo 点击确认之后的操作
            }
        })
                .setTitleText("选择地区")
                .build();
        pvOptions1.setPicker(items1, items2,items3);//二级选择器
        pvOptions1.show();

然后看看OptionsPickerView类内部,你会发现很多方法,但是基本都是builder方法个getset方法,我们可以找到重要的几个方法。

private void initView(Context context) {
        setDialogOutSideCancelable(cancelable);
        initViews(backgroundId);
        init();
        initEvents();
        if (customListener == null) {
            LayoutInflater.from(context).inflate(layoutRes, contentContainer);

            //顶部标题
            tvTitle = (TextView) findViewById(R.id.tvTitle);
            rv_top_bar = (RelativeLayout)findViewById(R.id.rv_topbar);

            //确定和取消按钮
            btnSubmit = (Button) findViewById(R.id.btnSubmit);
            btnCancel = (Button) findViewById(R.id.btnCancel);

            btnSubmit.setTag(TAG_SUBMIT);
            btnCancel.setTag(TAG_CANCEL);
            btnSubmit.setOnClickListener(this);
            btnCancel.setOnClickListener(this);

            //设置文字
            btnSubmit.setText(TextUtils.isEmpty(Str_Submit) ? context.getResources().getString(R.string.pickerview_submit) : Str_Submit);
            btnCancel.setText(TextUtils.isEmpty(Str_Cancel) ? context.getResources().getString(R.string.pickerview_cancel) : Str_Cancel);
            tvTitle.setText(TextUtils.isEmpty(Str_Title) ? "" : Str_Title);//默认为空

            //设置color
            btnSubmit.setTextColor(Color_Submit == 0 ? pickerview_timebtn_nor : Color_Submit);
            btnCancel.setTextColor(Color_Cancel == 0 ? pickerview_timebtn_nor : Color_Cancel);
            tvTitle.setTextColor(Color_Title == 0 ? pickerview_topbar_title : Color_Title);
            rv_top_bar.setBackgroundColor(Color_Background_Title == 0 ? pickerview_bg_topbar : Color_Background_Title);

            //设置文字大小
            btnSubmit.setTextSize(Size_Submit_Cancel);
            btnCancel.setTextSize(Size_Submit_Cancel);
            tvTitle.setTextSize(Size_Title);
            tvTitle.setText(Str_Title);
        } else {
            customListener.customLayout(LayoutInflater.from(context).inflate(layoutRes, contentContainer));
        }

        // ----滚轮布局
        final LinearLayout optionsPicker = (LinearLayout) findViewById(R.id.optionspicker);
        optionsPicker.setBackgroundColor(Color_Background_Wheel == 0 ? bgColor_default : Color_Background_Wheel);

        wheelOptions = new WheelOptions(optionsPicker, linkage);
        wheelOptions.setTextContentSize(Size_Content);
        wheelOptions.setLabels(label1, label2, label3);
        wheelOptions.setCyclic(cyclic1, cyclic2, cyclic3);
        wheelOptions.setTypeface(font);

        setOutSideCancelable(cancelable);

        if (tvTitle!= null){
            tvTitle.setText(Str_Title);
        }

        wheelOptions.setDividerColor(dividerColor);
        wheelOptions.setDividerType(dividerType);
        wheelOptions.setLineSpacingMultiplier(lineSpacingMultiplier);
        wheelOptions.setTextColorOut(textColorOut);
        wheelOptions.setTextColorCenter(textColorCenter);
        wheelOptions.isCenterLabel(isCenterLabel);

    }

这里做的是为view设置属性。重要的是这里

 final LinearLayout optionsPicker = (LinearLayout) findViewById(R.id.optionspicker);
        optionsPicker.setBackgroundColor(Color_Background_Wheel == 0 ? bgColor_default : Color_Background_Wheel);

        wheelOptions = new WheelOptions(optionsPicker, linkage);

这里的意思就是把这个View给WheelOptions这个对象,让它来做处理。然后可以看
看布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        layout="@layout/include_pickerview_topbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/pickerview_topbar_height" />

    <LinearLayout
        android:id="@+id/optionspicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <com.bigkoo.pickerview.lib.WheelView
            android:id="@+id/options1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <com.bigkoo.pickerview.lib.WheelView
            android:id="@+id/options2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <com.bigkoo.pickerview.lib.WheelView
            android:id="@+id/options3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>


</LinearLayout>

可以看出它里面是写死固定就是3列。其实我不太赞成这样的做法,对于这样的多情况view的封装,我个人还是比较喜欢做动态的。由于这里固定是3列,所以我上图中4列的情况直接使用是实现不了的,所以需要扩展。这里的WheelView就是单列

1.重写布局

它这里布局写死了固定3列,那我肯定是没法复用它的这个布局了,所以就只能重写布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        layout="@layout/include_pickerview_topbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/pickerview_topbar_height" />

    <LinearLayout
        android:id="@+id/optionspicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

    </LinearLayout>


</LinearLayout>

我只写了LinearLayout,就是要动态去添加WheelView。

2.重写OptionsPickerView

原本的OptionsPickerView中


Android仿ios条件选择器pickerview
image.png

在builder构造时就固定了布局,所以我这不好扩展,不如重写一个OptionsPickerView,当然重写Builder也行,但是我觉得重写OptionsPickerView比较好。而且他原本只有两个类

Android仿ios条件选择器pickerview
image.png

所以我们需要继承BasePickerView重写一个PickerView,他原本内部的逻辑没问题,我就抄过来用好了。

public class BerNpickerView<T> extends BasePickerView implements View.OnClickListener {


    protected BerWheel<T> berWheel;
    private int layoutRes;
    private CustomListener customListener;
    private Button btnSubmit, btnCancel; //确定、取消按钮
    private TextView tvTitle;
    private RelativeLayout rv_top_bar;

    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";

    private OnOptionsSelectListener optionsSelectListener;

    private String Str_Submit;//确定按钮文字
    private String Str_Cancel;//取消按钮文字
    private String Str_Title;//标题文字

    private int Color_Submit;//确定按钮颜色
    private int Color_Cancel;//取消按钮颜色
    private int Color_Title;//标题颜色

    private int Color_Background_Wheel;//滚轮背景颜色
    private int Color_Background_Title;//标题背景颜色

    private int Size_Submit_Cancel;//确定取消按钮大小
    private int Size_Title;//标题文字大小
    private int Size_Content;//内容文字大小

    private int textColorOut; //分割线以外的文字颜色
    private int textColorCenter; //分割线之间的文字颜色
    private int dividerColor; //分割线的颜色
    private int backgroundId; //显示时的外部背景色颜色,默认是灰色
    // 条目间距倍数 默认1.6
    private float lineSpacingMultiplier = 1.6F;
    private boolean isDialog;//是否是对话框模式

    private boolean cancelable;//是否能取消
    private boolean linkage;//是否联动

    private boolean isCenterLabel ;//是否只显示中间的label

    private String label1;//单位
    private String label2;
    private String label3;
    private String label4;

    private boolean cyclic1;//是否循环
    private boolean cyclic2;
    private boolean cyclic3;

    private Typeface font;//字体样式

    private int option1;//默认选中项
    private int option2;
    private int option3;
    private WheelView.DividerType dividerType;//分隔线类型
    private int total; // 列表数量

    public BerNpickerView(Builder builder) {
        super(builder.context);
        this.optionsSelectListener = builder.optionsSelectListener;
        this.Str_Submit = builder.Str_Submit;
        this.Str_Cancel = builder.Str_Cancel;
        this.Str_Title = builder.Str_Title;

        this.Color_Submit = builder.Color_Submit;
        this.Color_Cancel = builder.Color_Cancel;
        this.Color_Title = builder.Color_Title;
        this.Color_Background_Wheel = builder.Color_Background_Wheel;
        this.Color_Background_Title = builder.Color_Background_Title;

        this.Size_Submit_Cancel = builder.Size_Submit_Cancel;
        this.Size_Title = builder.Size_Title;
        this.Size_Content = builder.Size_Content;

        this.cyclic1 = builder.cyclic1;
        this.cyclic2 = builder.cyclic2;
        this.cyclic3 = builder.cyclic3;

        this.cancelable = builder.cancelable;
        this.linkage = builder.linkage;
        this.isCenterLabel = builder.isCenterLabel;

        this.label1 = builder.label1;
        this.label2 = builder.label2;
        this.label3 = builder.label3;
        this.label4 = builder.label4;

        this.font = builder.font;


        this.option1 = builder.option1;
        this.option2 = builder.option2;
        this.option3 = builder.option3;
        this.textColorCenter = builder.textColorCenter;
        this.textColorOut = builder.textColorOut;
        this.dividerColor = builder.dividerColor;
        this.lineSpacingMultiplier = builder.lineSpacingMultiplier;
        this.customListener = builder.customListener;
        this.layoutRes = builder.layoutRes;
        this.isDialog = builder.isDialog;
        this.dividerType = builder.dividerType;
        this.backgroundId = builder.backgroundId;
        this.decorView = builder.decorView;
        this.total = builder.total;
        initView(builder.context);
    }

    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        if (tag.equals(TAG_SUBMIT)) {
            returnData();
        }
        dismiss();
    }

    //抽离接口回调的方法
    public void returnData() {
        if (optionsSelectListener != null) {
            List<T> datalist = berWheel.getCurrentItems();
            optionsSelectListener.onOptionsSelect(datalist, clickView);
        }
    }

    private void initView(Context context) {
        setDialogOutSideCancelable(cancelable);
        initViews(backgroundId);
        init();
        initEvents();
        if (customListener == null) {
            LayoutInflater.from(context).inflate(layoutRes, contentContainer);

            //顶部标题
            tvTitle = (TextView) findViewById(com.bigkoo.pickerview.R.id.tvTitle);
            rv_top_bar = (RelativeLayout)findViewById(com.bigkoo.pickerview.R.id.rv_topbar);

            //确定和取消按钮
            btnSubmit = (Button) findViewById(com.bigkoo.pickerview.R.id.btnSubmit);
            btnCancel = (Button) findViewById(com.bigkoo.pickerview.R.id.btnCancel);

            btnSubmit.setTag(TAG_SUBMIT);
            btnCancel.setTag(TAG_CANCEL);
            btnSubmit.setOnClickListener(this);
            btnCancel.setOnClickListener(this);

            //设置文字
            btnSubmit.setText(TextUtils.isEmpty(Str_Submit) ? context.getResources().getString(com.bigkoo.pickerview.R.string.pickerview_submit) : Str_Submit);
            btnCancel.setText(TextUtils.isEmpty(Str_Cancel) ? context.getResources().getString(com.bigkoo.pickerview.R.string.pickerview_cancel) : Str_Cancel);
            tvTitle.setText(TextUtils.isEmpty(Str_Title) ? "" : Str_Title);//默认为空

            //设置color
            btnSubmit.setTextColor(Color_Submit == 0 ? pickerview_timebtn_nor : Color_Submit);
            btnCancel.setTextColor(Color_Cancel == 0 ? pickerview_timebtn_nor : Color_Cancel);
            tvTitle.setTextColor(Color_Title == 0 ? pickerview_topbar_title : Color_Title);
            rv_top_bar.setBackgroundColor(Color_Background_Title == 0 ? pickerview_bg_topbar : Color_Background_Title);

            //设置文字大小
            btnSubmit.setTextSize(Size_Submit_Cancel);
            btnCancel.setTextSize(Size_Submit_Cancel);
            tvTitle.setTextSize(Size_Title);
            tvTitle.setText(Str_Title);
        } else {
            customListener.customLayout(LayoutInflater.from(context).inflate(layoutRes, contentContainer));
        }

        // ----滚轮布局
        final LinearLayout optionsPicker = (LinearLayout) findViewById(com.bigkoo.pickerview.R.id.optionspicker);
        optionsPicker.setBackgroundColor(Color_Background_Wheel == 0 ? bgColor_default : Color_Background_Wheel);

        createWheel(optionsPicker, context, total);
        berWheel.setTextContentSize(Size_Content);
//        berWheel.setLabels(label1, label2, label3);
//        berWheel.setCyclic(cyclic1, cyclic2, cyclic3);
        berWheel.setTypeface(font);

        setOutSideCancelable(cancelable);

        if (tvTitle!= null){
            tvTitle.setText(Str_Title);
        }

        berWheel.setDividerColor(dividerColor);
        berWheel.setDividerType(dividerType);
        berWheel.setLineSpacingMultiplier(lineSpacingMultiplier);
        berWheel.setTextColorOut(textColorOut);
        berWheel.setTextColorCenter(textColorCenter);
        berWheel.isCenterLabel(isCenterLabel);

    }

    /**
     *  创建子类BerWheel
     */
    protected void createWheel(View view, Context context, int total){
        berWheel = new BerWheel(view, context, total);
    }

    //不联动情况下调用
    public void setNPicker(List<List<T>> items) {
        berWheel.setNPicker(items);
        int[] options = new int[items.size()];
        for (int i = 0; i < items.size(); i++) {
            options[i] = 0;
        }
//        SetCurrentItems(options);
    }

    private void SetCurrentItems(int[] options) {
        if(berWheel!=null){
            berWheel.setCurrentItems(options);
        }
    }

    /**
     *  ==================================================================
     *   builder
     *  ==============================================================
     */
    //建造器
    public static class Builder {
        private int layoutRes = R.layout.layout_ber_pickerview;
        private CustomListener customListener;
        private Context context;
        private OnOptionsSelectListener optionsSelectListener;

        private String Str_Submit;//确定按钮文字
        private String Str_Cancel;//取消按钮文字
        private String Str_Title;//标题文字

        private int Color_Submit;//确定按钮颜色
        private int Color_Cancel;//取消按钮颜色
        private int Color_Title;//标题颜色

        private int Color_Background_Wheel;//滚轮背景颜色
        private int Color_Background_Title;//标题背景颜色

        private int Size_Submit_Cancel = 17;//确定取消按钮大小
        private int Size_Title = 18;//标题文字大小
        private int Size_Content = 18;//内容文字大小

        private boolean cancelable = true;//是否能取消
        private boolean linkage = true;//是否联动
        private boolean isCenterLabel = true;//是否只显示中间的label

        private int textColorOut; //分割线以外的文字颜色
        private int textColorCenter; //分割线之间的文字颜色
        private int dividerColor; //分割线的颜色
        private int backgroundId; //显示时的外部背景色颜色,默认是灰色
        public ViewGroup decorView ;//显示pickerview的根View,默认是activity的根view
        // 条目间距倍数 默认1.6
        private float lineSpacingMultiplier = 1.6F;
        private boolean isDialog;//是否是对话框模式

        private String label1;
        private String label2;
        private String label3;
        private String label4;

        private boolean cyclic1 = false;//是否循环,默认否
        private boolean cyclic2 = false;
        private boolean cyclic3 = false;

        private Typeface font;

        private int option1;//默认选中项
        private int option2;
        private int option3;

        private WheelView.DividerType dividerType;//分隔线类型
        private int total = 3; // 列表数量

        //Required
        public Builder(Context context, OnOptionsSelectListener listener) {
            this.context = context;
            this.optionsSelectListener = listener;
        }

        //Option

        public Builder setSubmitText(String Str_Cancel) {
            this.Str_Submit = Str_Cancel;
            return this;
        }

        public Builder setCancelText(String Str_Cancel) {
            this.Str_Cancel = Str_Cancel;
            return this;
        }

        public Builder setTitleText(String Str_Title) {
            this.Str_Title = Str_Title;
            return this;
        }

        public Builder isDialog(boolean isDialog) {
            this.isDialog = isDialog;
            return this;
        }

        public Builder setSubmitColor(int Color_Submit) {
            this.Color_Submit = Color_Submit;
            return this;
        }

        public Builder setCancelColor(int Color_Cancel) {
            this.Color_Cancel = Color_Cancel;
            return this;
        }

        /**
         * 显示时的外部背景色颜色,默认是灰色
         * @param backgroundId
         * @return
         */
        public Builder setBackgroundId(int backgroundId) {
            this.backgroundId = backgroundId;
            return this;
        }
        /**
         * 必须是viewgroup
         * 设置要将pickerview显示到的容器
         * @param decorView
         * @return
         */
        public Builder setDecorView(ViewGroup decorView) {
            this.decorView = decorView;
            return this;
        }


        public Builder setLayoutRes(int res, CustomListener listener) {
            this.layoutRes = res;
            this.customListener = listener;
            return this;
        }

        public Builder setBgColor(int Color_Background_Wheel) {
            this.Color_Background_Wheel = Color_Background_Wheel;
            return this;
        }

        public Builder setTitleBgColor(int Color_Background_Title) {
            this.Color_Background_Title = Color_Background_Title;
            return this;
        }

        public Builder setTitleColor(int Color_Title) {
            this.Color_Title = Color_Title;
            return this;
        }

        public Builder setSubCalSize(int Size_Submit_Cancel) {
            this.Size_Submit_Cancel = Size_Submit_Cancel;
            return this;
        }

        public Builder setTitleSize(int Size_Title) {
            this.Size_Title = Size_Title;
            return this;
        }

        public Builder setContentTextSize(int Size_Content) {
            this.Size_Content = Size_Content;
            return this;
        }


        public Builder setOutSideCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }


        public Builder setLabels(String label1, String label2, String label3) {
            this.label1 = label1;
            this.label2 = label2;
            this.label3 = label3;
            return this;
        }

        /**
         * 设置间距倍数,但是只能在1.2-2.0f之间
         *
         * @param lineSpacingMultiplier
         */
        public Builder setLineSpacingMultiplier(float lineSpacingMultiplier) {
            this.lineSpacingMultiplier = lineSpacingMultiplier;
            return this;
        }

        /**
         * 设置分割线的颜色
         *
         * @param dividerColor
         */
        public Builder setDividerColor(int dividerColor) {
            this.dividerColor = dividerColor;
            return this;
        }

        /**
         * 设置分割线的类型
         *
         * @param dividerType
         */
        public Builder setDividerType(WheelView.DividerType dividerType) {
            this.dividerType = dividerType;
            return this;
        }

        /**
         * 设置分割线之间的文字的颜色
         *
         * @param textColorCenter
         */
        public Builder setTextColorCenter(int textColorCenter) {
            this.textColorCenter = textColorCenter;
            return this;
        }

        /**
         * 设置分割线以外文字的颜色
         *
         * @param textColorOut
         */
        public Builder setTextColorOut(int textColorOut) {
            this.textColorOut = textColorOut;
            return this;
        }

        public Builder setTypeface(Typeface font) {
            this.font = font;
            return this;
        }

        public Builder setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
            this.cyclic1 = cyclic1;
            this.cyclic2 = cyclic2;
            this.cyclic3 = cyclic3;
            return this;
        }

        public Builder setSelectOptions(int option1) {
            this.option1 = option1;
            return this;
        }

        public Builder setSelectOptions(int option1, int option2) {
            this.option1 = option1;
            this.option2 = option2;
            return this;
        }

        public Builder setSelectOptions(int option1, int option2, int option3) {
            this.option1 = option1;
            this.option2 = option2;
            this.option3 = option3;
            return this;
        }

        public Builder isCenterLabel(boolean isCenterLabel) {
            this.isCenterLabel = isCenterLabel;
            return this;
        }

        public Builder setTotal(int total) {
            this.total = total;
            return this;
        }

        public BerNpickerView build() {
            return new BerNpickerView(this);
        }
    }

    /**
     *  点击时的接口
     */
    public interface OnOptionsSelectListener<T> {
        void onOptionsSelect(List<T> datalist, View v);
    }

}

修改了
(1)修改布局变成我的布局
(2)然后把创建WheelView给加扩展createWheel(optionsPicker, context, total);因为我不想每次都都写Builder这么多参数,我把这个pickerview当成中间成来弄,让子类继承它来做简单的扩展

3.重写WheelOptions

我们重写个WheelView,因为原本的WheelView是做固定3列的处理,我们需要做成个动态的。

public class BerWheel<T> {
    protected View view;
    protected LinearLayout llContent;
    protected List<WheelView> wvList = new ArrayList<>();
    protected List<List<T>> items = new ArrayList<>();

    protected OnItemSelectedListener wheelListener_option1;
    protected OnItemSelectedListener wheelListener_option2;

    //文字的颜色和分割线的颜色
    protected int textColorOut;
    protected int textColorCenter;
    protected int dividerColor;

    protected WheelView.DividerType dividerType;
    protected Context context;

    // 条目间距倍数
    protected float lineSpacingMultiplier = 1.6F;
    protected int total;// 列表数量

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public BerWheel(View view, Context context, int total) {
        super();
        this.view = view;
        this.context = context;
        this.total = total;
        llContent = (LinearLayout) view.findViewById(R.id.optionspicker);

        showWheelView();
    }

    /**
     * 设置展示规则 默认为平均分布展示
     * 可以定义子类来重写该类来制定展示的规则
     */
    protected void showWheelView(){
        for (int i = 0; i < total; i++) {
            WheelView wheelview = new WheelView(context);
            wheelview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            wvList.add(wheelview);
            llContent.addView(wheelview);
        }
    }


    //不联动情况下
    public void setNPicker(List<List<T>> items) {
        this.items = items;
        int count = wvList.size() > items.size() ? items.size() : wvList.size();
        for (int i = 0; i < count; i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setVisibility(View.VISIBLE);
                wvList.get(i).setAdapter(new ArrayWheelAdapter(items.get(i)));
                wvList.get(i).setCurrentItem(0);
                wvList.get(i).setIsOptions(true);
            }else {
                wvList.get(i).setVisibility(View.GONE);
            }
        }
    }


    public void setTextContentSize(int textSize) {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setTextSize(textSize);
            }
        }
    }

    private void setTextColorOut() {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setTextColorOut(textColorOut);
            }
        }
    }

    private void setTextColorCenter() {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setTextColorCenter(textColorCenter);
            }
        }
    }

    private void setDividerColor() {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setDividerColor(dividerColor);
            }
        }
    }

    private void setDividerType() {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setDividerType(dividerType);
            }
        }
    }

    private void setLineSpacingMultiplier() {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setLineSpacingMultiplier(lineSpacingMultiplier);
            }
        }
    }

    /**
     * 设置选项的单位
     */
    public void setLabels(String...label) {
        int count = label.length > wvList.size() ? wvList.size() : label.length;
        for (int i = 0; i < count; i++) {
            if (wvList.get(i) != null && label[i] != null) {
                wvList.get(i).setLabel(label[i]);
            }
        }
    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic 是否循环
     */
    public void setCyclic(boolean...cyclic) {
        int count = cyclic.length > wvList.size() ? wvList.size() : cyclic.length;
        for (int i = 0; i < count; i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setCyclic(cyclic[i]);
            }
        }
    }

    /**
     * 设置字体样式
     *
     * @param font 系统提供的几种样式
     */
    public void setTypeface (Typeface font) {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setTypeface(font);
            }
        }
    }

    /**
     * 分别设置第一二三级是否循环滚动
     */
//    public void setCyclic(boolean...cyclic) {
//        int count = cyclic.length > wvList.size() ? wvList.size() : cyclic.length;
//        for (int i = 0; i < count; i++) {
//            if (wvList.get(i) != null) {
//                wvList.get(i).setCyclic(cyclic[i]);
//            }
//        }
//    }



    public void setCurrentItems(int...option) {
        int count = option.length > wvList.size() ? wvList.size() : option.length;
        for (int i = 0; i < count; i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).setCurrentItem(option[i]);
            }
        }
    }


    /**
     * 设置间距倍数,但是只能在1.2-2.0f之间
     *
     * @param lineSpacingMultiplier
     */
    public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
        this.lineSpacingMultiplier = lineSpacingMultiplier;
        setLineSpacingMultiplier();
    }

    /**
     * 设置分割线的颜色
     *
     * @param dividerColor
     */
    public void setDividerColor(int dividerColor) {
        this.dividerColor = dividerColor;
        setDividerColor();
    }

    /**
     * 设置分割线的类型
     *
     * @param dividerType
     */
    public void setDividerType(WheelView.DividerType dividerType) {
        this.dividerType = dividerType;
        setDividerType();
    }
    /**
     * 设置分割线之间的文字的颜色
     *
     * @param textColorCenter
     */
    public void setTextColorCenter(int textColorCenter) {
        this.textColorCenter = textColorCenter;
        setTextColorCenter();
    }

    /**
     * 设置分割线以外文字的颜色
     *
     * @param textColorOut
     */
    public void setTextColorOut(int textColorOut) {
        this.textColorOut = textColorOut;
        setTextColorOut();
    }

    /**
     * Label 是否只显示中间选中项的
     *
     * @param isCenterLabel
     */

    public void isCenterLabel(Boolean isCenterLabel) {
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                wvList.get(i).isCenterLabel(isCenterLabel);
            }
        }
    }

    public List<T> getCurrentItems() {
        List<T> datalist = new ArrayList<>();
        for (int i = 0; i < wvList.size(); i++) {
            if (wvList.get(i) != null) {
                datalist.add(items.get(i).get(wvList.get(i).getCurrentItem()));
            }
        }
        return datalist;
    }

}

(1)我多添加了个参数total表示要展示多少列
(2)用List<WheelView> wvList数组来动态创建添加WheelView
(3)用List<List<T>> items 来装每一列的数据(我这个Wheel只做了不关联情况下的多列,关联情况下我没弄)

(4)showWheelView();

 protected void showWheelView(){
        for (int i = 0; i < total; i++) {
            WheelView wheelview = new WheelView(context);
            wheelview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            wvList.add(wheelview);
            llContent.addView(wheelview);
        }
    }

这个方法做展示的规则,默认是平均展示total列,而如果需要做特殊的展示情况,像我上边一样的,就写个类继承这个类重新这个方法重新展示的规则就行,比如我的时间期间选择器。

@Override
    protected void showWheelView() {
        for (int i = 0; i < total; i++) {
            WheelView wheelview = new WheelView(context);
            wheelview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            wvList.add(wheelview);
        }

        View view = new View(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,3,1);
        lp.gravity = Gravity.CENTER;
        view.setLayoutParams(lp);
        view.setPadding(10,0,10,0);
        view.setBackgroundResource(R.color.app_black);

        TextView textview1 = new TextView(context);
        textview1.setText(":");
        LinearLayout.LayoutParams tvlp1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvlp1.gravity = Gravity.CENTER;
        textview1.setLayoutParams(tvlp1);
        TextView textview2 = new TextView(context);
        textview2.setText(":");
        LinearLayout.LayoutParams tvlp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvlp2.gravity = Gravity.CENTER;
        textview2.setLayoutParams(tvlp2);


        llContent.addView(wvList.get(0));
        llContent.addView(textview1);
        llContent.addView(wvList.get(1));
        llContent.addView(view);
        llContent.addView(wvList.get(2));
        llContent.addView(textview2);
        llContent.addView(wvList.get(3));

    }

重写这个方法就能展示出自己需要展示的效果

Android仿ios条件选择器pickerview
image.png

调用时也很方便。

BerNpickerView testview = new BerTimeBpickerView.Builder(this, new BerNpickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(List datalist, View v) {

            }
        })
                .setTotal(4)
                .setTitleText("选择时间段")
                .build();
        testview.setNPicker(timelist);
        testview.show();

四.结束

我讲这篇的目的是为了第一介绍一下这个三方库,还是比较实用的。第二,说下扩展的重要性。第三,放假了实在工作效率低。

上一篇:能去码也能打码!OpenCV实时检测视频流人脸并马赛克之,视频后期福音


下一篇:SparkStreming:使用Checkpoint创建StreamingContext修改executor-cores、executor-memory等资源信息不生效。