Skip to content

Commit

Permalink
修复默认选中第一个日期的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Mar 31, 2020
1 parent be7fd69 commit ac30a5c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate, DateChangeBehavior dateChangeBehavior) {
tv_result.setText(year + "年" + month + "月" + " 当前页面选中 " + localDate);
Log.d(TAG, " 当前页面选中 " + localDate);
Log.d(TAG, " dateChangeBehavior " + dateChangeBehavior);

if (localDate != null) {
CalendarDate calendarDate = CalendarUtil.getCalendarDate(localDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
miui10Calendar = findViewById(R.id.miui10Calendar);
miui10Calendar.setMonthStretchEnable(true);


List<String> pointList = Arrays.asList("2019-07-01", "2019-07-19", "2019-07-25", "2019-05-23", "2019-01-01", "2018-12-23");

InnerPainter innerPainter = (InnerPainter) miui10Calendar.getCalendarPainter();
Expand Down
41 changes: 18 additions & 23 deletions ncalendar/src/main/java/com/necer/calendar/BaseCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,23 @@ private void drawView(int position) {


public void onClickCurrentMonthOrWeekDate(LocalDate localDate) {
mDateChangeBehavior = DateChangeBehavior.CLICK;
jump(localDate, true);
jump(localDate, true, DateChangeBehavior.CLICK);
}

public void onClickLastMonthDate(LocalDate localDate) {
if (mLastNextMonthClickEnable && mScrollEnable) {
mDateChangeBehavior = DateChangeBehavior.CLICK_PAGE;
jump(localDate, true);
jump(localDate, true, DateChangeBehavior.CLICK_PAGE);
}
}

public void onClickNextMonthDate(LocalDate localDate) {
if (mLastNextMonthClickEnable && mScrollEnable) {
mDateChangeBehavior = DateChangeBehavior.CLICK_PAGE;
jump(localDate, true);
jump(localDate, true, DateChangeBehavior.CLICK_PAGE);
}
}

protected void jump(LocalDate localDate, boolean isCheck) {
protected void jump(LocalDate localDate, boolean isCheck, DateChangeBehavior dateChangeBehavior) {
this.mDateChangeBehavior = dateChangeBehavior;
//判断日期是否合法
if (!isAvailable(localDate)) {
if (getVisibility() == VISIBLE) {
Expand Down Expand Up @@ -318,6 +316,10 @@ private void callBack() {
}


// public void setDateChangeBehavior(DateChangeBehavior dateChangeBehavior) {
// this.mDateChangeBehavior = dateChangeBehavior;
// }

@Override
public void notifyCalendar() {
for (int i = 0; i < getChildCount(); i++) {
Expand Down Expand Up @@ -360,47 +362,43 @@ public void toLastPager() {

@Override
public void toToday() {
mDateChangeBehavior = DateChangeBehavior.API;
jump(new LocalDate(), true);
jump(new LocalDate(), true,DateChangeBehavior.API);
}


@Override
public void jumpDate(String formatDate) {
mDateChangeBehavior = DateChangeBehavior.API;
LocalDate jumpDate;
try {
jumpDate = new LocalDate(formatDate);
} catch (Exception e) {
throw new IllegalArgumentException("jumpDate的参数需要 yyyy-MM-dd 格式的日期");
}

jump(jumpDate, true);
jump(jumpDate, true,DateChangeBehavior.API);
}


@Override
public void jumpDate(int year, int month, int day) {
mDateChangeBehavior = DateChangeBehavior.API;
LocalDate jumpDate;
try {
jumpDate = new LocalDate(year, month, day);
} catch (Exception e) {
throw new IllegalArgumentException("jumpDate的参数需要正确的年月日数据");
}
jump(jumpDate, true);
jump(jumpDate, true,DateChangeBehavior.API);
}

@Override
public void jumpMonth(int year, int month) {
mDateChangeBehavior = DateChangeBehavior.API;
LocalDate jumpDate;
try {
jumpDate = new LocalDate(year, month, 1);
} catch (Exception e) {
throw new IllegalArgumentException("jumpDate的参数需要正确的年月日数据");
}
jump(jumpDate, mCheckModel == CheckModel.SINGLE_DEFAULT_CHECKED);
jump(jumpDate, mCheckModel == CheckModel.SINGLE_DEFAULT_CHECKED,DateChangeBehavior.API);
}

@Override
Expand Down Expand Up @@ -554,14 +552,6 @@ public int getPivotDistanceFromTop() {
//相差count之后的的日期
protected abstract LocalDate getIntervalDate(LocalDate localDate, int count);

// @Override
// protected void dispatchDraw(Canvas canvas) {
// super.dispatchDraw(canvas);
// if (!mIsInflateFinish) {
// drawView(getCurrentItem());
// mIsInflateFinish = true;
// }
// }

@Override
public List<LocalDate> getCurrPagerCheckDateList() {
Expand Down Expand Up @@ -601,6 +591,11 @@ public void setCheckMode(CheckModel checkModel) {
}
}

@Override
public CheckModel getCheckModel() {
return mCheckModel;
}

@Override
public void setDefaultCheckedFirstDate(boolean isDefaultCheckedFirstDate) {
this.mDefaultCheckedFirstDate = isDefaultCheckedFirstDate;
Expand Down
2 changes: 2 additions & 0 deletions ncalendar/src/main/java/com/necer/calendar/ICalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface ICalendar {
*/
void setCheckMode(CheckModel checkModel);

CheckModel getCheckModel();

/**
* 多选个数和模式
*
Expand Down
16 changes: 12 additions & 4 deletions ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.necer.R;
import com.necer.enumeration.CalendarState;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.enumeration.MultipleCountModel;
import com.necer.enumeration.CheckModel;
import com.necer.listener.OnCalendarChangedListener;
Expand Down Expand Up @@ -113,13 +114,14 @@ public NCalendar(@NonNull Context context, @Nullable AttributeSet attrs, int def
//交换数据,保证月周选中数据同步
weekCalendar.exchangeCheckedDateList(totalCheckedDateList);
//月日历变化,改变周的选中
weekCalendar.jump(localDate, false);
// weekCalendar.setDateChangeBehavior();
weekCalendar.jump(localDate, getCheckModel() == CheckModel.SINGLE_DEFAULT_CHECKED, DateChangeBehavior.API);

} else if (baseCalendar == weekCalendar && childViewY == weekHeight) {
//交换数据,保证月周选中数据同步
monthCalendar.exchangeCheckedDateList(totalCheckedDateList);
//周日历变化,改变月的选中
monthCalendar.jump(localDate, false);
monthCalendar.jump(localDate, getCheckModel() == CheckModel.SINGLE_DEFAULT_CHECKED, DateChangeBehavior.API);
monthCalendar.post(() -> {
//此时需要根据月日历的选中日期调整值
// 跳转翻页需要时间,需要等待完成之后再调整,如果不这样直接获取位置信息,会出现老的数据,不能获取正确的数据
Expand Down Expand Up @@ -617,6 +619,12 @@ public void setCheckMode(CheckModel checkModel) {
weekCalendar.setCheckMode(checkModel);
}

@Override
public CheckModel getCheckModel() {
return monthCalendar.getCheckModel();
}


@Override
public void setDefaultCheckedFirstDate(boolean isDefaultSelectFitst) {
monthCalendar.setDefaultCheckedFirstDate(isDefaultSelectFitst);
Expand Down Expand Up @@ -791,7 +799,7 @@ private void callBackCalendarState() {

//重新设置周的显示 展开月日历上面可能有选中的日期,需要下次折叠时周日历的显示要正确
LocalDate pivot = monthCalendar.getPivotDate();
weekCalendar.jump(pivot, false);
weekCalendar.jump(pivot, getCheckModel() == CheckModel.SINGLE_DEFAULT_CHECKED, DateChangeBehavior.API);

if (onCalendarStateChangedListener != null) {
onCalendarStateChangedListener.onCalendarStateChange(calendarState);
Expand All @@ -803,7 +811,7 @@ private void callBackCalendarState() {

//重新设置周的显示 展开月日历上面可能有选中的日期,需要下次折叠时周日历的显示要正确
LocalDate pivot = monthCalendar.getPivotDate();
weekCalendar.jump(pivot, false);
weekCalendar.jump(pivot, getCheckModel() == CheckModel.SINGLE_DEFAULT_CHECKED, DateChangeBehavior.API);

if (onCalendarStateChangedListener != null) {
onCalendarStateChangedListener.onCalendarStateChange(calendarState);
Expand Down

0 comments on commit ac30a5c

Please sign in to comment.