Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar committed Dec 22, 2017
1 parent 090f4a6 commit ff07156
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 37 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 9、......

#### [基本原理](http://www.jianshu.com/p/304c8e70d0bd)
#### [demo体验](https://fir.im/vehj?release_id=5a3b59f3959d691cc2000583)
#### [demo体验](https://fir.im/vehj?release_id=5a3c76cc959d695b3800035b)

### 效果图:
|![](screenshot/1.gif)|![](screenshot/2.gif)|![](screenshot/3.gif)|
Expand All @@ -32,7 +32,7 @@ allprojects {
**Step 2. 添加项目依赖**
``` gradle
dependencies {
compile 'com.github.Othershe:CalendarView:1.2.0'
compile 'com.github.Othershe:CalendarView:1.2.1'
}
```
**Step 3. 在布局文件中添加WeekView、CalendarView**
Expand All @@ -49,7 +49,7 @@ dependencies {
**Step 4. 相关初始化**
```java
CalendarView calendarView = (CalendarView) findViewById(R.id.calendar);
//日历init
//日历init,年月日之间用点号隔开
calendarView
.setStartEndDate("2010.7", "2018.12")
.setInitDate("2017.11")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ protected void onCreate(Bundle savedInstanceState) {
map.put("2017.11.11", "tgb");
calendarView
// .setSpecifyMap(map)
.setStartEndDate("2014.1", "2030.12")
.setDisableStartEndDate("2014.7.10", "2018.9.12")
.setStartEndDate("2016.1", "2028.12")
.setDisableStartEndDate("2016.10.10", "2028.10.10")
.setInitDate(cDate[0] + "." + cDate[1])
.setSingleDate("2018.9.17")
.setSingleDate(cDate[0] + "." + cDate[1] + "." + cDate[2])
.init();

// .setOnCalendarViewAdapter(R.layout.item_layout, new CalendarViewAdapter() {
Expand Down Expand Up @@ -98,10 +98,14 @@ public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "请完善日期!", Toast.LENGTH_SHORT).show();
return;
}
chooseDate.setText("当前选中的日期:" + year.getText() + "年" + month.getText() + "月" + day.getText() + "日");
calendarView.toSpecifyDate(Integer.valueOf(year.getText().toString()),
boolean result = calendarView.toSpecifyDate(Integer.valueOf(year.getText().toString()),
Integer.valueOf(month.getText().toString()),
Integer.valueOf(day.getText().toString()));
if (!result) {
Toast.makeText(MainActivity.this, "日期越界!", Toast.LENGTH_SHORT).show();
} else {
chooseDate.setText("当前选中的日期:" + year.getText() + "年" + month.getText() + "月" + day.getText() + "日");
}
dialog.dismiss();
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ protected void onCreate(Bundle savedInstanceState) {
List<String> list = new ArrayList<>();
list.add("2017.11.11");
list.add("2017.11.12");
list.add("2017.11.22");
list.add("2017.12.12");
list.add("2017.12.22");
list.add("2017.12.25");

calendarView = (CalendarView) findViewById(R.id.calendar);
calendarView
.setStartEndDate("2016.1", "2018.12")
.setDisableStartEndDate("2017.10.7", "2017.12.25")
.setStartEndDate("2017.1", "2019.12")
.setDisableStartEndDate("2017.10.7", "2019.10.7")
.setInitDate("2017.11")
.setMultiDate(list)
.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public static int[] strToArray(String str) {
return null;
}

public static long dateToMillis(int[] date) {
int day = date.length == 2 ? 1 : date[2];
Calendar calendar = Calendar.getInstance();
calendar.set(date[0], date[1], day);
return calendar.getTimeInMillis();
}

public static int getPxSize(Context context, int size) {
return size * context.getResources().getDisplayMetrics().densityDpi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.othershe.calendarview.listener.OnSingleChooseListener;
import com.othershe.calendarview.listener.OnPagerChangeListener;
import com.othershe.calendarview.utils.CalendarUtil;
import com.othershe.calendarview.utils.SolarUtil;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -34,8 +35,6 @@ public class CalendarView extends ViewPager {
private int[] initDate;//日历初始显示的年月
private int[] startDate;//日历的开始年、月
private int[] endDate;//日历的结束年、月
private int[] singleDate;//单选时默认选中的年、月、日
private List<int[]> multiDates;//多选时默认选中的年、月、日

private int count;//ViewPager的页数
private int[] lastClickDate = new int[2];//记录单选的ViewPager position以及选中的日期
Expand Down Expand Up @@ -108,20 +107,25 @@ public void init() {
currentPosition = CalendarUtil.dateToPosition(initDate[0], initDate[1], startDate[0], startDate[1]);

//单选
if (mAttrsBean.getChooseType() == 0 && singleDate != null) {
lastClickDate[0] = CalendarUtil.dateToPosition(singleDate[0], singleDate[1], startDate[0], startDate[1]);
lastClickDate[1] = singleDate[2];
if (mAttrsBean.getChooseType() == 0) {
int[] singleDate = mAttrsBean.getSingleDate();
if (singleDate != null) {
lastClickDate[0] = CalendarUtil.dateToPosition(singleDate[0], singleDate[1], startDate[0], startDate[1]);
lastClickDate[1] = singleDate[2];
}
}

//多选
if (mAttrsBean.getChooseType() == 1) {
positions = new HashSet<>();
chooseDate = new SparseArray<>();
if (multiDates != null) {
for (int[] date : multiDates) {
int datePosition = CalendarUtil.dateToPosition(date[0], date[1], startDate[0], startDate[1]);
positions.add(datePosition);
setChooseDate(date[2], true, datePosition);
if (mAttrsBean.getMultiDates() != null) {
for (int[] date : mAttrsBean.getMultiDates()) {
if (isIllegal(date)) {
int datePosition = CalendarUtil.dateToPosition(date[0], date[1], startDate[0], startDate[1]);
positions.add(datePosition);
setChooseDate(date[2], true, datePosition);
}
}
}
}
Expand Down Expand Up @@ -213,9 +217,44 @@ public void setChooseDate(int day, boolean flag, int position) {
}
}

private void checkDate(){
int[] disableStart = mAttrsBean.getDisableStartDate();
int[] disableEnd = mAttrsBean.getDisableEndDate();
/**
* 检查初始化选中的日期,或者要跳转的日期是否合法
*
* @param destDate
* @return
*/
private boolean isIllegal(int[] destDate) {

if (destDate[1] > 12 || destDate[1] < 1) {
return false;
}

if (CalendarUtil.dateToMillis(destDate) < CalendarUtil.dateToMillis(startDate)) {
return false;
}

if (CalendarUtil.dateToMillis(destDate) > CalendarUtil.dateToMillis(endDate)) {
return false;
}

if (destDate[2] > SolarUtil.getMonthDays(destDate[0], destDate[1]) || destDate[2] < 1) {
return false;
}


if (mAttrsBean.getDisableStartDate() != null) {
if (CalendarUtil.dateToMillis(destDate) < CalendarUtil.dateToMillis(mAttrsBean.getDisableStartDate())) {
return false;
}
}

if (mAttrsBean.getDisableEndDate() != null) {
if (CalendarUtil.dateToMillis(destDate) > CalendarUtil.dateToMillis(mAttrsBean.getDisableEndDate())) {
return false;
}
}

return true;
}

/**
Expand Down Expand Up @@ -286,7 +325,15 @@ public void today() {
* @param month
* @param day
*/
public void toSpecifyDate(int year, int month, int day) {
public boolean toSpecifyDate(int year, int month, int day) {
if (!isIllegal(new int[]{year, month, day})) {
return false;
}
toDestDate(year, month, day);
return true;
}

private void toDestDate(int year, int month, int day) {
int destPosition = CalendarUtil.dateToPosition(year, month, startDate[0], startDate[1]);
if (!mAttrsBean.isSwitchChoose() && day != 0) {
lastClickDate[0] = destPosition;
Expand All @@ -300,6 +347,7 @@ public void toSpecifyDate(int year, int month, int day) {
}
}


/**
* 跳转到下个月
*/
Expand Down Expand Up @@ -338,14 +386,14 @@ public void nextYear() {
* 跳转到日历的开始年月
*/
public void toStart() {
toSpecifyDate(startDate[0], startDate[1], 0);
toDestDate(startDate[0], startDate[1], 0);
}

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

/**
Expand Down Expand Up @@ -395,9 +443,12 @@ public CalendarView setStartEndDate(String startDate, String endDate) {
* @return
*/
public CalendarView setMultiDate(List<String> dates) {
multiDates = new ArrayList<>();
List<int[]> multiDates = new ArrayList<>();
for (String date : dates) {
multiDates.add(CalendarUtil.strToArray(date));
int[] d = CalendarUtil.strToArray(date);
if (isIllegal(d)) {
multiDates.add(d);
}
}
mAttrsBean.setMultiDates(multiDates);
return this;
Expand All @@ -411,7 +462,10 @@ public CalendarView setMultiDate(List<String> dates) {
* @return
*/
public CalendarView setSingleDate(String date) {
singleDate = CalendarUtil.strToArray(date);
int[] singleDate = CalendarUtil.strToArray(date);
if (!isIllegal(singleDate)) {
singleDate = null;
}
mAttrsBean.setSingleDate(singleDate);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.othershe.calendarview.listener.CalendarViewAdapter;
import com.othershe.calendarview.listener.OnMultiChooseListener;
import com.othershe.calendarview.listener.OnSingleChooseListener;
import com.othershe.calendarview.utils.CalendarUtil;

import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -168,9 +169,7 @@ public void setDateList(List<DateBean> dates, int currentMonthDays) {
if (date.getType() == 1) {
view.setTag(date.getSolar()[2]);
if (mAttrsBean.getDisableStartDate() != null
&& (date.getSolar()[0] < mAttrsBean.getDisableStartDate()[0]
|| (date.getSolar()[0] == mAttrsBean.getDisableStartDate()[0] && date.getSolar()[1] < mAttrsBean.getDisableStartDate()[1])
|| (date.getSolar()[0] == mAttrsBean.getDisableStartDate()[0] && date.getSolar()[1] == mAttrsBean.getDisableStartDate()[1] && date.getSolar()[2] < mAttrsBean.getDisableStartDate()[2]))) {
&& (CalendarUtil.dateToMillis(mAttrsBean.getDisableStartDate()) > CalendarUtil.dateToMillis(date.getSolar()))) {
solarDay.setTextColor(mAttrsBean.getColorLunar());
lunarDay.setTextColor(mAttrsBean.getColorLunar());
view.setTag(-1);
Expand All @@ -179,9 +178,7 @@ public void setDateList(List<DateBean> dates, int currentMonthDays) {
}

if (mAttrsBean.getDisableEndDate() != null
&& (date.getSolar()[0] > mAttrsBean.getDisableEndDate()[0]
|| (date.getSolar()[0] == mAttrsBean.getDisableEndDate()[0] && date.getSolar()[1] > mAttrsBean.getDisableEndDate()[1])
|| (date.getSolar()[0] == mAttrsBean.getDisableEndDate()[0] && date.getSolar()[1] == mAttrsBean.getDisableEndDate()[1] && date.getSolar()[2] > mAttrsBean.getDisableEndDate()[2]))) {
&& (CalendarUtil.dateToMillis(mAttrsBean.getDisableEndDate()) < CalendarUtil.dateToMillis(date.getSolar()))) {
solarDay.setTextColor(mAttrsBean.getColorLunar());
lunarDay.setTextColor(mAttrsBean.getColorLunar());
view.setTag(-1);
Expand Down

0 comments on commit ff07156

Please sign in to comment.