最近怎么老寫View,可能寫view比較方便,寫其它東西還要抽時(shí)間整理總結(jié),寫View就直接封完寫出來就行。
準(zhǔn)備國慶放假,無心工作,那就寫篇簡單實(shí)用一點(diǎn)的文章,總不能白白浪費(fèi)了時(shí)間。
一.需求
有時(shí)候ios端會(huì)用到條件選擇器,好像是那邊自帶的,而android這邊是沒有的,但是為了兩端統(tǒng)一,沒辦法,只能我們?nèi)ミw就他們了(你讓一個(gè)有自帶的去寫自定義是基本不可能的事)。
最經(jīng)典的是我們有選擇地址的需求,比如美團(tuán)這里的:

這個(gè)android是原生是沒有的,只有能選擇日期的。那怎么辦?自定義,好像略難,那就用三方的吧。
https://github.com/Bigkoo/Android-PickerView

我找了很多,就覺得這個(gè)庫是做得比較好,比較完整的,而且也一直有在維護(hù),還是比較推薦,使用起來也比較方便。項(xiàng)目里有很清晰的文檔,建議看之前先瀏覽過文檔。
二.效果展示
我使用的效果:

我還是順便把源碼也瀏覽了下。發(fā)現(xiàn)這里有3個(gè)比較重要的類,這個(gè)之后會(huì)簡單的介紹:
(1)WheelView
(2)條件選擇的WheelOptions,我感覺這個(gè)類的封裝有點(diǎn)vm的意思
(3)最外層封裝的OptionsPickerView
三.擴(kuò)展
如果只是為了選擇地址的話直接用它封裝好的就行,但是有時(shí)候可能會(huì)需要用到其它的布局或需求,那我們就要在它原有的功能上進(jìn)行擴(kuò)展,比如說我寫的這個(gè)時(shí)間段的現(xiàn)在,直接用是沒有的,需要自己擴(kuò)展。

而要進(jìn)行擴(kuò)展的話,就要先瀏覽源碼看看它內(nèi)部怎么寫的。
可以從調(diào)用的地方找到OptionsPickerView類
OptionsPickerView pvOptions1 = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
// todo 點(diǎn)擊確認(rèn)之后的操作
}
})
.setTitleText("選擇地區(qū)")
.build();
pvOptions1.setPicker(items1, items2,items3);//二級(jí)選擇器
pvOptions1.show();
然后看看OptionsPickerView類內(nèi)部,你會(huì)發(fā)現(xiàn)很多方法,但是基本都是builder方法個(gè)getset方法,我們可以找到重要的幾個(gè)方法。
private void initView(Context context) {
setDialogOutSideCancelable(cancelable);
initViews(backgroundId);
init();
initEvents();
if (customListener == null) {
LayoutInflater.from(context).inflate(layoutRes, contentContainer);
//頂部標(biāo)題
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);
//設(shè)置文字
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);//默認(rèn)為空
//設(shè)置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);
//設(shè)置文字大小
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設(shè)置屬性。重要的是這里
final LinearLayout optionsPicker = (LinearLayout) findViewById(R.id.optionspicker);
optionsPicker.setBackgroundColor(Color_Background_Wheel == 0 ? bgColor_default : Color_Background_Wheel);
wheelOptions = new WheelOptions(optionsPicker, linkage);
這里的意思就是把這個(gè)View給WheelOptions這個(gè)對(duì)象,讓它來做處理。然后可以看
看布局。
<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列。其實(shí)我不太贊成這樣的做法,對(duì)于這樣的多情況view的封裝,我個(gè)人還是比較喜歡做動(dòng)態(tài)的。由于這里固定是3列,所以我上圖中4列的情況直接使用是實(shí)現(xiàn)不了的,所以需要擴(kuò)展。這里的WheelView就是單列
1.重寫布局
它這里布局寫死了固定3列,那我肯定是沒法復(fù)用它的這個(gè)布局了,所以就只能重寫布局。
<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,就是要?jiǎng)討B(tài)去添加WheelView。
2.重寫OptionsPickerView
原本的OptionsPickerView中

在builder構(gòu)造時(shí)就固定了布局,所以我這不好擴(kuò)展,不如重寫一個(gè)OptionsPickerView,當(dāng)然重寫B(tài)uilder也行,但是我覺得重寫OptionsPickerView比較好。而且他原本只有兩個(gè)類

所以我們需要繼承BasePickerView重寫一個(gè)PickerView,他原本內(nèi)部的邏輯沒問題,我就抄過來用好了。
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;//標(biāo)題文字
private int Color_Submit;//確定按鈕顏色
private int Color_Cancel;//取消按鈕顏色
private int Color_Title;//標(biāo)題顏色
private int Color_Background_Wheel;//滾輪背景顏色
private int Color_Background_Title;//標(biāo)題背景顏色
private int Size_Submit_Cancel;//確定取消按鈕大小
private int Size_Title;//標(biāo)題文字大小
private int Size_Content;//內(nèi)容文字大小
private int textColorOut; //分割線以外的文字顏色
private int textColorCenter; //分割線之間的文字顏色
private int dividerColor; //分割線的顏色
private int backgroundId; //顯示時(shí)的外部背景色顏色,默認(rèn)是灰色
// 條目間距倍數(shù) 默認(rèn)1.6
private float lineSpacingMultiplier = 1.6F;
private boolean isDialog;//是否是對(duì)話框模式
private boolean cancelable;//是否能取消
private boolean linkage;//是否聯(lián)動(dòng)
private boolean isCenterLabel ;//是否只顯示中間的label
private String label1;//單位
private String label2;
private String label3;
private String label4;
private boolean cyclic1;//是否循環(huán)
private boolean cyclic2;
private boolean cyclic3;
private Typeface font;//字體樣式
private int option1;//默認(rèn)選中項(xiàng)
private int option2;
private int option3;
private WheelView.DividerType dividerType;//分隔線類型
private int total; // 列表數(shù)量
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();
}
//抽離接口回調(diào)的方法
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);
//頂部標(biāo)題
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);
//設(shè)置文字
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);//默認(rèn)為空
//設(shè)置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);
//設(shè)置文字大小
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);
}
/**
* 創(chuàng)建子類BerWheel
*/
protected void createWheel(View view, Context context, int total){
berWheel = new BerWheel(view, context, total);
}
//不聯(lián)動(dòng)情況下調(diào)用
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;//標(biāo)題文字
private int Color_Submit;//確定按鈕顏色
private int Color_Cancel;//取消按鈕顏色
private int Color_Title;//標(biāo)題顏色
private int Color_Background_Wheel;//滾輪背景顏色
private int Color_Background_Title;//標(biāo)題背景顏色
private int Size_Submit_Cancel = 17;//確定取消按鈕大小
private int Size_Title = 18;//標(biāo)題文字大小
private int Size_Content = 18;//內(nèi)容文字大小
private boolean cancelable = true;//是否能取消
private boolean linkage = true;//是否聯(lián)動(dòng)
private boolean isCenterLabel = true;//是否只顯示中間的label
private int textColorOut; //分割線以外的文字顏色
private int textColorCenter; //分割線之間的文字顏色
private int dividerColor; //分割線的顏色
private int backgroundId; //顯示時(shí)的外部背景色顏色,默認(rèn)是灰色
public ViewGroup decorView ;//顯示pickerview的根View,默認(rèn)是activity的根view
// 條目間距倍數(shù) 默認(rèn)1.6
private float lineSpacingMultiplier = 1.6F;
private boolean isDialog;//是否是對(duì)話框模式
private String label1;
private String label2;
private String label3;
private String label4;
private boolean cyclic1 = false;//是否循環(huán),默認(rèn)否
private boolean cyclic2 = false;
private boolean cyclic3 = false;
private Typeface font;
private int option1;//默認(rèn)選中項(xiàng)
private int option2;
private int option3;
private WheelView.DividerType dividerType;//分隔線類型
private int total = 3; // 列表數(shù)量
//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;
}
/**
* 顯示時(shí)的外部背景色顏色,默認(rèn)是灰色
* @param backgroundId
* @return
*/
public Builder setBackgroundId(int backgroundId) {
this.backgroundId = backgroundId;
return this;
}
/**
* 必須是viewgroup
* 設(shè)置要將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;
}
/**
* 設(shè)置間距倍數(shù),但是只能在1.2-2.0f之間
*
* @param lineSpacingMultiplier
*/
public Builder setLineSpacingMultiplier(float lineSpacingMultiplier) {
this.lineSpacingMultiplier = lineSpacingMultiplier;
return this;
}
/**
* 設(shè)置分割線的顏色
*
* @param dividerColor
*/
public Builder setDividerColor(int dividerColor) {
this.dividerColor = dividerColor;
return this;
}
/**
* 設(shè)置分割線的類型
*
* @param dividerType
*/
public Builder setDividerType(WheelView.DividerType dividerType) {
this.dividerType = dividerType;
return this;
}
/**
* 設(shè)置分割線之間的文字的顏色
*
* @param textColorCenter
*/
public Builder setTextColorCenter(int textColorCenter) {
this.textColorCenter = textColorCenter;
return this;
}
/**
* 設(shè)置分割線以外文字的顏色
*
* @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);
}
}
/**
* 點(diǎn)擊時(shí)的接口
*/
public interface OnOptionsSelectListener<T> {
void onOptionsSelect(List<T> datalist, View v);
}
}
修改了
(1)修改布局變成我的布局
(2)然后把創(chuàng)建WheelView給加擴(kuò)展createWheel(optionsPicker, context, total);因?yàn)槲也幌朊看味级紝態(tài)uilder這么多參數(shù),我把這個(gè)pickerview當(dāng)成中間成來弄,讓子類繼承它來做簡單的擴(kuò)展
3.重寫WheelOptions
我們重寫個(gè)WheelView,因?yàn)樵镜腤heelView是做固定3列的處理,我們需要做成個(gè)動(dòng)態(tài)的。
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;
// 條目間距倍數(shù)
protected float lineSpacingMultiplier = 1.6F;
protected int total;// 列表數(shù)量
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();
}
/**
* 設(shè)置展示規(guī)則 默認(rèn)為平均分布展示
* 可以定義子類來重寫該類來制定展示的規(guī)則
*/
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);
}
}
//不聯(lián)動(dòng)情況下
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);
}
}
}
/**
* 設(shè)置選項(xiàng)的單位
*/
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]);
}
}
}
/**
* 設(shè)置是否循環(huán)滾動(dòng)
*
* @param cyclic 是否循環(huán)
*/
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]);
}
}
}
/**
* 設(shè)置字體樣式
*
* @param font 系統(tǒng)提供的幾種樣式
*/
public void setTypeface (Typeface font) {
for (int i = 0; i < wvList.size(); i++) {
if (wvList.get(i) != null) {
wvList.get(i).setTypeface(font);
}
}
}
/**
* 分別設(shè)置第一二三級(jí)是否循環(huán)滾動(dòng)
*/
// 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]);
}
}
}
/**
* 設(shè)置間距倍數(shù),但是只能在1.2-2.0f之間
*
* @param lineSpacingMultiplier
*/
public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
this.lineSpacingMultiplier = lineSpacingMultiplier;
setLineSpacingMultiplier();
}
/**
* 設(shè)置分割線的顏色
*
* @param dividerColor
*/
public void setDividerColor(int dividerColor) {
this.dividerColor = dividerColor;
setDividerColor();
}
/**
* 設(shè)置分割線的類型
*
* @param dividerType
*/
public void setDividerType(WheelView.DividerType dividerType) {
this.dividerType = dividerType;
setDividerType();
}
/**
* 設(shè)置分割線之間的文字的顏色
*
* @param textColorCenter
*/
public void setTextColorCenter(int textColorCenter) {
this.textColorCenter = textColorCenter;
setTextColorCenter();
}
/**
* 設(shè)置分割線以外文字的顏色
*
* @param textColorOut
*/
public void setTextColorOut(int textColorOut) {
this.textColorOut = textColorOut;
setTextColorOut();
}
/**
* Label 是否只顯示中間選中項(xiàng)的
*
* @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)我多添加了個(gè)參數(shù)total表示要展示多少列
(2)用List<WheelView> wvList數(shù)組來動(dòng)態(tài)創(chuàng)建添加WheelView
(3)用List<List<T>> items 來裝每一列的數(shù)據(jù)(我這個(gè)Wheel只做了不關(guān)聯(lián)情況下的多列,關(guān)聯(lián)情況下我沒弄)
(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);
}
}
這個(gè)方法做展示的規(guī)則,默認(rèn)是平均展示total列,而如果需要做特殊的展示情況,像我上邊一樣的,就寫個(gè)類繼承這個(gè)類重新這個(gè)方法重新展示的規(guī)則就行,比如我的時(shí)間期間選擇器。
@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));
}
重寫這個(gè)方法就能展示出自己需要展示的效果

調(diào)用時(shí)也很方便。
BerNpickerView testview = new BerTimeBpickerView.Builder(this, new BerNpickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(List datalist, View v) {
}
})
.setTotal(4)
.setTitleText("選擇時(shí)間段")
.build();
testview.setNPicker(timelist);
testview.show();
四.結(jié)束
我講這篇的目的是為了第一介紹一下這個(gè)三方庫,還是比較實(shí)用的。第二,說下擴(kuò)展的重要性。第三,放假了實(shí)在工作效率低。