Skip to content

Commit

Permalink
修复莫名其妙跳转到2099年的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Aug 2, 2019
1 parent d9add3e commit 09473f1
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 37 deletions.
3 changes: 3 additions & 0 deletions app/src/main/res/layout/activity_emui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
app:selectCircleColor="#F29B38"
app:solarHolidayTextColor="#F29B38"
app:solarTermTextColor="#F29B38"
app:isAllMonthSixLine="true"
app:todaySolarTextColor="#F29B38">

<android.support.v4.widget.NestedScrollView
Expand All @@ -91,6 +92,8 @@
android:layout_margin="15dp"
android:background="#f5f5f5"
android:textColor="#333333"
android:text="测试"
android:gravity="center"
android:textSize="13sp" />

</android.support.v4.widget.NestedScrollView>
Expand Down
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.necer.utils.Attrs;
import com.necer.view.CalendarView;

import org.joda.time.LocalDate;
Expand All @@ -21,15 +22,15 @@ public abstract class BaseCalendarAdapter extends PagerAdapter {
protected int mCount;//总页数
protected int mCurr;//当前位置
protected LocalDate mInitializeDate;
protected int mFirstDayOfWeek;
protected Attrs mAttrs;


public BaseCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
public BaseCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
this.mContext = context;
this.mInitializeDate = initializeDate;
this.mCount = getIntervalCount(startDate, endDate, firstDayOfWeek) + 1;
this.mCurr = getIntervalCount(startDate, initializeDate, firstDayOfWeek);
this.mFirstDayOfWeek = firstDayOfWeek;
this.mCount = getIntervalCount(startDate, endDate, attrs.firstDayOfWeek) + 1;
this.mCurr = getIntervalCount(startDate, initializeDate, attrs.firstDayOfWeek);
this.mAttrs = attrs;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.view.ViewGroup;

import com.necer.utils.Attrs;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
import com.necer.view.MonthView;
Expand All @@ -18,14 +19,14 @@
public class MonthCalendarAdapter extends BaseCalendarAdapter {


public MonthCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
super(context, startDate, endDate, initializeDate, firstDayOfWeek);
public MonthCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
super(context, startDate, endDate, initializeDate, attrs);
}

@Override
protected CalendarView getCalendarView(ViewGroup container, int position) {
LocalDate localDate = mInitializeDate.plusMonths(position - mCurr);
List<LocalDate> dateList = CalendarUtil.getMonthCalendar(localDate, mFirstDayOfWeek);
List<LocalDate> dateList = CalendarUtil.getMonthCalendar(localDate, mAttrs.firstDayOfWeek, mAttrs.isAllMonthSixLine);
return new MonthView(mContext, container, localDate, dateList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.view.ViewGroup;

import com.necer.utils.Attrs;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
import com.necer.view.WeekView;
Expand All @@ -16,14 +17,14 @@
* qq群:127278900
*/
public class WeekCalendarAdapter extends BaseCalendarAdapter {
public WeekCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
super(context, startDate, endDate, initializeDate, firstDayOfWeek);
public WeekCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
super(context, startDate, endDate, initializeDate, attrs);
}

@Override
protected CalendarView getCalendarView(ViewGroup container, int position) {
LocalDate localDate = mInitializeDate.plusDays((position - mCurr) * 7);
List<LocalDate> dateList = CalendarUtil.getWeekCalendar(localDate, mFirstDayOfWeek);
List<LocalDate> dateList = CalendarUtil.getWeekCalendar(localDate, mAttrs.firstDayOfWeek);
return new WeekView(mContext, container, localDate, dateList);
}

Expand Down
11 changes: 6 additions & 5 deletions ncalendar/src/main/java/com/necer/calendar/BaseCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;

import com.necer.adapter.BaseCalendarAdapter;
Expand Down Expand Up @@ -99,7 +100,7 @@ private void initAdapter() {
throw new RuntimeException("日期区间必须包含初始化日期");
}

BaseCalendarAdapter calendarAdapter = getCalendarAdapter(mContext, mStartDate, mEndDate, mInitializeDate, mAttrs.firstDayOfWeek);
BaseCalendarAdapter calendarAdapter = getCalendarAdapter(mContext, mStartDate, mEndDate, mInitializeDate, mAttrs);
int currItem = calendarAdapter.getCurrItem();
setAdapter(calendarAdapter);
setCurrentItem(currItem);
Expand Down Expand Up @@ -288,14 +289,14 @@ public void run() {
@Override
public void notifyCalendar() {
for (int i = 0; i < getChildCount(); i++) {
CalendarView calendarView = (CalendarView) getChildAt(i);
if (calendarView != null) {
View childAt = getChildAt(i);
if (childAt != null && childAt instanceof CalendarView) {
CalendarView calendarView = (CalendarView) childAt;
calendarView.invalidate();
}
}
}


//日期边界处理
private LocalDate getAvailableDate(LocalDate localDate) {
if (localDate.isBefore(mStartDate)) {
Expand Down Expand Up @@ -447,7 +448,7 @@ public int getPivotDistanceFromTop() {
}

//回去viewpager的adapter
protected abstract BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek);
protected abstract BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs);

//两个日期的相差数量
protected abstract int getTwoDateCount(LocalDate startDate, LocalDate endDate, int type);
Expand Down
4 changes: 2 additions & 2 deletions ncalendar/src/main/java/com/necer/calendar/MonthCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public MonthCalendar(@NonNull Context context, @Nullable AttributeSet attributeS
}

@Override
protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
return new MonthCalendarAdapter(context, startDate, endDate, initializeDate, firstDayOfWeek);
protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
return new MonthCalendarAdapter(context, startDate, endDate, initializeDate, attrs);
}

@Override
Expand Down
28 changes: 15 additions & 13 deletions ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingParent;
Expand All @@ -18,6 +16,7 @@
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.necer.R;
import com.necer.enumeration.CalendarState;
import com.necer.enumeration.MultipleNumModel;
import com.necer.enumeration.SelectedModel;
Expand Down Expand Up @@ -96,6 +95,9 @@ public NCalendar(@NonNull Context context, @Nullable AttributeSet attrs, int def
monthCalendar = new MonthCalendar(context, attrs);
weekCalendar = new WeekCalendar(context, attrs);

monthCalendar.setId(R.id.N_monthCalendar);
weekCalendar.setId(R.id.N_weekCalendar);

setCalendarPainter(new InnerPainter(this));

monthCalendar.setOnMWDateChangeListener(onMWDateChangeListener);
Expand Down Expand Up @@ -188,7 +190,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {

weekCalendar.layout(0 + paddingLeft, 0, measuredWidth - paddingRight, weekHeight);

if (childView.getY() >= monthHeight) {
if (childView.getY() >= monthHeight && isMonthStretchEnable) {
monthCalendar.layout(0 + paddingLeft, 0, measuredWidth - paddingRight, stretchMonthHeight);
} else {
monthCalendar.layout(0 + paddingLeft, 0, measuredWidth - paddingRight, monthHeight);
Expand Down Expand Up @@ -312,11 +314,13 @@ public void onStopNestedScroll(View target) {
//此时 childViewY 必为 3个标志位之一,判断不为这三个数值就自动滑动

int childViewY = (int) childView.getY();
int monthCalendarY = (int) monthCalendar.getY();

if (childViewY != monthHeight && childViewY != weekHeight && childViewY != stretchMonthHeight) {
if ((childViewY != monthHeight && childViewY != weekHeight && childViewY != stretchMonthHeight) ||
(monthCalendarY != 0 && monthCalendarY != monthCalendar.getPivotDistanceFromTop())) {
autoScroll();
} else {
setCalenadrState();
callBackCalenadarState();
}
}

Expand Down Expand Up @@ -546,6 +550,9 @@ public CalendarState getCalendarState() {

@Override
public void setCalendarState(CalendarState calendarState) {
if (calendarState == CalendarState.MONTH_STRETCH) {
throw new RuntimeException("不允许直接设置成CalendarState.MONTH_STRETCH,可以设置成CalendarState.WEEK或者CalendarState.MONTH");
}
this.calendarState = calendarState;
}

Expand Down Expand Up @@ -680,11 +687,6 @@ public void setMonthStretchEnable(boolean isMonthStretchEnable) {
this.isMonthStretchEnable = isMonthStretchEnable;
}

//修复id重复
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(null);
}

@Override
public void onAnimationUpdate(ValueAnimator animation) {
Expand Down Expand Up @@ -712,12 +714,12 @@ public void onAnimationUpdate(ValueAnimator animation) {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
setCalenadrState();
callBackCalenadarState();
}
};

//设置日历的状态、月周的显示及状态回调
private void setCalenadrState() {
private void callBackCalenadarState() {

int childViewY = (int) childView.getY();

Expand Down Expand Up @@ -781,7 +783,7 @@ protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (!isInflateFinish) {
monthCalendar.setVisibility(calendarState == CalendarState.MONTH ? VISIBLE : INVISIBLE);
weekCalendar.setVisibility(calendarState == CalendarState.MONTH ? INVISIBLE : VISIBLE);
weekCalendar.setVisibility(calendarState == CalendarState.WEEK ? VISIBLE : INVISIBLE);
monthRect = new RectF(0, 0, monthCalendar.getMeasuredWidth(), monthCalendar.getMeasuredHeight());
weekRect = new RectF(0, 0, weekCalendar.getMeasuredWidth(), weekCalendar.getMeasuredHeight());
stretchMonthRect = new RectF(0, 0, monthCalendar.getMeasuredWidth(), stretchMonthHeight);
Expand Down
4 changes: 2 additions & 2 deletions ncalendar/src/main/java/com/necer/calendar/WeekCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public WeekCalendar(@NonNull Context context, @Nullable AttributeSet attributeSe
}

@Override
protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
return new WeekCalendarAdapter(context, startDate, endDate, initializeDate, firstDayOfWeek);
protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, Attrs attrs) {
return new WeekCalendarAdapter(context, startDate, endDate, initializeDate, attrs);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions ncalendar/src/main/java/com/necer/utils/Attrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ public class Attrs {
public int stretchTextColor; //拉伸显示的字体颜色
public float stretchTextDistance; //拉伸显示的字体距离矩形中心的距离

public boolean isAllMonthSixLine;//月是否都6行


}
2 changes: 2 additions & 0 deletions ncalendar/src/main/java/com/necer/utils/AttrsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.stretchTextDistance = ta.getDimension(R.styleable.NCalendar_stretchTextDistance, CalendarUtil.dp2px(context, 32));
attrs.stretchTextColor = ta.getColor(R.styleable.NCalendar_stretchTextColor, context.getResources().getColor(R.color.stretchTextColor));

attrs.isAllMonthSixLine = ta.getBoolean(R.styleable.NCalendar_isAllMonthSixLine, false);

ta.recycle();

return attrs;
Expand Down
28 changes: 24 additions & 4 deletions ncalendar/src/main/java/com/necer/utils/CalendarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ public static boolean isToday(LocalDate date) {
}

/**
* @param localDate 今天
* @param type 300,周日,301周一
* @param localDate
* @param weekType 300,周日,301周一
* @return
*/
public static List<LocalDate> getMonthCalendar(LocalDate localDate, int type) {
public static List<LocalDate> getMonthCalendar(LocalDate localDate, int weekType, boolean isAllMonthSixLine) {

LocalDate lastMonthDate = localDate.plusMonths(-1);//上个月
LocalDate nextMonthDate = localDate.plusMonths(1);//下个月
Expand All @@ -176,7 +176,7 @@ public static List<LocalDate> getMonthCalendar(LocalDate localDate, int type) {


//周一开始的
if (type == Attrs.MONDAY) {
if (weekType == Attrs.MONDAY) {

//周一开始的
for (int i = 0; i < firstDayOfWeek - 1; i++) {
Expand Down Expand Up @@ -222,6 +222,26 @@ public static List<LocalDate> getMonthCalendar(LocalDate localDate, int type) {
dateList.add(date);
}
}

//是否所有月份都6行
if (isAllMonthSixLine && dateList.size() == 35) {
LocalDate endLocalDate = dateList.get(dateList.size() - 1);
int dayOfMonth = endLocalDate.getDayOfMonth();
//如果是当月最后一天,直接加上下个月的7天,如果不是当月了,已经是下月了,就顺着下月数7天
if (dayOfMonth == days) {
for (int i = 0; i < 7; i++) {
LocalDate date = new LocalDate(nextMonthDate.getYear(), nextMonthDate.getMonthOfYear(), i + 1);
dateList.add(date);
}
} else {
for (int i = 0; i < 7; i++) {
LocalDate date = new LocalDate(nextMonthDate.getYear(), nextMonthDate.getMonthOfYear(), dayOfMonth + i + 1);
dateList.add(date);
}
}
}


return dateList;

}
Expand Down
1 change: 1 addition & 0 deletions ncalendar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


<attr name="isShowHoliday" format="boolean" />
<attr name="isAllMonthSixLine" format="boolean" />
<attr name="holidayColor" format="color" />
<attr name="workdayColor" format="color" />
<attr name="todaySelectContrastColor" format="color" />
Expand Down
5 changes: 5 additions & 0 deletions ncalendar/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="N_monthCalendar"/>
<item type="id" name="N_weekCalendar"/>
</resources>

0 comments on commit 09473f1

Please sign in to comment.