Skip to content

Commit c9d04ab

Browse files
committed
Support any week day start.
1 parent baea535 commit c9d04ab

File tree

9 files changed

+70
-86
lines changed

9 files changed

+70
-86
lines changed

app/src/main/java/com/haibin/calendarviewproject/EnglishWeekBar.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ protected void onWeekStartChange(int weekStart) {
5454
*/
5555
private String getWeekString(int index, int weekStart) {
5656
String[] weeks = getContext().getResources().getStringArray(R.array.english_week_string_array);
57-
58-
if (weekStart == 1) {
59-
return weeks[index];
60-
}
61-
if (weekStart == 2) {
62-
return weeks[index == 6 ? 0 : index + 1];
57+
int offset = weekStart - java.util.Calendar.SUNDAY;
58+
int adjustedIndex = (index + offset) % 7;
59+
if (adjustedIndex < 0) {
60+
adjustedIndex += 7;
6361
}
64-
return weeks[index == 0 ? 6 : index - 1];
62+
return weeks[adjustedIndex];
6563
}
6664

6765
/**

app/src/main/java/com/haibin/calendarviewproject/custom/CustomWeekBar.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ protected void onWeekStartChange(int weekStart) {
5252
*/
5353
private String getWeekString(int index, int weekStart) {
5454
String[] weeks = getContext().getResources().getStringArray(R.array.chinese_week_string_array);
55-
56-
if (weekStart == 1) {
57-
return weeks[index];
58-
}
59-
if (weekStart == 2) {
60-
return weeks[index == 6 ? 0 : index + 1];
55+
int offset = weekStart - java.util.Calendar.SUNDAY;
56+
int adjustedIndex = (index + offset) % 7;
57+
if (adjustedIndex < 0) {
58+
adjustedIndex += 7;
6159
}
62-
return weeks[index == 0 ? 6 : index - 1];
60+
return weeks[adjustedIndex];
6361
}
6462
}

app/src/main/java/com/haibin/calendarviewproject/mix/MixWeekBar.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ protected void onWeekStartChange(int weekStart) {
5252
*/
5353
private String getWeekString(int index, int weekStart) {
5454
String[] weeks = getContext().getResources().getStringArray(R.array.chinese_week_string_array);
55-
56-
if (weekStart == 1) {
57-
return weeks[index];
58-
}
59-
if (weekStart == 2) {
60-
return weeks[index == 6 ? 0 : index + 1];
55+
int offset = weekStart - java.util.Calendar.SUNDAY;
56+
int adjustedIndex = (index + offset) % 7;
57+
if (adjustedIndex < 0) {
58+
adjustedIndex += 7;
6159
}
62-
return weeks[index == 0 ? 6 : index - 1];
60+
return weeks[adjustedIndex];
6361
}
6462
}

app/src/main/java/com/haibin/calendarviewproject/solay/SolarWeekBar.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ protected void onWeekStartChange(int weekStart) {
4141
*/
4242
private String getWeekString(int index, int weekStart) {
4343
String[] weeks = getContext().getResources().getStringArray(R.array.english_week_string_array);
44-
45-
if (weekStart == 1) {
46-
return weeks[index];
47-
}
48-
if (weekStart == 2) {
49-
return weeks[index == 6 ? 0 : index + 1];
44+
int offset = weekStart - java.util.Calendar.SUNDAY;
45+
int adjustedIndex = (index + offset) % 7;
46+
if (adjustedIndex < 0) {
47+
adjustedIndex += 7;
5048
}
51-
return weeks[index == 0 ? 6 : index - 1];
49+
return weeks[adjustedIndex];
5250
}
5351
}

calendarview/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ dependencies {
4242
implementation 'androidx.appcompat:appcompat:1.2.0'
4343
implementation 'androidx.recyclerview:recyclerview:1.1.0'
4444
testImplementation 'junit:junit:4.12'
45+
46+
compileOnly("androidx.viewpager:viewpager:1.0.0")
4547
}
4648
apply from: '../script/gradle-jcenter-push.gradle'

calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,13 @@ public static Calendar getNextCalendar(Calendar calendar) {
206206
static int getMonthViewStartDiff(Calendar calendar, int weekStart) {
207207
java.util.Calendar date = java.util.Calendar.getInstance();
208208
date.set(calendar.getYear(), calendar.getMonth() - 1, 1, 12, 0, 0);
209-
int week = date.get(java.util.Calendar.DAY_OF_WEEK);
210-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) {
211-
return week - 1;
212-
}
213-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) {
214-
return week == 1 ? 6 : week - weekStart;
209+
int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取该月第一天是星期几
210+
int diff = week - weekStart; // 计算与周起始日的差值
211+
if (diff < 0) {
212+
// 如果差值小于0,说明周起始日在该月第一天之后,需要调整偏移量
213+
diff += 7;
215214
}
216-
return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week;
215+
return diff;
217216
}
218217

219218

@@ -230,14 +229,13 @@ static int getMonthViewStartDiff(Calendar calendar, int weekStart) {
230229
static int getMonthViewStartDiff(int year, int month, int weekStart) {
231230
java.util.Calendar date = java.util.Calendar.getInstance();
232231
date.set(year, month - 1, 1, 12, 0, 0);
233-
int week = date.get(java.util.Calendar.DAY_OF_WEEK);
234-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) {
235-
return week - 1;
232+
int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取该月第一天是星期几
233+
int diff = week - weekStart; // 计算与周起始日的差值
234+
if (diff < 0) {
235+
// 如果差值小于0,说明周起始日在该月第一天之后,需要调整偏移量
236+
diff += 7;
236237
}
237-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) {
238-
return week == 1 ? 6 : week - weekStart;
239-
}
240-
return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week;
238+
return diff;
241239
}
242240

243241

@@ -269,14 +267,12 @@ static int getMonthEndDiff(int year, int month, int weekStart) {
269267
private static int getMonthEndDiff(int year, int month, int day, int weekStart) {
270268
java.util.Calendar date = java.util.Calendar.getInstance();
271269
date.set(year, month - 1, day);
272-
int week = date.get(java.util.Calendar.DAY_OF_WEEK);
273-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) {
274-
return 7 - week;
270+
int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取给定日期是星期几
271+
int diff = week - weekStart; // 计算当前星期与周起始日的差异
272+
if (diff < 0) {
273+
diff += 7; // 如果差异为负数,说明跨越了周末,需要加7
275274
}
276-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) {
277-
return week == 1 ? 0 : 7 - week + 1;
278-
}
279-
return week == 7 ? 6 : 7 - week - 1;
275+
return 6 - diff; // 从当前星期到周末的天数差,总共7天,所以用6减去差异
280276
}
281277

282278
/**
@@ -691,15 +687,13 @@ static List<Calendar> initCalendarForWeekView(Calendar calendar, CalendarViewDel
691687
*/
692688
private static int getWeekViewStartDiff(int year, int month, int day, int weekStart) {
693689
java.util.Calendar date = java.util.Calendar.getInstance();
694-
date.set(year, month - 1, day, 12, 0);//
695-
int week = date.get(java.util.Calendar.DAY_OF_WEEK);
696-
if (weekStart == 1) {
697-
return week - 1;
698-
}
699-
if (weekStart == 2) {
700-
return week == 1 ? 6 : week - weekStart;
690+
date.set(year, month - 1, day, 12, 0); // 设置给定的日期
691+
int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取这一天是周几
692+
int diff = week - weekStart; // 计算与周起始日的差值
693+
if (diff < 0) {
694+
diff += 7; // 如果差值为负,说明给定日期在周起始日之前,需要加7天
701695
}
702-
return week == 7 ? 0 : week;
696+
return diff;
703697
}
704698

705699

@@ -717,15 +711,13 @@ private static int getWeekViewStartDiff(int year, int month, int day, int weekSt
717711
*/
718712
public static int getWeekViewEndDiff(int year, int month, int day, int weekStart) {
719713
java.util.Calendar date = java.util.Calendar.getInstance();
720-
date.set(year, month - 1, day, 12, 0);
721-
int week = date.get(java.util.Calendar.DAY_OF_WEEK);
722-
if (weekStart == 1) {
723-
return 7 - week;
724-
}
725-
if (weekStart == 2) {
726-
return week == 1 ? 0 : 7 - week + 1;
714+
date.set(year, month - 1, day, 12, 0); // 设置日期
715+
int week = date.get(java.util.Calendar.DAY_OF_WEEK); // 获取周几
716+
int diff = week - weekStart; // 计算与周起始日的差值
717+
if (diff < 0) {
718+
diff += 7; // 如果差值为负,说明给定日期在周起始日之前,需要加7天
727719
}
728-
return week == 7 ? 6 : 7 - week - 1;
720+
return 6 - diff; // 返回到周结束的天数差异
729721
}
730722

731723

calendarview/src/main/java/com/haibin/calendarview/CalendarView.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,16 +1537,12 @@ public void setWeekStarWithSat() {
15371537

15381538
/**
15391539
* 设置周起始
1540-
* CalendarViewDelegate.WEEK_START_WITH_SUN
1541-
* CalendarViewDelegate.WEEK_START_WITH_MON
1542-
* CalendarViewDelegate.WEEK_START_WITH_SAT
1540+
* 支持周一到周日任意周起始
15431541
*
15441542
* @param weekStart 周起始
15451543
*/
1546-
private void setWeekStart(int weekStart) {
1547-
if (weekStart != CalendarViewDelegate.WEEK_START_WITH_SUN &&
1548-
weekStart != CalendarViewDelegate.WEEK_START_WITH_MON &&
1549-
weekStart != CalendarViewDelegate.WEEK_START_WITH_SAT)
1544+
public void setWeekStart(int weekStart) {
1545+
if (!(java.util.Calendar.SUNDAY <= weekStart && weekStart <= java.util.Calendar.SATURDAY))
15501546
return;
15511547
if (weekStart == mDelegate.getWeekStart())
15521548
return;

calendarview/src/main/java/com/haibin/calendarview/WeekBar.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.haibin.calendarview;
1717

1818
import android.content.Context;
19+
import android.util.Log;
1920
import android.util.TypedValue;
2021
import android.view.LayoutInflater;
2122
import android.widget.LinearLayout;
@@ -108,14 +109,13 @@ protected void onWeekStartChange(int weekStart) {
108109
* @return 通过View的位置和周起始获取星期的对应坐标
109110
*/
110111
protected int getViewIndexByCalendar(Calendar calendar, int weekStart) {
111-
int week = calendar.getWeek() + 1;
112-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) {
113-
return week - 1;
112+
int week = calendar.getWeek() + 1; // 获取传入日期是周几,周日为1,周六为7
113+
int offset = weekStart - CalendarViewDelegate.WEEK_START_WITH_SUN; // 计算偏移量
114+
int index = week - offset - 1; // 计算新的索引位置
115+
if (index < 0) {
116+
index += 7; // 如果索引为负数,加上7调整到正确位置
114117
}
115-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) {
116-
return week == CalendarViewDelegate.WEEK_START_WITH_SUN ? 6 : week - 2;
117-
}
118-
return week == CalendarViewDelegate.WEEK_START_WITH_SAT ? 0 : week;
118+
return index % 7; // 确保索引在0到6之间
119119
}
120120

121121
/**
@@ -127,14 +127,12 @@ protected int getViewIndexByCalendar(Calendar calendar, int weekStart) {
127127
*/
128128
private String getWeekString(int index, int weekStart) {
129129
String[] weeks = getContext().getResources().getStringArray(R.array.week_string_array);
130-
131-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_SUN) {
132-
return weeks[index];
133-
}
134-
if (weekStart == CalendarViewDelegate.WEEK_START_WITH_MON) {
135-
return weeks[index == 6 ? 0 : index + 1];
130+
int offset = weekStart - CalendarViewDelegate.WEEK_START_WITH_SUN;
131+
int adjustedIndex = (index + offset) % 7;
132+
if (adjustedIndex < 0) {
133+
adjustedIndex += 7;
136134
}
137-
return weeks[index == 0 ? 6 : index - 1];
135+
return weeks[adjustedIndex];
138136
}
139137

140138
@Override

calendarview/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
<attr name="week_start_with">
9292
<enum name="sun" value="1" />
9393
<enum name="mon" value="2" />
94+
<enum name="tue" value="3" />
95+
<enum name="wed" value="4" />
96+
<enum name="thu" value="5" />
97+
<enum name="fri" value="6" />
9498
<enum name="sat" value="7" />
9599
</attr>
96100

0 commit comments

Comments
 (0)