Android仿ios条件选择器pickerview

简介: 最近怎么老写View,可能写view比较方便,写其它东西还要抽时间整理总结,写View就直接封完写出来就行。准备国庆放假,无心工作,那就写篇简单实用一点的文章,总不能白白浪费了时间。

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

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

一.需求

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

img_59bede2e01ee930d8e11fb86a95e608b.png
image.png

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

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

img_e75cff4b59d53ff9e83770a43147fe14.png
image.png

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

二.效果展示

我使用的效果:

img_02e8c8c7f6641a1f3412fb797da89038.png
image.png

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

三.扩展

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

img_fdf90dd6d40018b9360fda14b213fb69.png
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中


img_2428b0fe09e54293006c3d897a004299.png
image.png

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

img_89fc2e535d5707691008ce884ad23f67.png
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));

    }

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

img_fdf90dd6d40018b9360fda14b213fb69.png
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();

四.结束

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

目录
相关文章
|
1月前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
22 1
|
2月前
|
物联网 vr&ar Android开发
探索安卓与iOS操作系统的未来发展趋势
【2月更文挑战第9天】本文将深入探讨安卓与iOS操作系统的未来发展趋势。通过分析当前技术发展和市场趋势,我们将探讨移动操作系统在人工智能、虚拟现实、物联网等领域的应用前景,以及如何满足用户需求并提升用户体验。同时,我们还将着重讨论两大操作系统在隐私保护、系统优化和生态建设方面的不断改进。
28 4
|
2月前
|
搜索推荐 Android开发 iOS开发
探析安卓与iOS系统的优劣
【2月更文挑战第7天】安卓与iOS是当今手机市场上最主流的两款操作系统,各有优劣。本文将从用户体验、开放程度、生态系统等方面对两者进行深入探析,以期帮助读者更好地了解它们的特点。
|
28天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
29天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS操作系统的发展与比较
在移动互联网时代,安卓和iOS两大操作系统在智能手机市场竞争激烈。本文将从技术架构、生态系统、用户体验等方面对安卓和iOS进行比较分析,探讨它们各自的特点和发展趋势。
|
1月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
58 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
1月前
|
人工智能 算法 Android开发
探索未来:Android与iOS在人工智能时代的融合与创新
【2月更文挑战第13天】 在数字化时代的快速发展下,Android与iOS作为两大主流移动操作系统,它们在人工智能(AI)领域的融合与创新已成为推动科技进步的关键力量。本文将从操作系统的核心功能拓展、AI技术的集成应用,以及开发者生态系统的演变三个维度,深入探讨Android和iOS如何在AI时代实现协同发展,以及这一进程对用户体验、应用开发和行业趋势产生的深远影响。通过对比分析和案例研究,我们旨在揭示两大平台在AI驱动下的创新路径,及其对未来科技格局的塑造作用。
|
1月前
|
人工智能 自然语言处理 语音技术
探索未来:安卓与iOS在人工智能领域的竞争与合作
【2月更文挑战第12天】本文深入探讨了安卓和iOS两大操作系统在人工智能(AI)领域的发展现状、竞争态势及未来合作可能性。通过对比分析两系统在AI技术集成、开发者支持、用户体验优化等方面的表现,揭示了它们各自的优势与挑战。文章最终展望了一个既有竞争又充满合作的未来,认为安卓和iOS的共同进步将推动整个人工智能技术向前发展,为用户带来更加智能、便捷的生活体验。
|
1月前
|
搜索推荐 Android开发 iOS开发
探索未来:安卓与iOS双系统的融合与创新
【2月更文挑战第12天】 在数字化时代,智能手机操作系统的发展不仅代表了技术的进步,更是用户体验革新的前沿。本文深入探讨了安卓和iOS这两大主流操作系统的未来走向,特别是它们在技术融合与创新方面的可能性。通过分析当前的市场需求、技术挑战和潜在的发展机会,我们将展望一个可能出现的未来场景:一个结合了安卓开放性和iOS优雅体验的双系统融合平台。这不仅仅是对技术极限的挑战,更是对用户体验极致追求的一次探索。
37 2
|
1月前
|
人工智能 搜索推荐 Android开发
探索未来:安卓与iOS在人工智能时代的融合趋势
【2月更文挑战第12天】 在这篇探索性文章中,我们将深入分析安卓和iOS两大移动操作系统在人工智能(AI)时代的融合趋势。随着技术的飞速发展,AI已成为推动智能手机进化的关键力量。本文通过对安卓和iOS各自在AI领域的最新进展进行比较,揭示了两大平台如何在保持各自特色的同时,也在向着更加智能、更加个性化的方向发展。我们不仅聚焦于当前的技术现状,而且还将展望未来,探讨这一趋势对用户体验、应用开发以及整个科技生态的深远影响。