美文网首页
Android Calendar自定义日历控件

Android Calendar自定义日历控件

作者: i小灰 | 来源:发表于2020-09-19 02:32 被阅读0次

    效果预览

    image

    思路:

    1、RecyclerView+Calendar
    2、Calendar获取整月的天数
    3、Calendar获取上一个月和下一个月需要补全的天数
    
    

    1、Calendar获取整月的天数

     Calendar instance = Calendar.getInstance();
    
            if (indexDate != null) {
                //自定月份
                instance.setTime(indexDate.getDate());
            }
    
            //获取当前月最大的日期
            int maxDate = instance.getActualMaximum(Calendar.DATE);
            instance.set(Calendar.DAY_OF_MONTH, 1);
            //获取当前月所有的天数
            for (int i = 0; i < maxDate; i++, instance.add(Calendar.DATE, 1)) {
                int day = instance.get(Calendar.DAY_OF_MONTH);
                CalendarBean calendarBean = new CalendarBean();
                calendarBean.setDay(String.valueOf(day));
                calendarBean.setDate(instance.getTime());
                mDatas.add(calendarBean);
            }
    
    

    2、获取上一个月需要补全的天数

     private ArrayList<CalendarBean> getLastMonth(CalendarBean calendarBean) {
            //获取当前月第一天
            Calendar calendarFirstDay = Calendar.getInstance();
            calendarFirstDay.setTime(calendarBean.getDate());
            calendarFirstDay.set(Calendar.DAY_OF_MONTH, 1);
            //当月第一天是星期几
            int firstDayOfWeek = calendarFirstDay.get(Calendar.DAY_OF_WEEK);
            if (firstDayOfWeek != 1) {
                /**
                 *   firstDayOfWeek =1时,就是星期日,当前月第一天已经处于星期日,不需要添加上一个月补充天数
                 */
                //需要补全的天数,当needAdd为负数时候,说明是星期日,需要补6天
                int needAdd = firstDayOfWeek - 1;
    
                ArrayList<CalendarBean> lastMonth = new ArrayList<>();
                for (int i = 0; i < needAdd; i++) {
                    CalendarBean dayBean = new CalendarBean();
                    //0:当月,1:上月,2:下一个月
                    dayBean.setDayType(1);
                    dayBean.setWeekOfDay(getWeekString(i));
                    //取出上一个月需要补全的天数
                    //calendarFirstDay.set(Calendar.DAY_OF_MONTH, 0 - i);
                    calendarFirstDay.add(Calendar.DATE, -1);
                    //上一个月的天数
                    int lastMonthDay = calendarFirstDay.get(Calendar.DAY_OF_MONTH);
                    dayBean.setDay(String.valueOf(lastMonthDay));
                    dayBean.setDate(dayBean.getDate());
                    lastMonth.add(0, dayBean);
                }
    
                return lastMonth;
    
            }
            return new ArrayList<>();
        }
    
    

    3、获取下一个月需要补全的天数

     private ArrayList<CalendarBean> getNextMonth(CalendarBean calendarBean) {
    
            //获取当前月最后一天
            Calendar calendarLastDay = Calendar.getInstance();
            calendarLastDay.setTime(calendarBean.getDate());
            calendarLastDay.add(Calendar.MONTH, 1);
            calendarLastDay.set(Calendar.DAY_OF_MONTH, 0);
            //获取当前月最后一天是星期几
            int nextDayOfWeek = calendarLastDay.get(Calendar.DAY_OF_WEEK);
            if (nextDayOfWeek != 7) {
                /**
                 *   nextDayOfWeek =7时,就是星期六,当前月最后一天已经处于星期六,不需要添加上一个月补充天数
                 */
                int needAdd = 7 - nextDayOfWeek;
    
                ArrayList<CalendarBean> nextMonth = new ArrayList<>();
                for (int i = 1; i <= needAdd; i++) {
                    CalendarBean dayBean = new CalendarBean();
                    //0:当月,1:上月,2:下一个月
                    dayBean.setDayType(2);
                    dayBean.setWeekOfDay(getWeekString(i));
                    //取出上一个月需要补全的天数
                    calendarLastDay.set(Calendar.DAY_OF_MONTH, i);
                    //上一个月的天数
                    int lastMonthDay = calendarLastDay.get(Calendar.DAY_OF_MONTH);
                    dayBean.setDay(String.valueOf(lastMonthDay));
                    dayBean.setDate(dayBean.getDate());
                    nextMonth.add(dayBean);
                }
                return nextMonth;
            }
            return new ArrayList<>();
        }
    
    
    Demo地址

    相关文章

      网友评论

          本文标题:Android Calendar自定义日历控件

          本文链接:https://www.haomeiwen.com/subject/appfektx.html