@@ -82,6 +82,7 @@ public class DateTimePicker extends WheelPicker {
8282 private int endHour , endMinute = 59 ;
8383 private int textSize = WheelView .TEXT_SIZE ;
8484 private boolean useWeight = false ;
85+ private boolean resetWhileWheel = false ;
8586
8687 @ IntDef (value = {NONE , YEAR_MONTH_DAY , YEAR_MONTH , MONTH_DAY })
8788 @ Retention (RetentionPolicy .SOURCE )
@@ -132,6 +133,13 @@ public void setUseWeight(boolean useWeight) {
132133 this .useWeight = useWeight ;
133134 }
134135
136+ /**
137+ * 滚动时是否重置下一级的索引
138+ */
139+ public void setResetWhileWheel (boolean resetWhileWheel ) {
140+ this .resetWhileWheel = resetWhileWheel ;
141+ }
142+
135143 /**
136144 * 设置年份范围
137145 *
@@ -441,8 +449,10 @@ public void onSelected(int index) {
441449 onWheelListener .onYearWheeled (selectedYearIndex , selectedYearStr );
442450 }
443451 LogUtils .verbose (this , "change months after year wheeled" );
444- selectedMonthIndex = 0 ;//重置月份索引
445- selectedDayIndex = 0 ;//重置日子索引
452+ if (resetWhileWheel ) {
453+ selectedMonthIndex = 0 ;//重置月份索引
454+ selectedDayIndex = 0 ;//重置日子索引
455+ }
446456 //需要根据年份及月份动态计算天数
447457 int selectedYear = DateUtils .trimZero (selectedYearStr );
448458 changeMonthData (selectedYear );
@@ -480,7 +490,9 @@ public void onSelected(int index) {
480490 }
481491 if (dateMode == YEAR_MONTH_DAY || dateMode == MONTH_DAY ) {
482492 LogUtils .verbose (this , "change days after month wheeled" );
483- selectedDayIndex = 0 ;//重置日子索引
493+ if (resetWhileWheel ) {
494+ selectedDayIndex = 0 ;//重置日子索引
495+ }
484496 int selectedYear ;
485497 if (dateMode == YEAR_MONTH_DAY ) {
486498 selectedYear = DateUtils .trimZero (getSelectedYear ());
@@ -633,25 +645,29 @@ private void initYearData() {
633645 years .add (String .valueOf (i ));
634646 }
635647 }
636- // if (dateMode == YEAR_MONTH_DAY || dateMode == YEAR_MONTH) {
637- // int index = years.indexOf(DateUtils.fillZero(Calendar.getInstance().get(Calendar.YEAR)));
638- // if (index == -1) {
639- // //当前设置的年份不在指定范围,则默认选中范围开始的年
640- // selectedYearIndex = 0;
641- // } else {
642- // selectedYearIndex = index;
643- // }
644- // }
648+ if (!resetWhileWheel ) {
649+ if (dateMode == YEAR_MONTH_DAY || dateMode == YEAR_MONTH ) {
650+ int index = years .indexOf (DateUtils .fillZero (Calendar .getInstance ().get (Calendar .YEAR )));
651+ if (index == -1 ) {
652+ //当前设置的年份不在指定范围,则默认选中范围开始的年
653+ selectedYearIndex = 0 ;
654+ } else {
655+ selectedYearIndex = index ;
656+ }
657+ }
658+ }
645659 }
646660
647661 private void changeMonthData (int selectedYear ) {
648- // String preSelectMonth;
649- // if (months.size() > selectedMonthIndex) {
650- // preSelectMonth = months.get(selectedMonthIndex);
651- // } else {
652- // preSelectMonth = DateUtils.fillZero(Calendar.getInstance().get(Calendar.MONTH) + 1);
653- // }
654- // LogUtils.verbose(this, "preSelectMonth=" + preSelectMonth);
662+ String preSelectMonth = "" ;
663+ if (!resetWhileWheel ) {
664+ if (months .size () > selectedMonthIndex ) {
665+ preSelectMonth = months .get (selectedMonthIndex );
666+ } else {
667+ preSelectMonth = DateUtils .fillZero (Calendar .getInstance ().get (Calendar .MONTH ) + 1 );
668+ }
669+ LogUtils .verbose (this , "preSelectMonth=" + preSelectMonth );
670+ }
655671 months .clear ();
656672 if (startMonth < 1 || endMonth < 1 || startMonth > 12 || endMonth > 12 ) {
657673 throw new IllegalArgumentException ("Month out of range [1-12]" );
@@ -679,25 +695,29 @@ private void changeMonthData(int selectedYear) {
679695 months .add (DateUtils .fillZero (i ));
680696 }
681697 }
682- // //当前设置的月份不在指定范围,则默认选中范围开始的月份
683- // int preSelectMonthIndex = months.indexOf(preSelectMonth);
684- // selectedMonthIndex = preSelectMonthIndex == -1 ? 0 : preSelectMonthIndex;
698+ if (!resetWhileWheel ) {
699+ //当前设置的月份不在指定范围,则默认选中范围开始的月份
700+ int preSelectMonthIndex = months .indexOf (preSelectMonth );
701+ selectedMonthIndex = preSelectMonthIndex == -1 ? 0 : preSelectMonthIndex ;
702+ }
685703 }
686704
687705 private void changeDayData (int selectedYear , int selectedMonth ) {
688706 int maxDays = DateUtils .calculateDaysInMonth (selectedYear , selectedMonth );
689- // if (selectedDayIndex >= maxDays) {
690- // //如果之前选择的日是之前年月的最大日,则日自动为该年月的最大日
691- // selectedDayIndex = maxDays - 1;
692- // }
693- // String preSelectDay;
694- // if (days.size() > selectedDayIndex) {
695- // //年或月变动时,保持之前选择的日不动
696- // preSelectDay = days.get(selectedDayIndex);
697- // } else {
698- // preSelectDay = DateUtils.fillZero(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
699- // }
700- // LogUtils.verbose(this, "maxDays=" + maxDays + ", preSelectDay=" + preSelectDay);
707+ String preSelectDay = "" ;
708+ if (!resetWhileWheel ) {
709+ if (selectedDayIndex >= maxDays ) {
710+ //如果之前选择的日是之前年月的最大日,则日自动为该年月的最大日
711+ selectedDayIndex = maxDays - 1 ;
712+ }
713+ if (days .size () > selectedDayIndex ) {
714+ //年或月变动时,保持之前选择的日不动
715+ preSelectDay = days .get (selectedDayIndex );
716+ } else {
717+ preSelectDay = DateUtils .fillZero (Calendar .getInstance ().get (Calendar .DAY_OF_MONTH ));
718+ }
719+ LogUtils .verbose (this , "maxDays=" + maxDays + ", preSelectDay=" + preSelectDay );
720+ }
701721 days .clear ();
702722 if (selectedYear == startYear && selectedMonth == startMonth
703723 && selectedYear == endYear && selectedMonth == endMonth ) {
@@ -720,30 +740,38 @@ private void changeDayData(int selectedYear, int selectedMonth) {
720740 days .add (DateUtils .fillZero (i ));
721741 }
722742 }
723- // //当前设置的日子不在指定范围,则默认选中范围开始的日子
724- // int preSelectDayIndex = days.indexOf(preSelectDay);
725- // selectedDayIndex = preSelectDayIndex == -1 ? 0 : preSelectDayIndex;
743+ if (!resetWhileWheel ) {
744+ //当前设置的日子不在指定范围,则默认选中范围开始的日子
745+ int preSelectDayIndex = days .indexOf (preSelectDay );
746+ selectedDayIndex = preSelectDayIndex == -1 ? 0 : preSelectDayIndex ;
747+ }
726748 }
727749
728750 private void initHourData () {
729- // int currentHour;
730- // if (timeMode == HOUR_24) {
731- // currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
732- // } else {
733- // currentHour = Calendar.getInstance().get(Calendar.HOUR);
734- // }
751+ int currentHour = 0 ;
752+ if (!resetWhileWheel ) {
753+ if (timeMode == HOUR_24 ) {
754+ currentHour = Calendar .getInstance ().get (Calendar .HOUR_OF_DAY );
755+ } else {
756+ currentHour = Calendar .getInstance ().get (Calendar .HOUR );
757+ }
758+ }
735759 for (int i = startHour ; i <= endHour ; i ++) {
736760 String hour = DateUtils .fillZero (i );
737- // if (i == currentHour) {
738- // selectedHour = hour;
739- // }
761+ if (!resetWhileWheel ) {
762+ if (i == currentHour ) {
763+ selectedHour = hour ;
764+ }
765+ }
740766 hours .add (hour );
741767 }
742768 if (hours .indexOf (selectedHour ) == -1 ) {
743769 //当前设置的小时不在指定范围,则默认选中范围开始的小时
744770 selectedHour = hours .get (0 );
745771 }
746- // selectedMinute = DateUtils.fillZero(Calendar.getInstance().get(Calendar.MINUTE));
772+ if (!resetWhileWheel ) {
773+ selectedMinute = DateUtils .fillZero (Calendar .getInstance ().get (Calendar .MINUTE ));
774+ }
747775 }
748776
749777 private void changeMinuteData (int selectedHour ) {
0 commit comments