|
|
@@ -6,15 +6,18 @@ import android.support.annotation.Nullable;
|
|
|
import android.util.AttributeSet;
|
|
|
import android.util.Log;
|
|
|
import android.view.Gravity;
|
|
|
+import android.view.LayoutInflater;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.NumberPicker;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import com.usai.redant.rautils.R;
|
|
|
import com.usai.redant.rautils.utils.RAUtil;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
+import static com.usai.redant.rautils.dateTimePicker.DatePickerView.DatePickerViewLayoutMode.DATE_PICKER_VIEW_LAYOUT_MODE_COMPACT;
|
|
|
import static com.usai.redant.rautils.dateTimePicker.DatePickerView.DatePickerViewLayoutMode.DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR;
|
|
|
|
|
|
public class DatePickerView extends LinearLayout {
|
|
|
@@ -24,25 +27,32 @@ public class DatePickerView extends LinearLayout {
|
|
|
DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR
|
|
|
}
|
|
|
|
|
|
- DatePickerViewLayoutMode mMode = DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR;
|
|
|
+ private DatePickerViewLayoutMode mMode = DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR;
|
|
|
|
|
|
public static DatePickerView buildDatePickerView(Context context, DatePickerViewLayoutMode layoutMode) {
|
|
|
|
|
|
- DatePickerView datePickerView = new DatePickerView(context);
|
|
|
+// DatePickerView datePickerView = new DatePickerView(context);
|
|
|
+
|
|
|
+ int layoutId = R.layout.ra_date_picker_regular_view;
|
|
|
+ if (layoutMode == DATE_PICKER_VIEW_LAYOUT_MODE_COMPACT) {
|
|
|
+ layoutId = R.layout.ra_date_picker_compact_view;
|
|
|
+ }
|
|
|
+
|
|
|
+ DatePickerView datePickerView = (DatePickerView) LayoutInflater.from(context).inflate(layoutId,null);
|
|
|
datePickerView.mMode = layoutMode;
|
|
|
datePickerView.init();
|
|
|
return datePickerView;
|
|
|
}
|
|
|
|
|
|
- private DatePickerView(Context context) {
|
|
|
+ public DatePickerView(Context context) {
|
|
|
super(context);
|
|
|
}
|
|
|
|
|
|
- private DatePickerView(Context context, @Nullable AttributeSet attrs) {
|
|
|
+ public DatePickerView(Context context, @Nullable AttributeSet attrs) {
|
|
|
super(context, attrs);
|
|
|
}
|
|
|
|
|
|
- private DatePickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
+ public DatePickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
}
|
|
|
|
|
|
@@ -52,7 +62,7 @@ public class DatePickerView extends LinearLayout {
|
|
|
* month: 从0开始
|
|
|
* day: 从1开始
|
|
|
* */
|
|
|
- private Calendar mCalendar = Calendar.getInstance();
|
|
|
+ private Calendar mCalendar = Calendar.getInstance();
|
|
|
private boolean initialized = false;
|
|
|
private void init() {
|
|
|
if (!initialized) {
|
|
|
@@ -73,16 +83,53 @@ public class DatePickerView extends LinearLayout {
|
|
|
//
|
|
|
// }
|
|
|
|
|
|
+ /**
|
|
|
+ * View
|
|
|
+ * */
|
|
|
// year
|
|
|
- mYearPicker = new NumberPicker(getContext());
|
|
|
- int yearWidth = RAUtil.dp2px(getContext(), 60);
|
|
|
- int yearWeight = 1;
|
|
|
- if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
- yearWidth = 0;
|
|
|
- yearWeight = 2;
|
|
|
- }
|
|
|
+// mYearPicker = new NumberPicker(getContext());
|
|
|
+// int yearWidth = RAUtil.dp2px(getContext(), 60);
|
|
|
+// int yearWeight = 1;
|
|
|
+// if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
+// yearWidth = 0;
|
|
|
+// yearWeight = 2;
|
|
|
+// }
|
|
|
+// setupPicker(mYearPicker, 10000, yearWidth, yearWeight,"-");
|
|
|
+//
|
|
|
+//
|
|
|
+// // month
|
|
|
+// mMonthPicker = new NumberPicker(getContext());
|
|
|
+// int monthWidth = RAUtil.dp2px(getContext(), 30);
|
|
|
+// int monthWeight = 1;
|
|
|
+// if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
+// monthWidth = 0;
|
|
|
+// monthWeight = 1;
|
|
|
+// }
|
|
|
+// setupPicker(mMonthPicker, 12, monthWidth, monthWeight, "-");
|
|
|
+//
|
|
|
+// // day
|
|
|
+// mDayPicker = new NumberPicker(getContext());
|
|
|
+// int dayWidth = RAUtil.dp2px(getContext(), 30);
|
|
|
+// int dayWeight = 1;
|
|
|
+// if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
+// dayWidth = 0;
|
|
|
+// dayWeight = 1;
|
|
|
+// }
|
|
|
+// setupPicker(mDayPicker, mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH), dayWidth, dayWeight, "");
|
|
|
+
|
|
|
+ mYearPicker = findViewById(R.id.ra_date_year_picker);
|
|
|
+ initPicker(mYearPicker,3000);
|
|
|
+
|
|
|
+ mMonthPicker = findViewById(R.id.ra_date_month_picker);
|
|
|
+ initPicker(mMonthPicker, 12);
|
|
|
|
|
|
- setupPicker(mYearPicker, 10000, yearWidth, yearWeight,"-");
|
|
|
+ mDayPicker = findViewById(R.id.ra_date_day_picker);
|
|
|
+ initPicker(mDayPicker, mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * setup
|
|
|
+ * */
|
|
|
+ // setup
|
|
|
setYear(mCalendar.get(Calendar.YEAR));
|
|
|
mYearPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
|
|
|
@Override
|
|
|
@@ -91,34 +138,15 @@ public class DatePickerView extends LinearLayout {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- // month
|
|
|
- mMonthPicker = new NumberPicker(getContext());
|
|
|
- int monthWidth = RAUtil.dp2px(getContext(), 30);
|
|
|
- int monthWeight = 1;
|
|
|
- if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
- monthWidth = 0;
|
|
|
- monthWeight = 1;
|
|
|
- }
|
|
|
-
|
|
|
- setupPicker(mMonthPicker, 12, monthWidth, monthWeight, "-");
|
|
|
- setMonth(mCalendar.get(Calendar.MONTH) + 1);
|
|
|
mMonthPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
|
|
|
@Override
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
p_setMonth(newVal + 1);
|
|
|
}
|
|
|
});
|
|
|
+ setMonth(mCalendar.get(Calendar.MONTH) + 1);
|
|
|
|
|
|
- // day
|
|
|
- mDayPicker = new NumberPicker(getContext());
|
|
|
- int dayWidth = RAUtil.dp2px(getContext(), 30);
|
|
|
- int dayWeight = 1;
|
|
|
- if (mMode == DATE_PICKER_VIEW_LAYOUT_MODE_REGULAR) {
|
|
|
- dayWidth = 0;
|
|
|
- dayWeight = 1;
|
|
|
- }
|
|
|
|
|
|
- setupPicker(mDayPicker, mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH), dayWidth, dayWeight, "");
|
|
|
mDayPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
|
|
|
@Override
|
|
|
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
|
|
|
@@ -153,14 +181,21 @@ public class DatePickerView extends LinearLayout {
|
|
|
addView(unitTv, unitParams);
|
|
|
}
|
|
|
|
|
|
- private void initPicker(NumberPicker picker, int max) {
|
|
|
+ private static String[] setupRangeArray(int max) {
|
|
|
|
|
|
ArrayList<String> hourArr = new ArrayList<>();
|
|
|
for (int i = 0; i < max; i++) {
|
|
|
- hourArr.add(String.format("%d",i + 1));
|
|
|
+ hourArr.add(String.valueOf(i + 1));
|
|
|
}
|
|
|
String[] values = RAUtil.list2Array(hourArr);
|
|
|
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initPicker(NumberPicker picker, int max) {
|
|
|
+
|
|
|
+ String[] values = setupRangeArray(max);
|
|
|
+
|
|
|
int oldMax = picker.getMaxValue();
|
|
|
|
|
|
picker.setMinValue(0);
|