From c9d04ab27007ed6af1c5e3db2552b56a524e0f70 Mon Sep 17 00:00:00 2001 From: Sylvester Yao Date: Fri, 12 Jul 2024 14:01:26 +0800 Subject: [PATCH] Support any week day start. --- .../calendarviewproject/EnglishWeekBar.java | 12 ++-- .../custom/CustomWeekBar.java | 12 ++-- .../calendarviewproject/mix/MixWeekBar.java | 12 ++-- .../solay/SolarWeekBar.java | 12 ++-- calendarview/build.gradle | 2 + .../com/haibin/calendarview/CalendarUtil.java | 66 ++++++++----------- .../com/haibin/calendarview/CalendarView.java | 10 +-- .../java/com/haibin/calendarview/WeekBar.java | 26 ++++---- calendarview/src/main/res/values/attrs.xml | 4 ++ 9 files changed, 70 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/com/haibin/calendarviewproject/EnglishWeekBar.java b/app/src/main/java/com/haibin/calendarviewproject/EnglishWeekBar.java index deb54a8c..9ce96484 100644 --- a/app/src/main/java/com/haibin/calendarviewproject/EnglishWeekBar.java +++ b/app/src/main/java/com/haibin/calendarviewproject/EnglishWeekBar.java @@ -54,14 +54,12 @@ protected void onWeekStartChange(int weekStart) { */ private String getWeekString(int index, int weekStart) { String[] weeks = getContext().getResources().getStringArray(R.array.english_week_string_array); - - if (weekStart == 1) { - return weeks[index]; - } - if (weekStart == 2) { - return weeks[index == 6 ? 0 : index + 1]; + int offset = weekStart - java.util.Calendar.SUNDAY; + int adjustedIndex = (index + offset) % 7; + if (adjustedIndex < 0) { + adjustedIndex += 7; } - return weeks[index == 0 ? 6 : index - 1]; + return weeks[adjustedIndex]; } /** diff --git a/app/src/main/java/com/haibin/calendarviewproject/custom/CustomWeekBar.java b/app/src/main/java/com/haibin/calendarviewproject/custom/CustomWeekBar.java index 3c78fd5f..5112496e 100644 --- a/app/src/main/java/com/haibin/calendarviewproject/custom/CustomWeekBar.java +++ b/app/src/main/java/com/haibin/calendarviewproject/custom/CustomWeekBar.java @@ -52,13 +52,11 @@ protected void onWeekStartChange(int weekStart) { */ private String getWeekString(int index, int weekStart) { String[] weeks = getContext().getResources().getStringArray(R.array.chinese_week_string_array); - - if (weekStart == 1) { - return weeks[index]; - } - if (weekStart == 2) { - return weeks[index == 6 ? 0 : index + 1]; + int offset = weekStart - java.util.Calendar.SUNDAY; + int adjustedIndex = (index + offset) % 7; + if (adjustedIndex < 0) { + adjustedIndex += 7; } - return weeks[index == 0 ? 6 : index - 1]; + return weeks[adjustedIndex]; } } diff --git a/app/src/main/java/com/haibin/calendarviewproject/mix/MixWeekBar.java b/app/src/main/java/com/haibin/calendarviewproject/mix/MixWeekBar.java index 1dd1efa3..c56213e0 100644 --- a/app/src/main/java/com/haibin/calendarviewproject/mix/MixWeekBar.java +++ b/app/src/main/java/com/haibin/calendarviewproject/mix/MixWeekBar.java @@ -52,13 +52,11 @@ protected void onWeekStartChange(int weekStart) { */ private String getWeekString(int index, int weekStart) { String[] weeks = getContext().getResources().getStringArray(R.array.chinese_week_string_array); - - if (weekStart == 1) { - return weeks[index]; - } - if (weekStart == 2) { - return weeks[index == 6 ? 0 : index + 1]; + int offset = weekStart - java.util.Calendar.SUNDAY; + int adjustedIndex = (index + offset) % 7; + if (adjustedIndex < 0) { + adjustedIndex += 7; } - return weeks[index == 0 ? 6 : index - 1]; + return weeks[adjustedIndex]; } } diff --git a/app/src/main/java/com/haibin/calendarviewproject/solay/SolarWeekBar.java b/app/src/main/java/com/haibin/calendarviewproject/solay/SolarWeekBar.java index 9254170c..03cb3fb6 100644 --- a/app/src/main/java/com/haibin/calendarviewproject/solay/SolarWeekBar.java +++ b/app/src/main/java/com/haibin/calendarviewproject/solay/SolarWeekBar.java @@ -41,13 +41,11 @@ protected void onWeekStartChange(int weekStart) { */ private String getWeekString(int index, int weekStart) { String[] weeks = getContext().getResources().getStringArray(R.array.english_week_string_array); - - if (weekStart == 1) { - return weeks[index]; - } - if (weekStart == 2) { - return weeks[index == 6 ? 0 : index + 1]; + int offset = weekStart - java.util.Calendar.SUNDAY; + int adjustedIndex = (index + offset) % 7; + if (adjustedIndex < 0) { + adjustedIndex += 7; } - return weeks[index == 0 ? 6 : index - 1]; + return weeks[adjustedIndex]; } } diff --git a/calendarview/build.gradle b/calendarview/build.gradle index 76cb79a6..b6294aa3 100644 --- a/calendarview/build.gradle +++ b/calendarview/build.gradle @@ -42,5 +42,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' testImplementation 'junit:junit:4.12' + + compileOnly("androidx.viewpager:viewpager:1.0.0") } apply from: '../script/gradle-jcenter-push.gradle' \ No newline at end of file diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java index a77157c0..9711de0e 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java @@ -206,14 +206,13 @@ public static Calendar getNextCalendar(Calendar calendar) { static int getMonthViewStartDiff(Calendar calendar, int weekStart) { java.util.Calendar date = java.util.Calendar.getInstance(); date.set(calendar.getYear(), calendar.getMonth() - 1, 1, 12, 0, 0); - int week = date.get(java.util.Calendar.DAY_OF_WEEK); - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) { - return week - 1; - } - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) { - return week == 1 ? 6 : week - weekStart; + int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取该月第一天是星期几 + int diff = week - weekStart; // 计算与周起始日的差值 + if (diff < 0) { + // 如果差值小于0,说明周起始日在该月第一天之后,需要调整偏移量 + diff += 7; } - return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week; + return diff; } @@ -230,14 +229,13 @@ static int getMonthViewStartDiff(Calendar calendar, int weekStart) { static int getMonthViewStartDiff(int year, int month, int weekStart) { java.util.Calendar date = java.util.Calendar.getInstance(); date.set(year, month - 1, 1, 12, 0, 0); - int week = date.get(java.util.Calendar.DAY_OF_WEEK); - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) { - return week - 1; + int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取该月第一天是星期几 + int diff = week - weekStart; // 计算与周起始日的差值 + if (diff < 0) { + // 如果差值小于0,说明周起始日在该月第一天之后,需要调整偏移量 + diff += 7; } - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) { - return week == 1 ? 6 : week - weekStart; - } - return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week; + return diff; } @@ -269,14 +267,12 @@ static int getMonthEndDiff(int year, int month, int weekStart) { private static int getMonthEndDiff(int year, int month, int day, int weekStart) { java.util.Calendar date = java.util.Calendar.getInstance(); date.set(year, month - 1, day); - int week = date.get(java.util.Calendar.DAY_OF_WEEK); - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) { - return 7 - week; + int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取给定日期是星期几 + int diff = week - weekStart; // 计算当前星期与周起始日的差异 + if (diff < 0) { + diff += 7; // 如果差异为负数,说明跨越了周末,需要加7 } - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) { - return week == 1 ? 0 : 7 - week + 1; - } - return week == 7 ? 6 : 7 - week - 1; + return 6 - diff; // 从当前星期到周末的天数差,总共7天,所以用6减去差异 } /** @@ -691,15 +687,13 @@ static List initCalendarForWeekView(Calendar calendar, CalendarViewDel */ private static int getWeekViewStartDiff(int year, int month, int day, int weekStart) { java.util.Calendar date = java.util.Calendar.getInstance(); - date.set(year, month - 1, day, 12, 0);// - int week = date.get(java.util.Calendar.DAY_OF_WEEK); - if (weekStart == 1) { - return week - 1; - } - if (weekStart == 2) { - return week == 1 ? 6 : week - weekStart; + date.set(year, month - 1, day, 12, 0); // 设置给定的日期 + int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取这一天是周几 + int diff = week - weekStart; // 计算与周起始日的差值 + if (diff < 0) { + diff += 7; // 如果差值为负,说明给定日期在周起始日之前,需要加7天 } - return week == 7 ? 0 : week; + return diff; } @@ -717,15 +711,13 @@ private static int getWeekViewStartDiff(int year, int month, int day, int weekSt */ public static int getWeekViewEndDiff(int year, int month, int day, int weekStart) { java.util.Calendar date = java.util.Calendar.getInstance(); - date.set(year, month - 1, day, 12, 0); - int week = date.get(java.util.Calendar.DAY_OF_WEEK); - if (weekStart == 1) { - return 7 - week; - } - if (weekStart == 2) { - return week == 1 ? 0 : 7 - week + 1; + date.set(year, month - 1, day, 12, 0); // 设置日期 + int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取周几 + int diff = week - weekStart; // 计算与周起始日的差值 + if (diff < 0) { + diff += 7; // 如果差值为负,说明给定日期在周起始日之前,需要加7天 } - return week == 7 ? 6 : 7 - week - 1; + return 6 - diff; // 返回到周结束的天数差异 } diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java index aa7df945..54e5ac04 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java @@ -1537,16 +1537,12 @@ public void setWeekStarWithSat() { /** * 设置周起始 - * CalendarViewDelegate.WEEK_START_WITH_SUN - * CalendarViewDelegate.WEEK_START_WITH_MON - * CalendarViewDelegate.WEEK_START_WITH_SAT + * 支持周一到周日任意周起始 * * @param weekStart 周起始 */ - private void setWeekStart(int weekStart) { - if (weekStart != CalendarViewDelegate.WEEK_START_WITH_SUN && - weekStart != CalendarViewDelegate.WEEK_START_WITH_MON && - weekStart != CalendarViewDelegate.WEEK_START_WITH_SAT) + public void setWeekStart(int weekStart) { + if (!(java.util.Calendar.SUNDAY <= weekStart && weekStart <= java.util.Calendar.SATURDAY)) return; if (weekStart == mDelegate.getWeekStart()) return; diff --git a/calendarview/src/main/java/com/haibin/calendarview/WeekBar.java b/calendarview/src/main/java/com/haibin/calendarview/WeekBar.java index 772ad1d5..70f604c7 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/WeekBar.java +++ b/calendarview/src/main/java/com/haibin/calendarview/WeekBar.java @@ -16,6 +16,7 @@ package com.haibin.calendarview; import android.content.Context; +import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.widget.LinearLayout; @@ -108,14 +109,13 @@ protected void onWeekStartChange(int weekStart) { * @return 通过View的位置和周起始获取星期的对应坐标 */ protected int getViewIndexByCalendar(Calendar calendar, int weekStart) { - int week = calendar.getWeek() + 1; - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) { - return week - 1; + int week = calendar.getWeek() + 1; // 获取传入日期是周几,周日为1,周六为7 + int offset = weekStart - CalendarViewDelegate.WEEK_START_WITH_SUN; // 计算偏移量 + int index = week - offset - 1; // 计算新的索引位置 + if (index < 0) { + index += 7; // 如果索引为负数,加上7调整到正确位置 } - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) { - return week == CalendarViewDelegate.WEEK_START_WITH_SUN ? 6 : week - 2; - } - return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week; + return index % 7; // 确保索引在0到6之间 } /** @@ -127,14 +127,12 @@ protected int getViewIndexByCalendar(Calendar calendar, int weekStart) { */ private String getWeekString(int index, int weekStart) { String[] weeks = getContext().getResources().getStringArray(R.array.week_string_array); - - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) { - return weeks[index]; - } - if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) { - return weeks[index == 6 ? 0 : index + 1]; + int offset = weekStart - CalendarViewDelegate.WEEK_START_WITH_SUN; + int adjustedIndex = (index + offset) % 7; + if (adjustedIndex < 0) { + adjustedIndex += 7; } - return weeks[index == 0 ? 6 : index - 1]; + return weeks[adjustedIndex]; } @Override diff --git a/calendarview/src/main/res/values/attrs.xml b/calendarview/src/main/res/values/attrs.xml index bce063b2..e7ec7908 100644 --- a/calendarview/src/main/res/values/attrs.xml +++ b/calendarview/src/main/res/values/attrs.xml @@ -91,6 +91,10 @@ + + + +