总结自己写的小算法(二)

横向滑动选择日期填充数据

  • 先贴代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

//设置月
Date time = Calendar.getInstance(Locale.CHINA).getTime();
int year = 1900 + time.getYear();
int month = time.getMonth();
String currentMonth = (month + 1) + "月";

//填充当月天数
int dayCountOfCurrentMonth = SelectDateUtil.getDayCountOfCurrentMonth() + 1; //10月 31天 +1是为补当月最后一天
Calendar instance = Calendar.getInstance(Locale.CHINA);
for (int i = 0; i < dayCountOfCurrentMonth; i++) {
String s = (i) + "";
instance.set(year, month, i);
Date time1 = instance.getTime();
String dayOfWeekFromDate = SelectDateUtil.getDayOfWeekFromDate(time1);
//Log.e(TAG, "loadData: " + s + " -- " +time1 + " -- " + dayOfWeekFromDate);
selectDates.add(new LPCSelectDate(s, dayOfWeekFromDate));
}
selectDates.remove(0); //移除第一位上个月的最后一天
lpcDateSelectAdapter.notifyDataSetChanged();

  • 需求

    • 向横向滑动的RecycleView日历中添加数据
  • 思路

    • 由Calendar类获取整个月份的总天数
    • 遍历天数并添加到集合中刷新RecycleView适配器
  • 实现

    • 见代码注释

END