Android仿ios条件选择器pickerview

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

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

一.需求

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

image.png

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

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

image.png

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

二.效果展示

我使用的效果:

image.png

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

三.扩展

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

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中


image.png

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

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));

    }

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

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();

四.结束

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容