高仿小米日歷
https://github.com/wuda615/StickyCalendar
[TOC]
使用方式
導(dǎo)入
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樣式或內(nèi)容的日歷克懊,提供
app:calendarItemId
屬性自由替換對應(yīng)的布局秸妥,@layout/item_calendar_demo
對應(yīng)的布局文件完全樣式由用戶自己定義(是否顯示農(nóng)歷,當(dāng)天是否紅點標(biāo)識事件等)榆芦,當(dāng)然不設(shè)置將使用默認的樣式柄粹。將
ClpsCalendarWrapperLayout
和包含的控件聯(lián)系起來app:contentId="@+id/lv_bottom"
,這個屬性是必須設(shè)置匆绣,否則滑動事件無法關(guān)聯(lián)驻右。
自定義日歷Item樣式和內(nèi)容
這里只是簡單的設(shè)置日歷Item樣式,當(dāng)天紅色背景白色文字高亮崎淳,選中的日期有個藍色圓環(huán)高亮堪夭,周二、周五都用綠色標(biāo)識當(dāng)天有事件
- 設(shè)置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
和一個標(biāo)識事件的ImageView
2.Item視圖更新邏輯
設(shè)置setRenderItemCallBack
更新回調(diào)凯力,通過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.其他功能
設(shè)置周月模式calendar.setCalendarMode(isChecked);
跳轉(zhuǎn)到制定日期calendar.jump2Day(DateUtils.getNow());