Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

master: added month name as String #718

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bigkoo.pickerview.adapter;

public interface IMonthNameResolver {
String resolveName(int index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -44,4 +52,5 @@ public int indexOf(Object o){
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,4 +112,6 @@ public PickerOptions(int buildType) {
public Typeface font = Typeface.MONOSPACE;//字体样式
public WheelView.DividerType dividerType = WheelView.DividerType.FILL;//分隔线类型

public IMonthNameResolver mMonthNameResolver;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down