Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Othershe committed May 15, 2017
1 parent c65d83d commit 2f3ba0c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,30 @@ calendarView.setOnMonthItemChooseListener(new OnMonthItemChooseListener() {
### CalendarView的自定义属性
namespace:xmlns:calendarview="http://schemas.android.com/apk/res-auto"

|属性名|格式|描述
|---|---|---|
|show_lunar|boolean|是否显示农历
|show_last_next|boolean|是否在MonthView显示上月和下月日期
|show_holiday|boolean|是否显示节假日
|show_term|boolean|是否显示节气
|date_start|string|日历的开始年月(例如:1990.5)
|date_end|string|日历的结束年月(例如:2025.12)
|属性名|格式|描述|默认值
|---|---|---|---|
|show_lunar|boolean|是否显示农历|true
|show_last_next|boolean|是否在MonthView显示上月和下月日期|true
|show_holiday|boolean|是否显示节假日|true
|show_term|boolean|是否显示节气|true
|date_start|string|日历的开始年月(例如:1990.5)|1900.1
|date_end|string|日历的结束年月(例如:2025.12)|2049.12
|date_init|string|日历默认展示、选中的日期(例如:2017.5.20),不设置则为当天
|disable_before|boolean|默认展示、选中的日期前的所有日期是否可用
|disable_before|boolean|是否禁用默认选中日期前的所有日期|false
|switch_choose|boolean|单选时切换月份,是否选中上次的日期|true
|color_solar|color|阳历日期的颜色
|size_solar|integer|阳历的日期尺寸
|size_solar|integer|阳历的日期尺寸|14
|color_lunar|color|农历的日期颜色
|size_lunar|integer|农历的日期尺寸
|size_lunar|integer|农历的日期尺寸|8
|color_holiday|color|节假日、节气的颜色
|color_choose|color|选中的日期颜色
|day_bg|reference|选中的日期背景(图片)

### WeekView的自定义属性
namespace:xmlns:weekview="http://schemas.android.com/apk/res-auto"

|属性名|格式|描述
|---|---|---|
|属性名|格式|描述|默认值
|---|---|---|---|
|week_str|string|周的表示形式,用点隔开(例如:日.一.二.三.四.五.六)
|week_color|color|周的颜色
|week_size|integer|周的尺寸
|week_size|integer|周的尺寸|12
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class CalendarView extends ViewPager {
private boolean showLunar = true;//是否显示农历
private boolean showHoliday = true;//是否显示节假日(不显示农历则节假日无法显示,节假日会覆盖农历显示)
private boolean showTerm = true;//是否显示节气
private boolean disableBefore = false;//默认展示、选中的日期前的所有日期是否可用
private boolean disableBefore = false;//是否禁用默认选中日期前的所有日期
private boolean switchChoose = true;//单选时切换月份,是否选中上次的日期
private int colorSolar = Color.BLACK;//阳历的日期颜色
private int colorLunar = Color.parseColor("#999999");//阴历的日期颜色
private int colorHoliday = Color.parseColor("#EC9729");//节假日的颜色
Expand All @@ -43,7 +44,7 @@ public class CalendarView extends ViewPager {
private int dayBg = R.drawable.blue_circle;//选中的背景

private int count;//ViewPager的页数
private int lastClickedDay;//上次点击的日期
private int[] lastClickDate = new int[2];//上次点击的日期
private SparseArray<HashSet<Integer>> chooseDate = new SparseArray<>();//记录多选时全部选中的日期

private CalendarPagerAdapter calendarPagerAdapter;
Expand Down Expand Up @@ -80,7 +81,9 @@ private void initAttr(Context context, AttributeSet attrs) {
} else if (attr == R.styleable.CalendarView_show_term) {
showTerm = ta.getBoolean(attr, true);
} else if (attr == R.styleable.CalendarView_disable_before) {
disableBefore = ta.getBoolean(attr, true);
disableBefore = ta.getBoolean(attr, false);
} else if (attr == R.styleable.CalendarView_switch_choose) {
switchChoose = ta.getBoolean(attr, true);
} else if (attr == R.styleable.CalendarView_color_solar) {
colorSolar = ta.getColor(attr, colorSolar);
} else if (attr == R.styleable.CalendarView_size_solar) {
Expand Down Expand Up @@ -119,7 +122,6 @@ private void initAttr(Context context, AttributeSet attrs) {
}

public void init() {
lastClickedDay = dateInit[2];
//根据设定的日期范围计算日历的页数
count = (dateEnd[0] - dateStart[0]) * 12 + dateEnd[1] - dateStart[1] + 1;
calendarPagerAdapter = new CalendarPagerAdapter(count);
Expand All @@ -133,6 +135,9 @@ public void init() {
setAdapter(calendarPagerAdapter);

currentPosition = CalendarUtil.dateToPosition(dateInit[0], dateInit[1], dateStart[0], dateStart[1]);
lastClickDate[0] = currentPosition;
lastClickDate[1] = dateInit[2];

setLastChooseDate(dateInit[2], true);//因为有默认选中日期,所以需要此操作
setCurrentItem(currentPosition, false);

Expand All @@ -143,7 +148,7 @@ public void onPageSelected(int position) {
currentPosition = position;
if (pagerChangeListener != null) {
int[] date = CalendarUtil.positionToDate(position, dateStart[0], dateStart[1]);
pagerChangeListener.onPagerChanged(new int[]{date[0], date[1], lastClickedDay});
pagerChangeListener.onPagerChanged(new int[]{date[0], date[1], lastClickDate[1]});
}
}
});
Expand Down Expand Up @@ -180,7 +185,8 @@ private void refreshMonthView(int position) {
if (chooseDate.get(position) != null)
monthView.multiChooseRefresh(chooseDate.get(position));
} else {
monthView.refresh(lastClickedDay);
boolean flag = (!switchChoose && lastClickDate[0] == position) || switchChoose;
monthView.refresh(lastClickDate[1], flag);
}
}

Expand All @@ -190,7 +196,8 @@ private void refreshMonthView(int position) {
* @param day
*/
public void setLastClickDay(int day) {
lastClickedDay = day;
lastClickDate[0] = currentPosition;
lastClickDate[1] = day;
}

/**
Expand Down Expand Up @@ -264,8 +271,9 @@ public void setOnCalendarViewAdapter(int item_layout, CalendarViewAdapter calend
* 跳转到今天
*/
public void today() {
lastClickedDay = SolarUtil.getCurrentDate()[2];
int destPosition = CalendarUtil.dateToPosition(SolarUtil.getCurrentDate()[0], SolarUtil.getCurrentDate()[1], dateStart[0], dateStart[1]);
lastClickDate[0] = destPosition;
lastClickDate[1] = SolarUtil.getCurrentDate()[2];
if (destPosition == currentPosition) {
refreshMonthView(destPosition);
} else {
Expand All @@ -281,8 +289,12 @@ public void today() {
* @param day
*/
public void toSpecifyDate(int year, int month, int day) {
lastClickedDay = day != 0 ? day : lastClickedDay;
setCurrentItem(CalendarUtil.dateToPosition(year, month, dateStart[0], dateStart[1]), false);
int destPosition = CalendarUtil.dateToPosition(year, month, dateStart[0], dateStart[1]);
if (!switchChoose && day != 0) {
lastClickDate[0] = destPosition;
}
lastClickDate[1] = day != 0 ? day : lastClickDate[1];
setCurrentItem(destPosition, false);
}

/**
Expand Down Expand Up @@ -323,14 +335,14 @@ public void nextYear() {
* 跳转到日历的开始年月
*/
public void toStart() {
toSpecifyDate(dateStart[0], dateStart[1], lastClickedDay);
toSpecifyDate(dateStart[0], dateStart[1], 0);
}

/**
* 跳转到日历的结束年月
*/
public void toEnd() {
toSpecifyDate(dateEnd[0], dateEnd[1], lastClickedDay);
toSpecifyDate(dateEnd[0], dateEnd[1], 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}

public void refresh(int day) {
View destView = findDestView(day);
if (destView == null) {
return;
}
public void refresh(int day, boolean flag) {
if (lastClickedView != null) {
setDayColor(lastClickedView, COLOR_RESET);
}
if (!flag){
return;
}
View destView = findDestView(day);
setDayColor(destView, COLOR_SET);
lastClickedView = destView;
invalidate();
Expand Down
4 changes: 3 additions & 1 deletion calendarview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<attr name="date_end" format="string" />
<!--默认展示、选中的日期(xxxx.x.x)-->
<attr name="date_init" format="string" />
<!--默认展示、选中的日期前的所有日期是否可用-->
<!--是否禁用默认选中日期前的所有日期-->
<attr name="disable_before" format="boolean" />
<!--阳历的日期颜色-->
<attr name="color_solar" format="color" />
Expand All @@ -31,6 +31,8 @@
<attr name="color_choose" format="color" />
<!--选中的日期背景-->
<attr name="day_bg" format="reference" />
<!--单选时切换月份,是否选中上次的日期-->
<attr name="switch_choose" format="boolean" />
</declare-styleable>

<declare-styleable name="WeekView">
Expand Down

0 comments on commit 2f3ba0c

Please sign in to comment.