最近找了几个日历的控件,忙活半天发现有的不是很好用比如CompactCalendarView,发现EasyCalendar还可以,应该挺好用的。使用也很方便
1、添加依赖
https://github.com/shichaohui/EasyCalendar
compile 'com.sch.calendar:easy-calendar:1.0.0'
2、布局
<com.sch.calendar.CalendarView
android:id="@+id/easy_calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/checkin_calendar_bg"
android:padding="10dp"
app:titleColor="@color/colorPrimaryDark"
app:monthBackground="@drawable/checkin_calendar_month_bg"
app:weekBackground="#FFFFFF"
app:weekColor="#000000"
>
com.sch.calendar.CalendarView>
3、使用
(1)初始化
private void initCalendar() {
mCalendarView.setCanDrag(true);//可拖动
mCalendarView.setScaleEnable(false);//不可伸缩
mCalendarView.setShowOverflowDate(true);
mCalendarView.setCanFling(true);
// mCalendarView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
mCalendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
@Override
public void onMonthChanged(Date date) {
//月份变化
}
});
mCalendarView.setOnDateClickedListener(new OnDateClickedListener() {
@Override
public void onDateClicked(View itemView, int year, int month, int dayOfMonth) {
//日期点击,可以弹出日期或者是否签到,看需求
}
});
//adapter是必须设置的不然不显示日期,
//设置的布局是日期的布局,数据是用于签到的
vagueAdapter = new MyVagueAdapter(R.layout.layout_checkin_calendar_item);
vagueAdapter.setData(new HashMap>());
mCalendarView.setVagueAdapter(vagueAdapter);
}
(2)根据data判断是否显示签到的对号(imgview)
public class MyVagueAdapter extends VagueAdapter<Map<String, Map<String, Checkin>>> {
/**
* Initialization adapter.
*
* @param dayLayout layout for day of month
*/
public MyVagueAdapter(@LayoutRes int dayLayout) {
super(dayLayout);
}
@Override
public void onBindVague(View itemView, int year, int month, int dayOfMonth) {
ImageView ivCheckinAlready = (ImageView) itemView.findViewById(R.id.iv_checkin_already);
if (data == null) return;
//获取当月数据
Map monthMap = data.get(DateUtil.formatDate(year, month, dayOfMonth, MONTH_FORMAT));
//无当月数据
if (monthMap == null) {
ivCheckinAlready.setVisibility(View.GONE);
return;
}
Checkin history = monthMap.get(DateUtil.formatDate(year, month, dayOfMonth, DAY_OF_MONTH_FORMAT));
ivCheckinAlready.setVisibility(history == null ? View.GONE : View.VISIBLE);
}
@Override
public void flagToday(View todayView) {
//标记今天
TextView tvDayView = (TextView) todayView.findViewById(R.id.tv_day_of_month);
tvDayView.setBackgroundResource(R.mipmap.checkin_calendar_ic_today_flag);
tvDayView.setTextColor(Color.WHITE);
}
@Override
public void flagNotToday(View dayView, Date date) {
//设置非今天的文本颜色
TextView tvDayView = (TextView) dayView.findViewById(R.id.tv_day_of_month);
tvDayView.setBackgroundColor(Color.TRANSPARENT);
tvDayView.setTextColor(Color.BLUE);
}
}
(3)造签到数据
private Map<String, Map<String, Checkin>> createCheckinData() {
Map<String, Map<String, Checkin>> checkinMap = new HashMap<>();
Map<String, Checkin> monthCheckinMap = new HashMap<>();
Date today = DateUtil.today();
int year = today.getYear();
int month = today.getMonth();
int dayofmonth = today.getDayOfMonth();
checkinMap.put(DateUtil.formatDate(year, month, dayofmonth, MONTH_FORMAT), monthCheckinMap);
for (int i = 1, N = today.getDayOfMonth() - 1; i < N; i++) {
if (i % 3 == 0) {
monthCheckinMap.put(DateUtil.formatDate(year, month, dayofmonth + 1, DAY_OF_MONTH_FORMAT), new Checkin());
}
}
return checkinMap;
}
(4)是否签到直接插入数据,更新即可
public void sign(View view) {
Date today = DateUtil.today();
int year = today.getYear();
int month = today.getMonth();
int dayOfMonth = today.getDayOfMonth();
Map<String, Checkin> checkinMap = vagueAdapter.getData().get(DateUtil.formatDate(year, month, dayOfMonth, MONTH_FORMAT));
if (checkinMap == null) return;
checkinMap.put(DateUtil.formatDate(year, month, dayOfMonth, DAY_OF_MONTH_FORMAT), new Checkin());
vagueAdapter.notifyDataSetChanged();
view.setEnabled(false);
((Button) view).setText("已签到");
}
4、最后完整
public class EasyCalendarActivity extends AppCompatActivity {
private CalendarView mCalendarView;
private final String MONTH_FORMAT = "yyyyMM";
private final String DAY_OF_MONTH_FORMAT = "yyyyMMdd";
private VagueAdapter