From 9739c1d70b64aa283613c69f31afb74f17c70bbc Mon Sep 17 00:00:00 2001 From: Aleksandr Koscheev Date: Sun, 17 Feb 2019 04:19:25 -0500 Subject: [PATCH] master: added month name as String --- .../adapter/IMonthNameResolver.java | 5 +++++ .../adapter/NumericWheelAdapter.java | 11 +++++++++- .../pickerview/builder/TimePickerBuilder.java | 6 ++++++ .../pickerview/configure/PickerOptions.java | 3 +++ .../pickerview/view/TimePickerView.java | 3 ++- .../com/bigkoo/pickerview/view/WheelTime.java | 21 ++++++++++++------- 6 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 pickerview/src/main/java/com/bigkoo/pickerview/adapter/IMonthNameResolver.java diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/adapter/IMonthNameResolver.java b/pickerview/src/main/java/com/bigkoo/pickerview/adapter/IMonthNameResolver.java new file mode 100644 index 00000000..b011c2dc --- /dev/null +++ b/pickerview/src/main/java/com/bigkoo/pickerview/adapter/IMonthNameResolver.java @@ -0,0 +1,5 @@ +package com.bigkoo.pickerview.adapter; + +public interface IMonthNameResolver { + String resolveName(int index); +} diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java b/pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java index 99a9721a..8952a05c 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java @@ -11,6 +11,13 @@ public class NumericWheelAdapter implements WheelAdapter { private int minValue; private int maxValue; + public NumericWheelAdapter setMonthNameResolver(IMonthNameResolver resolver){ + mMonthNameResolver = resolver; + return this; + } + + private IMonthNameResolver mMonthNameResolver; + /** * Constructor * @param minValue the wheel min value @@ -25,6 +32,7 @@ public NumericWheelAdapter(int minValue, int maxValue) { public Object getItem(int index) { if (index >= 0 && index < getItemsCount()) { int value = minValue + index; + if (mMonthNameResolver != null) return mMonthNameResolver.resolveName(value); return value; } return 0; @@ -44,4 +52,5 @@ public int indexOf(Object o){ } } -} + +} \ No newline at end of file diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java index ebbad1e7..14c9da82 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java @@ -5,6 +5,7 @@ import android.view.View; import android.view.ViewGroup; +import com.bigkoo.pickerview.adapter.IMonthNameResolver; import com.bigkoo.pickerview.configure.PickerOptions; import com.bigkoo.pickerview.listener.CustomListener; import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener; @@ -288,6 +289,11 @@ public TimePickerBuilder setTimeSelectChangeListener(OnTimeSelectChangeListener return this; } + public TimePickerBuilder setMonthNameResolver(IMonthNameResolver resolver) { + mPickerOptions.mMonthNameResolver = resolver; + return this; + } + public TimePickerView build() { return new TimePickerView(mPickerOptions); } diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java index d3fccabd..359ad8a5 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java @@ -7,6 +7,7 @@ import android.view.ViewGroup; import com.bigkoo.pickerview.R; +import com.bigkoo.pickerview.adapter.IMonthNameResolver; import com.bigkoo.pickerview.listener.CustomListener; import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener; import com.bigkoo.pickerview.listener.OnOptionsSelectListener; @@ -111,4 +112,6 @@ public PickerOptions(int buildType) { public Typeface font = Typeface.MONOSPACE;//字体样式 public WheelView.DividerType dividerType = WheelView.DividerType.FILL;//分隔线类型 + public IMonthNameResolver mMonthNameResolver; + } diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java index 4742c6a9..f838b69b 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java @@ -83,7 +83,8 @@ private void initView(Context context) { } private void initWheelTime(LinearLayout timePickerView) { - wheelTime = new WheelTime(timePickerView, mPickerOptions.type, mPickerOptions.textGravity, mPickerOptions.textSizeContent); + wheelTime = new WheelTime(timePickerView, mPickerOptions.type, mPickerOptions.textGravity, mPickerOptions.textSizeContent) + .set(mPickerOptions.mMonthNameResolver); if (mPickerOptions.timeSelectChangeListener != null) { wheelTime.setSelectChangeCallback(new ISelectTimeCallback() { @Override diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index a41ee546..206012ca 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -4,6 +4,7 @@ import com.bigkoo.pickerview.R; import com.bigkoo.pickerview.adapter.ArrayWheelAdapter; +import com.bigkoo.pickerview.adapter.IMonthNameResolver; import com.bigkoo.pickerview.adapter.NumericWheelAdapter; import com.bigkoo.pickerview.listener.ISelectTimeCallback; import com.bigkoo.pickerview.utils.ChinaDate; @@ -56,6 +57,7 @@ public class WheelTime { private WheelView.DividerType dividerType; private boolean isLunarCalendar = false; private ISelectTimeCallback mSelectChangeCallback; + private IMonthNameResolver mMonthNameResolver; public WheelTime(View view, boolean[] type, int gravity, int textSize) { super(); @@ -65,6 +67,11 @@ public WheelTime(View view, boolean[] type, int gravity, int textSize) { this.textSize = textSize; } + public WheelTime set(IMonthNameResolver resolver){ + mMonthNameResolver = resolver; + return this; + } + public void setLunarMode(boolean isLunarCalendar) { this.isLunarCalendar = isLunarCalendar; } @@ -261,18 +268,18 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { // 月 wv_month = (WheelView) view.findViewById(R.id.month); if (startYear == endYear) {//开始年等于终止年 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth)); + wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth).setMonthNameResolver(mMonthNameResolver)); wv_month.setCurrentItem(month + 1 - startMonth); } else if (year == startYear) { //起始日期的月份控制 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); + wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12).setMonthNameResolver(mMonthNameResolver)); wv_month.setCurrentItem(month + 1 - startMonth); } else if (year == endYear) { //终止日期的月份控制 - wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); + wv_month.setAdapter(new NumericWheelAdapter(1, endMonth).setMonthNameResolver(mMonthNameResolver)); wv_month.setCurrentItem(month); } else { - wv_month.setAdapter(new NumericWheelAdapter(1, 12)); + wv_month.setAdapter(new NumericWheelAdapter(1, 12).setMonthNameResolver(mMonthNameResolver)); wv_month.setCurrentItem(month); } wv_month.setGravity(gravity); @@ -424,7 +431,7 @@ public void onItemSelected(int index) { } } else if (year_num == startYear) {//等于开始的年 //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); + wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12).setMonthNameResolver(mMonthNameResolver)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; @@ -442,7 +449,7 @@ public void onItemSelected(int index) { } else if (year_num == endYear) { //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); + wv_month.setAdapter(new NumericWheelAdapter(1, endMonth).setMonthNameResolver(mMonthNameResolver)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; wv_month.setCurrentItem(currentMonthItem); @@ -459,7 +466,7 @@ public void onItemSelected(int index) { } else { //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(1, 12)); + wv_month.setAdapter(new NumericWheelAdapter(1, 12).setMonthNameResolver(mMonthNameResolver)); //重新设置日 setReDay(year_num, wv_month.getCurrentItem() + 1, 1, 31, list_big, list_little); }