Skip to content

Commit

Permalink
支持任意日期范围限制、支持多个新特性
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghaibin committed Aug 23, 2018
1 parent 731dbe0 commit 42c86b9
Show file tree
Hide file tree
Showing 31 changed files with 1,383 additions and 315 deletions.
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Android上一个优雅、高度自定义、性能高效的日历控件,完美

### Gradle
```
compile 'com.haibin:calendarview:3.4.2'
compile 'com.haibin:calendarview:3.4.3'
```
```
<dependency>
<groupId>com.haibin</groupId>
<artifactId>calendarview</artifactId>
<version>3.4.2</version>
<version>3.4.3</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -214,7 +214,8 @@ app:week_bar_view="com.haibin.calendarviewproject.EnglishWeekBar"
### CalendarView api
```java

public void setRange(int minYear, int minYearMonth, int maxYear, int maxYearMonth);//置日期范围
public void setRange(int minYear, int minYearMonth, int minYearDay,
int maxYear, int maxYearMonth, int maxYearDay) ;//置日期范围

public int getCurDay(); //今天
public int getCurMonth(); //当前的月份
Expand All @@ -228,24 +229,14 @@ public void setOnMonthChangeListener(OnMonthChangeListener listener);//月份改

public void setOnYearChangeListener(OnYearChangeListener listener);//年份切换事件

public void setOnDateSelectedListener(OnDateSelectedListener listener);//日期选择事件
public void setOnCalendarSelectListener(OnCalendarSelectListener listener)//日期选择事件

public void setOnDateLongClickListener(OnDateLongClickListener listener);//日期长按事件
public void setOnCalendarLongClickListener(OnCalendarLongClickListener listener);//日期长按事件

public void setOnCalendarInterceptListener(OnCalendarInterceptListener listener);//日期拦截和日期有效性绘制
public void setOnCalendarLongClickListener(OnCalendarLongClickListener listener, boolean preventLongPressedSelect);//日期长按事件

@Deprecated
public void setSchemeDate(List<Calendar> mSchemeDate);//标记日期
public void setOnCalendarInterceptListener(OnCalendarInterceptListener listener);//日期拦截和日期有效性绘制

/**
* 标记哪些日期有事件
* 如果标记的日期数量很大,mSchemeDatesMap.size()>10000?,请使用这个
* key=Calendar.toString();
* 优势?Android的用户相应时间一旦大于16ms,UI就会卡顿,Map在数据增长量巨大时,查找性能上不会损耗,
* List的性能就会差很大,List.contains()会遍历整个数组,性能太差
*
* @param mSchemeDates mSchemeDatesMap 通过自己的需求转换即可
*/
public void setSchemeDate(Map<String, Calendar> mSchemeDates);//标记日期

public void update();//动态更新
Expand All @@ -267,6 +258,10 @@ public void scrollToNext();//滚动到下一个月

public void scrollToCalendar(int year, int month, int day);//滚动到指定日期

public Calendar getMinRangeCalendar();//获得最小范围日期

public Calendar getMaxRangeCalendar();//获得最大范围日期

/**
* 设置背景色
*
Expand Down
20 changes: 12 additions & 8 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ This widget has obvious advantages of memory and efficiency, adapt to many scene

### Gradle
```
compile 'com.haibin:calendarview:3.4.2'
compile 'com.haibin:calendarview:3.4.3'
```
```
<dependency>
<groupId>com.haibin</groupId>
<artifactId>calendarview</artifactId>
<version>3.4.2</version>
<version>3.4.3</version>
<type>pom</type>
</dependency>
```
Expand Down Expand Up @@ -207,7 +207,8 @@ app:week_bar_view="com.haibin.calendarviewproject.EnglishWeekBar"
### CalendarView api
```java

public void setRange(int minYear, int minYearMonth, int maxYear, int maxYearMonth);//date range
public void setRange(int minYear, int minYearMonth, int minYearDay,
int maxYear, int maxYearMonth, int maxYearDay) ;//date range

public int getCurDay(); //get today
public int getCurMonth(); //get currentMonth
Expand All @@ -221,22 +222,25 @@ public void setOnMonthChangeListener(OnMonthChangeListener listener);//month cha

public void setOnYearChangeListener(OnYearChangeListener listener);//year change listener

public void setOnDateSelectedListener(OnDateSelectedListener listener);//date selected listener
public void setOnCalendarSelectListener(OnCalendarSelectListener listener)//date selected listener

public void setOnDateLongClickListener(OnDateLongClickListener listener);//date long click listener
public void setOnCalendarLongClickListener(OnCalendarLongClickListener listener);//date long click listener

public void setOnCalendarLongClickListener(OnCalendarLongClickListener listener, boolean preventLongPressedSelect);//date long click listener

public void setOnCalendarInterceptListener(OnCalendarInterceptListener listener);//Intercept calendar click and Available draw

@Deprecated
public void setSchemeDate(List<Calendar> mSchemeDate);//mark dates
public Calendar getMinRangeCalendar();//getMinRangeCalendar

public Calendar getMaxRangeCalendar();//getMaxRangeCalendar

/**
* mark dates
* when mSchemeDatesMap.size()>10000?,use this
* key=Calendar.toString();
* best performance
*
* @param mSchemeDates mSchemeDatesMap 通过自己的需求转换即可
* @param mSchemeDates mSchemeDatesMap
*/
public void setSchemeDate(Map<String, Calendar> mSchemeDates);//mark dates

Expand Down
91 changes: 47 additions & 44 deletions app/src/main/java/com/haibin/calendarviewproject/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
import com.haibin.calendarviewproject.single.SingleActivity;
import com.haibin.calendarviewproject.solay.SolarActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity implements
CalendarView.OnDateSelectedListener,
CalendarView.OnCalendarSelectListener,
CalendarView.OnCalendarLongClickListener,
CalendarView.OnMonthChangeListener,
CalendarView.OnYearChangeListener,
CalendarView.OnDateLongClickListener,
CalendarView.OnWeekChangeListener,
CalendarView.OnViewChangeListener,
CalendarView.OnCalendarInterceptListener,
DialogInterface.OnClickListener,
Expand Down Expand Up @@ -93,7 +93,8 @@ public void onClick(View v) {
public void onClick(View v) {
//mCalendarView.scrollToCurrent();
//mCalendarView.scrollToCalendar(2018,7,14);
Log.e("scrollToCurrent", " -- " + mCalendarView.getSelectedCalendar());
//Log.e("scrollToCurrent", " -- " + mCalendarView.getSelectedCalendar());

}
});
findViewById(R.id.iv_more).setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -128,7 +129,23 @@ public void onClick(DialogInterface dialog, int which) {
break;
case 4:
mCalendarView.scrollToCurrent(true);
//mCalendarView.scrollToCalendar(2018,7,14);
break;
case 5:
mCalendarView.setRange(mCalendarView.getCurYear(), mCalendarView.getCurMonth(), 6,
mCalendarView.getCurYear(), mCalendarView.getCurMonth(), 23);
break;
case 6:
Log.e("scheme", " " + mCalendarView.getSelectedCalendar().getScheme() + " -- "
+ mCalendarView.getSelectedCalendar().isCurrentDay());
List<Calendar> weekCalendars = mCalendarView.getCurrentWeekCalendars();
for (Calendar calendar : weekCalendars) {
Log.e("onWeekChange", calendar.toString() + " -- " + calendar.getScheme());
}
new AlertDialog.Builder(MainActivity.this)
.setMessage(String.format("Calendar Range: \n%s —— %s",
mCalendarView.getMinRangeCalendar(),
mCalendarView.getMaxRangeCalendar()))
.show();
break;
}
}
Expand All @@ -149,9 +166,10 @@ public void onClick(View v) {

mCalendarLayout = (CalendarLayout) findViewById(R.id.calendarLayout);
mCalendarView.setOnYearChangeListener(this);
mCalendarView.setOnDateSelectedListener(this);
mCalendarView.setOnCalendarSelectListener(this);
mCalendarView.setOnMonthChangeListener(this);
mCalendarView.setOnDateLongClickListener(this, true);
mCalendarView.setOnCalendarLongClickListener(this, true);
mCalendarView.setOnWeekChangeListener(this);

//设置日期拦截事件,仅适用单选模式,当前无效
mCalendarView.setOnCalendarInterceptListener(this);
Expand All @@ -167,7 +185,7 @@ public void onClick(View v) {
@SuppressWarnings("unused")
@Override
protected void initData() {
final List<Calendar> schemes = new ArrayList<>();

final int year = mCalendarView.getCurYear();
final int month = mCalendarView.getCurMonth();

Expand Down Expand Up @@ -233,38 +251,6 @@ protected void initData() {
}
}

for (int y = 1997; y < 2082; y++) {
for (int m = 1; m <= 12; m++) {
schemes.add(getSchemeCalendar(y, m, 1, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 2, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 3, 0xFFe69138, "假"));
schemes.add(getSchemeCalendar(y, m, 4, 0xFFe69138, "假"));
schemes.add(getSchemeCalendar(y, m, 5, 0xFFdf1356, "假"));
schemes.add(getSchemeCalendar(y, m, 6, 0xFFdf1356, "假"));
schemes.add(getSchemeCalendar(y, m, 7, 0xFFaacc44, "事"));
schemes.add(getSchemeCalendar(y, m, 8, 0xFFaacc44, "议"));
schemes.add(getSchemeCalendar(y, m, 9, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 10, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 11, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 12, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 13, 0xFFedc56d, "记"));
schemes.add(getSchemeCalendar(y, m, 14, 0xFFedc56d, "记"));
schemes.add(getSchemeCalendar(y, m, 15, 0xFFaacc44, "假"));
schemes.add(getSchemeCalendar(y, m, 16, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 17, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 18, 0xFFbc13f0, "记"));
schemes.add(getSchemeCalendar(y, m, 19, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 20, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 21, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 22, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 23, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 24, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 25, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 26, 0xFF13acf0, "假"));
schemes.add(getSchemeCalendar(y, m, 37, 0xFF40db25, "假"));
schemes.add(getSchemeCalendar(y, m, 38, 0xFF40db25, "假"));
}
}
//28560 数据量增长不会影响UI响应速度,请使用这个API替换
mCalendarView.setSchemeDate(map);

Expand Down Expand Up @@ -324,8 +310,6 @@ public void onClick(View v) {
switch (v.getId()) {
case R.id.ll_flyme:
MeiZuActivity.show(this);
Log.e("scheme", " " + mCalendarView.getSelectedCalendar().getScheme() + " -- "
+ mCalendarView.getSelectedCalendar().isCurrentDay());
break;
case R.id.ll_custom:
CustomActivity.show(this);
Expand Down Expand Up @@ -365,9 +349,15 @@ private Calendar getSchemeCalendar(int year, int month, int day, int color, Stri
return calendar;
}


@Override
public void onCalendarOutOfRange(Calendar calendar) {
Toast.makeText(this, String.format("%s : OutOfRange", calendar), Toast.LENGTH_SHORT).show();
}

@SuppressLint("SetTextI18n")
@Override
public void onDateSelected(Calendar calendar, boolean isClick) {
public void onCalendarSelect(Calendar calendar, boolean isClick) {
//Log.e("onDateSelected", " -- " + calendar.getYear() + " -- " + calendar.getMonth() + " -- " + calendar.getDay());
mTextLunar.setVisibility(View.VISIBLE);
mTextYear.setVisibility(View.VISIBLE);
Expand All @@ -387,7 +377,12 @@ public void onDateSelected(Calendar calendar, boolean isClick) {
}

@Override
public void onDateLongClick(Calendar calendar) {
public void onCalendarLongClickOutOfRange(Calendar calendar) {
Toast.makeText(this, String.format("%s : LongClickOutOfRange", calendar), Toast.LENGTH_SHORT).show();
}

@Override
public void onCalendarLongClick(Calendar calendar) {
Toast.makeText(this, "长按不选择日期\n" + getCalendarText(calendar), Toast.LENGTH_SHORT).show();
}

Expand Down Expand Up @@ -419,6 +414,14 @@ public void onViewChange(boolean isMonthView) {
Log.e("onViewChange", " --- " + (isMonthView ? "月视图" : "周视图"));
}


@Override
public void onWeekChange(List<Calendar> weekCalendars) {
for (Calendar calendar : weekCalendars) {
Log.e("onWeekChange", calendar.toString());
}
}

/**
* 屏蔽某些不可点击的日期,可根据自己的业务自行修改
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;

public class ColorfulActivity extends BaseActivity implements
CalendarView.OnDateSelectedListener,
CalendarView.OnCalendarSelectListener,
CalendarView.OnYearChangeListener,
View.OnClickListener {

Expand Down Expand Up @@ -86,7 +86,7 @@ public void onClick(View v) {
}
});
mCalendarLayout = (CalendarLayout) findViewById(R.id.calendarLayout);
mCalendarView.setOnDateSelectedListener(this);
mCalendarView.setOnCalendarSelectListener(this);
mCalendarView.setOnYearChangeListener(this);
mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
mYear = mCalendarView.getCurYear();
Expand Down Expand Up @@ -179,9 +179,14 @@ private Calendar getSchemeCalendar(int year, int month, int day, int color, Stri



@Override
public void onCalendarOutOfRange(Calendar calendar) {

}

@SuppressLint("SetTextI18n")
@Override
public void onDateSelected(Calendar calendar, boolean isClick) {
public void onCalendarSelect(Calendar calendar, boolean isClick) {
mTextLunar.setVisibility(View.VISIBLE);
mTextYear.setVisibility(View.VISIBLE);
mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
Expand All @@ -190,7 +195,6 @@ public void onDateSelected(Calendar calendar, boolean isClick) {
mYear = calendar.getYear();
}


@Override
public void onYearChange(int year) {
mTextMonthDay.setText(String.valueOf(year));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;

public class CustomActivity extends BaseActivity implements
CalendarView.OnDateSelectedListener,
CalendarView.OnCalendarSelectListener,
CalendarView.OnYearChangeListener,
View.OnClickListener {

Expand Down Expand Up @@ -86,7 +86,7 @@ public void onClick(View v) {
}
});
mCalendarLayout = (CalendarLayout) findViewById(R.id.calendarLayout);
mCalendarView.setOnDateSelectedListener(this);
mCalendarView.setOnCalendarSelectListener(this);
mCalendarView.setOnYearChangeListener(this);
mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
mYear = mCalendarView.getCurYear();
Expand Down Expand Up @@ -181,10 +181,14 @@ private Calendar getSchemeCalendar(int year, int month, int day, int color, Stri
return calendar;
}

@Override
public void onCalendarOutOfRange(Calendar calendar) {

}

@SuppressLint("SetTextI18n")
@Override
public void onDateSelected(Calendar calendar, boolean isClick) {
public void onCalendarSelect(Calendar calendar, boolean isClick) {
mTextLunar.setVisibility(View.VISIBLE);
mTextYear.setVisibility(View.VISIBLE);
mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
Expand All @@ -193,7 +197,6 @@ public void onDateSelected(Calendar calendar, boolean isClick) {
mYear = calendar.getYear();
}


@Override
public void onYearChange(int year) {
mTextMonthDay.setText(String.valueOf(year));
Expand Down
Loading

0 comments on commit 42c86b9

Please sign in to comment.