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

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

一.需求

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

最经典的是我们有选择地址的需求,比如美团这里的:

image.png

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

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这个对象,让它来做处理。然后可以看

看布局。

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

layout="@layout/include_pickerview_topbar"

android:layout_width="match_parent"

android:layout_height="@dimen/pickerview_topbar_height" />

android:id="@+id/optionspicker"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:color/white"

android:orientation="horizontal">

android:id="@+id/options1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:id="@+id/options2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" />

android:id="@+id/options3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1" />

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

1.重写布局

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

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

layout="@layout/include_pickerview_topbar"

android:layout_width="match_parent"

android:layout_height="@dimen/pickerview_topbar_height" />

android:id="@+id/optionspicker"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:color/white"

android:orientation="horizontal">

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

2.重写OptionsPickerView

原本的OptionsPickerView中

image.png

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

image.png

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

public class BerNpickerView extends BasePickerView implements View.OnClickListener {

protected BerWheel 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 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> 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 {

void onOptionsSelect(List datalist, View v);

}

}

修改了

(1)修改布局变成我的布局

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

3.重写WheelOptions

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

public class BerWheel {

protected View view;

protected LinearLayout llContent;

protected List wvList = new ArrayList<>();

protected List> 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> 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 getCurrentItems() {

List 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 wvList数组来动态创建添加WheelView

(3)用List> 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();

四.结束

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

android pickerview 多行,Android仿ios条件选择器pickerview相关推荐

  1. Android设置透明状态栏,仿ios状态栏

    为什么80%的码农都做不了架构师?>>>    Android设置透明状态栏,仿ios状态栏 设置透明状态栏后,效果如下: 我的实现思路是: 在根布局上添加一块布局 添加了一块线性布 ...

  2. Android 文件夹放大缩小仿IOS融合动画效果

    效果如下,打开文件夹时从小到大与最终位置大小吻合,关闭文件夹时缩小到图标原有位置做到无缝融合效果. 过程分析 (一)文件展开后的布局 如上图文件夹打开后布局层次 1.最下面时背景层(背景不移动但有透明 ...

  3. android tablelayout 多行,android – 自定义TableLayout,行中有多个TextView

    我想用这样的行来创建自定义TableLayout: 电视用于TextView,即我想在行中添加11个TextView: 每行以标题开头,然后我添加5对TextView,这样表格行就像屏幕一样宽. 这是 ...

  4. android ellipsize 多行,android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法...

    android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法 布局文件中的TextView属性 复制代码 代码如下: android:id="@+id/b ...

  5. android tablelayout 多行,Android入门户五篇之TableLayout (二)//生成10行,8列的表格

    当前位置:我的异常网» Android » Android入门户五篇之TableLayout (二)//生成10行,8 Android入门户五篇之TableLayout (二)//生成10行,8列的表 ...

  6. android 高仿ios时间选择器,仿ios时间选择

    再mui得picker的基础上修改为类似ios选择时间的插件. muipicker exapmple地址 把里面数据换成下面的数据就可以了. (function($, doc) { $.init(); ...

  7. android iperf 命令行,Android 移植之 iperf

    1 .移植准备 1 ,源代码地址: 2 ,解压到 Adroid_root/external/ iperf-2.0.4.tar.gz; tar –zxvf iperf-2.0.4.tar.gz 2. 创 ...

  8. android 通知多行,Android多行通知,例如Gmail应用

    我正在尝试创建一个多行通知,如Gmail应用程序所示,如下图所示(5个通知分组在一个通知下) 我尝试了各种示例,但似乎只能创建单个通知 public void createSingleNotifica ...

  9. JavaScript 仿ios滑动选择器

    从git上找了一个,不过不是我想要的,更改了许多.到了我想要的效果: roller_selector_0_9.js 首先上js: "use strict";/** Author: ...

  10. android 仿ios三级联动,仿iOS的PickerView控件,有时间选择和选项选择并支持一二三级联动效果...

    Android-PickerView 注意事项.详请使用方式.更新日志等,请查看 Wiki文档 Wiki文档,Wiki文档,Wiki文档 !~ 重要的事情说三遍 对于使用上有任何疑问或优化建议等,欢迎 ...

最新文章

  1. mysql创建新用户方法_Mysql创建新用户方法
  2. Matlab之switch-case语句
  3. 内存管理vma_(十三)Linux内存管理之vma/malloc/mmap
  4. 不为人知的网络编程(七):如何让不可靠的UDP变的可靠?
  5. win7服务器远程灰色的,小编为你细说win7系统远程协助复选框是灰色的详细技巧...
  6. Linux下C语言实现LCD屏幕截图
  7. InstanceBeginEditable dw中特有标识
  8. 深度学习神经网络的预测间隔
  9. 原型与继承学习笔记3
  10. oracle数据库查询空间大小,Oracle查看数据库空间使用情况
  11. python如何看字符串长度_Python如何查找字符串的长度?(代码示例)
  12. 网管“北向接口”与“南向接口”
  13. 伦敦 quant_伦敦统一用户组(LUUG)见面v1.0
  14. warning: TCG doesn‘t support requested feature: CPUID.01H:ECX.vmx [bit 5]Could not initialize SDL
  15. Hamming Distance (汉明距离)
  16. Web前端开发技术:Web前端开发技术
  17. 足不出户买遍全球:亚马逊海外购启动史上最长“海外购物节”
  18. 【神州网信】【win10】相机等权限的开启方法
  19. vue中this.$confirm,确定和取消执行不同的逻辑处理
  20. redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refu

热门文章

  1. 利用kali下的msfvenom入侵电脑
  2. XenCenter导出虚拟机
  3. 评价法(四):yaahp软件——层次分析法模块使用
  4. java程序与数据库连接_将Java程序与数据库进行连接的操作方法
  5. ibmx系列服务器装系统,IBM X系列服务器 系统安装指南.pdf
  6. Python实现音乐推荐系统【跟着迪哥学python】
  7. 利用Jwing窗口写程序-----简单计算器(JAVA实用教程2-第五版 第九章 编程题 三(2)小题)
  8. 打开 Freetextbox 的 InsertImageFromGallery 及如何接收 ftb.imagegallery.aspx 返回的图片信息...
  9. VMware Workstation 14 Pro 安装 Windows Server 2003(完)
  10. Android MediaCodec学习笔记