高仿小米日历
https://github.com/wuda615/StickyCalendar
[TOC]
使用方式
导入
Gradle
compile 'com.github.wuda615:StickyCalendar-release:1.0.1'
xml添加布局
<com.github.wuda615.calendar.widget.ClpsCalendarWrapperLayout
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:calendarItemId="@layout/item_calendar_demo"
app:contentId="@+id/lv_bottom">
<ListView
android:id="@+id/lv_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
</com.github.wuda615.calendar.widget.ClpsCalendarWrapperLayout>
将要包含的滑动控件放置在
ClpsCalendarWrapperLayout
,可以是任何View,包括但不限于ListView,ScrollView,RecyclerView。对于需要使用自定义item样式或内容的日历,提供
app:calendarItemId
属性自由替换对应的布局,@layout/item_calendar_demo
对应的布局文件完全样式由用户自己定义(是否显示农历,当天是否红点标识事件等),当然不设置将使用默认的样式。将
ClpsCalendarWrapperLayout
和包含的控件联系起来app:contentId="@+id/lv_bottom"
,这个属性是必须设置,否则滑动事件无法关联。
自定义日历Item样式和内容
这里只是简单的设置日历Item样式,当天红色背景白色文字高亮,选中的日期有个蓝色圆环高亮,周二、周五都用绿色标识当天有事件
- 设置Item布局文件`item_calendar_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/calendar_item"
android:layout_width="wrap_content"
android:layout_height="50dp">
<TextView
android:id="@+id/tx_date"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:button="@null"
android:gravity="center"
android:textColor="@color/calendar_text_color"
android:textSize="15sp"
tools:background="@drawable/calendar_orange_solid"
tools:text="18" />
<ImageView
android:id="@+id/imv_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:src="@drawable/calendar_marker_green" />
</RelativeLayout>
包含一个用来显示日期的TextView
和一个标识事件的ImageView
2.Item视图更新逻辑
设置setRenderItemCallBack
更新回调,通过CommonViewHolder
和DateBeanWrapper
来判断该如何处理自己的视图显示
calendar.setRenderItemCallBack(new RenderItemCallBack() {
@Override
public void onItemRender(CommonViewHolder viewHolder, DateBeanWrapper dateBean) {
rendItemView(viewHolder, dateBean);
}
});
参考本例需求代码rendItemView(viewHolder, dateBean)
private void rendItemView(CommonViewHolder viewHolder, DateBeanWrapper dateBean) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateBean.getDate());
String text = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
viewHolder.setText(R.id.tx_date, text);
boolean isCurrentMonth = dateBean.getMonthType() == DateBeanWrapper.MONTH_TYPE_THIS;
if (DateUtils.isCheckedDay(dateBean.getDate())) {
Log.d("EventCalendarAdapter", "drawCheckedDay:" + DateUtils.formatDate(dateBean.getDate()));
if (DateUtils.isSameDay(dateBean.getDate(), DateUtils.getNow())) {
viewHolder.setBackgroundResource(R.id.tx_date, R.drawable.calendar_item_today_bg);
viewHolder.setTextColor(R.id.tx_date, ContextCompat.getColor(this, R.color.calendar_text_color_white));
} else {
viewHolder.setBackgroundResource(R.id.tx_date, R.drawable.calendar_item_checked_bg);
viewHolder.setTextColor(R.id.tx_date, ContextCompat.getColor(this, R.color.calendar_text_color));
}
} else {
viewHolder.setTextColor(R.id.tx_date, ContextCompat.getColor(this, isCurrentMonth ? R.color.calendar_text_color : R.color.calendar_text_color_disable));
viewHolder.setBackgroundColor(R.id.tx_date, ContextCompat.getColor(this, android.R.color.transparent));
}
if ((dateBean.getDayOfWeek() == 2 || dateBean.getDayOfWeek() == 5) && isCurrentMonth) {
viewHolder.setVisibility(R.id.imv_point, VISIBLE);
} else {
viewHolder.setVisibility(R.id.imv_point, GONE);
}
}
3.其他功能
设置周月模式calendar.setCalendarMode(isChecked);
跳转到制定日期calendar.jump2Day(DateUtils.getNow());