水平展示日历控件:HorizontalCalendar
HorizontalCalendar
该库是一个水平展示日历的控件,也是通过RecycerView来实现的。
效果如下:

配置
模块中 build.gradle:
repositories {
jcenter()
}
dependencies {
compile 'devs.mulham.horizontalcalendar:horizontalcalendar:1.3.4'
}使用
- 添加
HorizontalCalendarView到你的layout
<android.support.design.widget.AppBarLayout>
............
<devs.mulham.horizontalcalendar.HorizontalCalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:textColorSelected="#FFFF"/>
android.support.design.widget.AppBarLayout>定义你的开始和结束设置日历范围的日期:
/* starts before 1 month from now */
Calendar startDate = Calendar.getInstance();
startDate.add(Calendar.MONTH, -1);
/* ends after 1 month from now */
Calendar endDate = Calendar.getInstance();
endDate.add(Calendar.MONTH, 1);- 可以用建造者模式构建
HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(5)
.build();- Fragment中使用:
HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder(rootView, R.id.calendarView)
...................- 监听日期改变监听器
horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
@Override
public void onDateSelected(Calendar date, int position) {
//do something
}
});- 监听滑动和长按
horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
@Override
public void onDateSelected(Calendar date, int position) {
}
@Override
public void onCalendarScroll(HorizontalCalendarView calendarView,
int dx, int dy) {
}
@Override
public boolean onDateLongClicked(Calendar date, int position) {
return true;
}
});定制
- layout:
<devs.mulham.horizontalcalendar.HorizontalCalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:textColorNormal="#bababa"
app:textColorSelected="#FFFF"
app:selectorColor="#c62828" //default to colorAccent
app:selectedDateBackground="@drawable/myDrawable"/>- Activity 或者 Fragment 使用
HorizontalCalendar.Builder:
HorizontalCalendar horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
.range(Calendar startDate, Calendar endDate)
.datesNumberOnScreen(int number) // Number of Dates cells shown on screen (default to 5).
.configure() // starts configuration.
.formatTopText(String dateFormat) // default to "MMM".
.formatMiddleText(String dateFormat) // default to "dd".
.formatBottomText(String dateFormat) // default to "EEE".
.showTopText(boolean show) // show or hide TopText (default to true).
.showBottomText(boolean show) // show or hide BottomText (default to true).
.textColor(int normalColor, int selectedColor) // default to (Color.LTGRAY, Color.WHITE).
.selectedDateBackground(Drawable background) // set selected date cell background.
.selectorColor(int color) // set selection indicator bar's color (default to colorAccent).
.end() // ends configuration.
.defaultSelectedDate(Calendar date) // Date to be selected at start (default to current day `Calendar.getInstance()`).
.build();更多的自定义
builder.configure()
.textSize(float topTextSize, float middleTextSize, float bottomTextSize)
.sizeTopText(float size)
.sizeMiddleText(float size)
.sizeBottomText(float size)
.colorTextTop(int normalColor, int selectedColor)
.colorTextMiddle(int normalColor, int selectedColor)
.colorTextBottom(int normalColor, int selectedColor)
.end()月份 模式
水平日历只能显示月 添加模式(HorizontalCalendar.mode.MONTHS)例如:
horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
.range(Calendar startDate, Calendar endDate)
.datesNumberOnScreen(int number)
.mode(HorizontalCalendar.Mode.MONTHS)
.configure()
.formatMiddleText("MMM")
.formatBottomText("yyyy")
.showTopText(false)
.showBottomText(true)
.textColor(Color.LTGRAY, Color.WHITE)
.end()
.defaultSelectedDate(defaultSelectedDate)事件
可以为每个日期提供事件列表,这些事件将在日期下用圆圈表示:
builder.addEvents(new CalendarEventsPredicate() {
@Override
public List<CalendarEvent> events(Calendar date) {
// test the date and return a list of CalendarEvent to assosiate with this Date.
}
})重新配置
初始化后可以更改水平日历配置:
更改日历日期范围:
horizontalCalendar.setRange(Calendar startDate, Calendar endDate);更改默认(未选定)项目样式:
horizontalCalendar.getDefaultStyle()
.setColorTopText(int color)
.setColorMiddleText(int color)
.setColorBottomText(int color)
.setBackground(Drawable background); - 改变选中样式
horizontalCalendar.getSelectedItemStyle()
.setColorTopText(int color)
..............更改格式、文本大小和选择器颜色:
horizontalCalendar.getConfig()
.setSelectorColor(int color)
.setFormatTopText(String format)
.setSizeTopText(float size)
..............重要的
一定要调用horizontalCalendar.refresh();完成更改后
特征
禁用特定HorizontalCalendarPredicate, 也可以使用指定禁用日期的唯一样式CalendarItemStyle:
builder.disableDates(new HorizontalCalendarPredicate() {
@Override
public boolean test(Calendar date) {
return false; // return true if this date should be disabled, false otherwise.
}
@Override
public CalendarItemStyle style() {
return null; // create and return a new Style for disabled dates, or null if no styling needed.
}
})选择特定的日期通过编程方式选择是否播放动画:
horizontalCalendar.selectDate(Calendar date, boolean immediate); // set immediate to false to ignore animation.
// or simply
horizontalCalendar.goToday(boolean immediate);检查日历中是否包含日期:
horizontalCalendar.contains(Calendar date);检查两个日期是否相等(年、月、日):
Utils.isSameDate(Calendar date1, Calendar date2);获取天两个日期之间:
Utils.daysBetween(Calendar startInclusive, Calendar endExclusive);