Skip to content

Commit

Permalink
增加多选模式下初始化选中多个日期
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Apr 1, 2020
1 parent 2c67665 commit 4df76f0
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.necer.entity.CalendarDate;
import com.necer.entity.Lunar;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.enumeration.MultipleCountModel;
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.ncalendar.R;
Expand Down Expand Up @@ -56,6 +57,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
innerPainter.setPointList(pointList);



// Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
// miui10Calendar.setMonthCalendarBackground(new CalendarBackground() {
// @Override
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_miui10.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:showNumberBackground="true"
>
app:solarTextBold="true">


<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@android:color/darker_gray">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_miui10"
android:orientation="vertical"
android:padding="15dp">
Expand Down
21 changes: 21 additions & 0 deletions ncalendar/src/main/java/com/necer/calendar/BaseCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,27 @@ public List<LocalDate> getTotalCheckedDateList() {
return mTotalCheckedDateList;
}


@Override
public void setCheckedDates(List<String> dateList) {

if (mCheckModel != CheckModel.MULTIPLE) {
throw new RuntimeException(getContext().getString(R.string.N_set_checked_dates_illegal));
}

if (mMultipleCountModel != null && dateList.size() > mMultipleCount) {
throw new RuntimeException(getContext().getString(R.string.N_set_checked_dates_count_illegal));
}
mTotalCheckedDateList.clear();
try {
for (int i = 0; i < dateList.size(); i++) {
mTotalCheckedDateList.add(new LocalDate(dateList.get(i)));
}
} catch (Exception e) {
throw new IllegalArgumentException(getContext().getString(R.string.N_date_format_illegal));
}
}

//点击的日期是否可用
public boolean isAvailable(LocalDate localDate) {
return !localDate.isBefore(mStartDate) && !localDate.isAfter(mEndDate);
Expand Down
8 changes: 8 additions & 0 deletions ncalendar/src/main/java/com/necer/calendar/ICalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,12 @@ public interface ICalendar {
CalendarBackground getCalendarBackground() throws IllegalAccessException;


/**
* 获选模式下,初始化时选中的日期
*
* @param dateList 日期几何 yyyy-MM-dd
*/
void setCheckedDates(List<String> dateList);


}
18 changes: 13 additions & 5 deletions ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void autoToWeekBySetY() {

//自动滑动到月 通过setY 周->月
private void autoToMonthBySetY() {
float monthCalendarStart = monthCalendar.getY();//起始位置
float monthCalendarStart = monthCalendar.getY();
float monthCalendarEnd = 0;
monthValueAnimator.setFloatValues(monthCalendarStart, monthCalendarEnd);
monthValueAnimator.start();
Expand Down Expand Up @@ -400,7 +400,7 @@ protected void gestureMove(float dy, int[] consumed) {
}
scrolling(dy);

} else if (dy < 0 && childViewY <= monthHeight && childViewY >= weekHeight && (!isWeekHoldEnable || consumed == null) && (targetView == null || !targetView.canScrollVertically(-1))) {
} else if (dy < 0 && childViewY <= monthHeight && childViewY >= weekHeight && (!(isWeekHoldEnable && calendarState == CalendarState.WEEK) || consumed == null) && (targetView == null || !targetView.canScrollVertically(-1))) {
//setY 持续过程 周->月的过程
if (isMonthStretchEnable && realMonthHeight != monthHeight) {
layoutParams.height = monthHeight;
Expand Down Expand Up @@ -557,6 +557,14 @@ public void setInitializeDate(String formatDate) {
weekCalendar.setInitializeDate(formatDate);
}

@Override
public void setCheckedDates(List<String> dateList) {
if (calendarState == CalendarState.WEEK) {
weekCalendar.setCheckedDates(dateList);
} else {
monthCalendar.setCheckedDates(dateList);
}
}

@Override
public void toToday() {
Expand Down Expand Up @@ -608,7 +616,7 @@ public CalendarState getCalendarState() {
@Override
public void setCalendarState(CalendarState calendarState) {
if (calendarState == CalendarState.MONTH_STRETCH) {
throw new RuntimeException("不允许直接设置成CalendarState.MONTH_STRETCH,可以设置成CalendarState.WEEK或者CalendarState.MONTH");
throw new RuntimeException(getContext().getString(R.string.N_calendarState_illegal));
}
this.calendarState = calendarState;
}
Expand Down Expand Up @@ -924,12 +932,12 @@ protected float getOffset(float offset, float maxOffset) {

@Override
public CalendarBackground getCalendarBackground() throws IllegalAccessException {
throw new IllegalAccessException("折叠日历不能使用此方法");
throw new IllegalAccessException(getContext().getString(R.string.N_NCalendar_calendar_background_illegal));
}

@Override
public void setCalendarBackground(CalendarBackground calendarBackground) throws IllegalAccessException {
throw new IllegalAccessException("折叠日历请调用setMonthCalendarBackground()和setWeekCalendarBackground()");
throw new IllegalAccessException(getContext().getString(R.string.N_NCalendar_set_calendar_background_illegal));
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions ncalendar/src/main/java/com/necer/painter/InnerPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void drawSolar(Canvas canvas, RectF rectF, LocalDate date, int color, in
mTextPaint.setColor(color);
mTextPaint.setAlpha(alphaColor);
mTextPaint.setTextSize(mAttrs.solarTextSize);
// mTextPaint.setFakeBoldText(true);
mTextPaint.setFakeBoldText(mAttrs.solarTextBold);
canvas.drawText(date.getDayOfMonth() + "", rectF.centerX(), mAttrs.showLunar ? rectF.centerY() : getTextBaseLineY(rectF.centerY()), mTextPaint);
}

Expand All @@ -196,7 +196,7 @@ private void drawLunar(Canvas canvas, RectF rectF, LocalDate localDate, int colo
mTextPaint.setColor(replaceColor == null ? color : replaceColor);
mTextPaint.setTextSize(mAttrs.lunarTextSize);
mTextPaint.setAlpha(alphaColor);
// mTextPaint.setFakeBoldText(false);
mTextPaint.setFakeBoldText(mAttrs.lunarTextBold);
canvas.drawText(lunarString, rectF.centerX(), rectF.centerY() + mAttrs.lunarDistance, mTextPaint);
}
}
Expand All @@ -215,7 +215,7 @@ private void drawPoint(Canvas canvas, RectF rectF, LocalDate date, Drawable draw

//绘制节假日
private void drawHolidayWorkday(Canvas canvas, RectF rectF, LocalDate localDate, Drawable holidayDrawable, Drawable workdayDrawable, int holidayTextColor, int workdayTextColor, int alphaColor) {
if (mAttrs.showHoliday) {
if (mAttrs.showHolidayWorkday) {
int[] holidayLocation = getHolidayWorkdayLocation(rectF.centerX(), rectF.centerY());
if (mHolidayList.contains(localDate)) {
if (holidayDrawable == null) {
Expand All @@ -232,6 +232,7 @@ private void drawHolidayWorkday(Canvas canvas, RectF rectF, LocalDate localDate,
if (workdayDrawable == null) {
mTextPaint.setTextSize(mAttrs.holidayWorkdayTextSize);
mTextPaint.setColor(workdayTextColor);
mTextPaint.setFakeBoldText(mAttrs.holidayWorkdayTextBold);
canvas.drawText(TextUtils.isEmpty(mAttrs.workdayText) ? mContext.getString(R.string.N_workdayText) : mAttrs.workdayText, holidayLocation[0], getTextBaseLineY(holidayLocation[1]), mTextPaint);
} else {
Rect drawableBounds = DrawableUtil.getDrawableBounds(holidayLocation[0], holidayLocation[1], workdayDrawable);
Expand All @@ -253,6 +254,7 @@ private void drawStretchText(Canvas canvas, RectF rectF, int alphaColor, LocalDa
mTextPaint.setTextSize(mAttrs.stretchTextSize);
mTextPaint.setColor(mAttrs.stretchTextColor);
mTextPaint.setAlpha(alphaColor);
mTextPaint.setFakeBoldText(mAttrs.stretchTextBold);
canvas.drawText(stretchText, rectF.centerX(), rectF.centerY() + mAttrs.stretchTextDistance, mTextPaint);
}
}
Expand Down
31 changes: 29 additions & 2 deletions ncalendar/src/main/java/com/necer/utils/Attrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public class Attrs {
*/
public float solarTextSize;

/**
* 公历日期属性
* 公历日期是否加粗
*/
public boolean solarTextBold;

/**
* 标记日期属性
Expand Down Expand Up @@ -160,7 +165,7 @@ public class Attrs {
* 节假日日期属性 text
* 是否显示节假日和工作日标记
*/
public boolean showHoliday;
public boolean showHolidayWorkday;
/**
* 节假日日期属性 text
* 节假日文字
Expand All @@ -176,6 +181,13 @@ public class Attrs {
* 字体大小
*/
public float holidayWorkdayTextSize;


/**
* 节假日日期属性 text
* 是否加粗
*/
public boolean holidayWorkdayTextBold;
/**
* 节假日日期属性 text
* 文字距离中心距离
Expand Down Expand Up @@ -258,6 +270,13 @@ public class Attrs {
* 农历字体大小
*/
public float lunarTextSize;


/**
* 农历属性
* 农历字体是否加粗
*/
public boolean lunarTextBold;
/**
* 农历属性
* 农历到文字中心的距离
Expand Down Expand Up @@ -313,6 +332,12 @@ public class Attrs {
* 拉伸显示的字体大小
*/
public float stretchTextSize;

/**
* 拉伸字体加粗
*/
public boolean stretchTextBold;

/**
* 拉伸显示的字体颜色
*/
Expand Down Expand Up @@ -359,7 +384,9 @@ public class Attrs {
*/
public int defaultCheckedBackground;


/**
* 日历背景
*/
public Drawable calendarBackground;


Expand Down
7 changes: 6 additions & 1 deletion ncalendar/src/main/java/com/necer/utils/AttrsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.defaultCheckedSolarTextColor = ta.getColor(R.styleable.NCalendar_defaultCheckedSolarTextColor, ContextCompat.getColor(context, R.color.N_defaultSolarTextColor));
attrs.defaultUnCheckedSolarTextColor = ta.getColor(R.styleable.NCalendar_defaultUnCheckedSolarTextColor, ContextCompat.getColor(context, R.color.N_defaultSolarTextColor));
attrs.solarTextSize = ta.getDimension(R.styleable.NCalendar_solarTextSize, context.getResources().getDimension(R.dimen.N_solarTextSize));
attrs.solarTextBold = ta.getBoolean(R.styleable.NCalendar_solarTextBold, context.getResources().getBoolean(R.bool.N_textBold));

attrs.pointLocation = ta.getInt(R.styleable.NCalendar_pointLocation, Attrs.UP);
attrs.pointDistance = ta.getDimension(R.styleable.NCalendar_pointDistance, context.getResources().getDimension(R.dimen.N_pointDistance));
Expand All @@ -41,8 +42,10 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.todayUnCheckedWorkday = ta.getDrawable(R.styleable.NCalendar_todayUnCheckedWorkday);
attrs.defaultCheckedWorkday = ta.getDrawable(R.styleable.NCalendar_defaultCheckedWorkday);
attrs.defaultUnCheckedWorkday = ta.getDrawable(R.styleable.NCalendar_defaultUnCheckedWorkday);
attrs.holidayWorkdayTextBold = ta.getBoolean(R.styleable.NCalendar_holidayWorkdayTextBold, context.getResources().getBoolean(R.bool.N_textBold));

attrs.showHoliday = ta.getBoolean(R.styleable.NCalendar_showHoliday, context.getResources().getBoolean(R.bool.N_showHoliday));

attrs.showHolidayWorkday = ta.getBoolean(R.styleable.NCalendar_showHoliday, context.getResources().getBoolean(R.bool.N_showHolidayWorkday));
attrs.holidayWorkdayTextSize = ta.getDimension(R.styleable.NCalendar_holidayWorkdayTextSize, context.getResources().getDimension(R.dimen.N_holidayWorkdayTextSize));
attrs.holidayWorkdayDistance = ta.getDimension(R.styleable.NCalendar_holidayWorkdayDistance, context.getResources().getDimension(R.dimen.N_holidayWorkdayDistance));
attrs.holidayWorkdayLocation = ta.getInt(R.styleable.NCalendar_holidayWorkdayLocation, Attrs.TOP_RIGHT);
Expand All @@ -63,6 +66,7 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.defaultCheckedLunarTextColor = ta.getColor(R.styleable.NCalendar_defaultCheckedLunarTextColor, ContextCompat.getColor(context, R.color.N_defaultLunarTextColor));
attrs.defaultUnCheckedLunarTextColor = ta.getColor(R.styleable.NCalendar_defaultUnCheckedLunarTextColor, ContextCompat.getColor(context, R.color.N_defaultLunarTextColor));
attrs.lunarTextSize = ta.getDimension(R.styleable.NCalendar_lunarTextSize, context.getResources().getDimension(R.dimen.N_lunarTextSize));
attrs.lunarTextBold = ta.getBoolean(R.styleable.NCalendar_lunarTextBold, context.getResources().getBoolean(R.bool.N_textBold));
attrs.lunarDistance = ta.getDimension(R.styleable.NCalendar_lunarDistance, context.getResources().getDimension(R.dimen.N_lunarDistance));

attrs.calendarHeight = (int) ta.getDimension(R.styleable.NCalendar_calendarHeight, context.getResources().getDimension(R.dimen.N_calendarHeight));
Expand All @@ -76,6 +80,7 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.disabledString = ta.getString(R.styleable.NCalendar_disabledString);

attrs.stretchTextSize = ta.getDimension(R.styleable.NCalendar_stretchTextSize, context.getResources().getDimension(R.dimen.N_stretchTextSize));
attrs.stretchTextBold = ta.getBoolean(R.styleable.NCalendar_stretchTextBold, context.getResources().getBoolean(R.bool.N_textBold));
attrs.stretchTextDistance = ta.getDimension(R.styleable.NCalendar_stretchTextDistance, context.getResources().getDimension(R.dimen.N_stretchTextDistance));
attrs.stretchTextColor = ta.getColor(R.styleable.NCalendar_stretchTextColor, ContextCompat.getColor(context, R.color.N_stretchTextColor));

Expand Down
4 changes: 4 additions & 0 deletions ncalendar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<declare-styleable name="NCalendar">

<attr name="solarTextSize" format="dimension" />
<attr name="solarTextBold" format="boolean" />
<attr name="todayCheckedSolarTextColor" format="color" />
<attr name="todayUnCheckedSolarTextColor" format="color" />
<attr name="defaultCheckedSolarTextColor" format="color" />
Expand All @@ -14,6 +15,7 @@
<attr name="defaultCheckedLunarTextColor" format="color" />
<attr name="defaultUnCheckedLunarTextColor" format="color" />
<attr name="lunarTextSize" format="dimension" />
<attr name="lunarTextBold" format="boolean" />
<attr name="lunarDistance" format="dimension" />


Expand All @@ -39,6 +41,7 @@

<attr name="holidayText" format="string" />
<attr name="workdayText" format="string" />
<attr name="holidayWorkdayTextBold" format="boolean" />
<attr name="holidayWorkdayTextSize" format="dimension" />
<attr name="holidayWorkdayDistance" format="dimension" />
<attr name="todayCheckedHolidayTextColor" format="color" />
Expand Down Expand Up @@ -89,6 +92,7 @@
</attr>

<attr name="stretchTextSize" format="dimension" />
<attr name="stretchTextBold" format="boolean" />
<attr name="stretchTextDistance" format="dimension" />
<attr name="stretchTextColor" format="color" />

Expand Down
3 changes: 2 additions & 1 deletion ncalendar/src/main/res/values/bools.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="N_showHoliday">true</bool>
<bool name="N_textBold">false</bool>
<bool name="N_showHolidayWorkday">true</bool>
<bool name="N_showLunar">true</bool>
<bool name="N_allMonthSixLine">false</bool>
<bool name="N_showNumberBackground">false</bool>
Expand Down
2 changes: 1 addition & 1 deletion ncalendar/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<dimen name="N_solarTextSize">17sp</dimen>
<dimen name="N_solarTextSize">18sp</dimen>
<dimen name="N_lunarTextSize">10sp</dimen>
<dimen name="N_lunarDistance">15dp</dimen>
<dimen name="N_holidayWorkdayTextSize">10sp</dimen>
Expand Down
5 changes: 5 additions & 0 deletions ncalendar/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
<string name="N_end_after_20991231">endDate必须在2099-12-31之前</string>
<string name="N_initialize_date_illegal">日期区间必须包含初始化日期</string>
<string name="N_date_format_illegal">需要 yyyy-MM-dd 格式的日期</string>
<string name="N_set_checked_dates_illegal">只能多选模式下才能使用此方法</string>
<string name="N_set_checked_dates_count_illegal">设置的日期数量和MultipleCountModel冲突</string>
<string name="N_date_format_jump">jumpDate的参数需要正确的年月日数据</string>
<string name="N_stretch_month_height">日历拉伸之后的高度必须大于正常高度,日历默认的正常高度为300dp</string>
<string name="N_NCalendar_child_num">NCalendar中的有且只能有一个直接子view</string>
<string name="N_NCalendar_calendar_background_illegal">折叠日历不能使用此方法</string>
<string name="N_NCalendar_set_calendar_background_illegal">折叠日历请调用setMonthCalendarBackground()和setWeekCalendarBackground()</string>
<string name="N_calendarState_illegal">不允许直接设置成CalendarState.MONTH_STRETCH,可以设置成CalendarState.WEEK或者CalendarState.MONTH</string>
</resources>

0 comments on commit 4df76f0

Please sign in to comment.