Android自定义日历控件

其实只要把 Calandar这个类熟悉了之后,都好写了。在学校的时候老师教过一次的是从1900年开始算的那种,当时还花费好多时间在这个上面,花了很多时间之后,才发现自己是如此愚蠢

先说下我的思路

1.确定要的行数
我见过有些的是5行,有些是6行。当然列数肯定都是7列(毕竟是一周7天嘛),这样返回的数据大小为row*7

2.计算当前显示月的上一月份日期
把日期设置为当月的第一天calendar.set(Calendar.DAY_OF_MONTH, 1);
获取本月第一天的星期数int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
将日期设置为上个月要开始的时间(后面我会说为什么要这样减)calendar.setTimeInMillis(calendar.getTimeInMillis()-firstDayOfWeek * oneDayTime);
从本月第一天的星期数开始循环递减,直到firstDayOfWeek不大于一为止,这里的数据就是上月份的
3.计算当前月份的日期
把日期设置为当月的第一天calendar.set(Calendar.DAY_OF_MONTH, 1);
获取本月最大的日期int maxDateOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
从一开始循环到最大日期数就是本月的日期数据了
4.计算下个月份的日期
将日期设置为本月最大的那一天calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
循环获取下月的日期数据,row*7 - 上月+本月的数据.size()就是本次递加要获取的数据

关于上月份为什么要那样减请看下图
Android自定义日历控件_第1张图片
日历.jpg

以下是所有代码,欢迎指出问题,欢迎技术交流讨论


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by cynthia on 2018/4/15.
 */
public class CalendarUtils {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
    private long oneDayTime = 24 * 60 * 60 * 1000;//一天的时间戳

    /**
     * @param date 生成月份 (yyyy-MM-dd)
     * @param row  日期总共几行,有些日历是只有5行,window7的日历是6行,列数应当永远为7
     * @return list
     */
    public List> genDate(String date, int row) {
        List> dateList = new ArrayList<>();
        int dateListSize = row * 7;
        Date paramDate;
        try {
            paramDate = dateFormat.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTime(paramDate);
            calendar.set(Calendar.DAY_OF_MONTH, 1);//设置日期从传入月第一天开始
            int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);//本月第一天是周几
            calendar.setTimeInMillis(calendar.getTimeInMillis()-firstDayOfWeek * oneDayTime);
            //将上个月的日期计算出来
            for (int i = firstDayOfWeek; i > 1; i--) {
                long timeLong = calendar.getTimeInMillis();
                calendar.clear();
                calendar.setTimeInMillis(timeLong + oneDayTime);
                dateList.add(new CalendarEntity(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                        calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.DAY_OF_MONTH), dateFormat.format(calendar.getTime()),
                        calendar.getTimeInMillis(), CalendarEntity.lastMonth));
            }
            //将传入的月份数据计算出来
            calendar.clear();
            calendar.setTime(paramDate);
            calendar.set(Calendar.DAY_OF_MONTH, 1);//设置日期从传入月第一天开始
            //获得本月最大的日期
            int maxDateOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            dateList.add(new CalendarEntity(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.DAY_OF_MONTH), dateFormat.format(calendar.getTime()),
                    calendar.getTimeInMillis(), CalendarEntity.nowMonth));
            for (int i = 1; i < maxDateOfMonth; i++) {
                long timeLong = calendar.getTimeInMillis();
                calendar.clear();
                calendar.setTimeInMillis(timeLong + oneDayTime);
                dateList.add(new CalendarEntity(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                        calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.DAY_OF_MONTH), dateFormat.format(calendar.getTime()),
                        calendar.getTimeInMillis(), CalendarEntity.nowMonth));
            }

            calendar.clear();
            calendar.setTime(paramDate);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//设置日期从传入月最后一天开始
            //获取下月的日期数据
            for (int i = dateList.size(); i < dateListSize; i++) {
                long timeLong = calendar.getTimeInMillis();
                calendar.clear();
                calendar.setTimeInMillis(timeLong + oneDayTime);
                dateList.add(new CalendarEntity(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                        calendar.get(Calendar.DAY_OF_WEEK), calendar.get(Calendar.DAY_OF_MONTH), dateFormat.format(calendar.getTime()),
                        calendar.getTimeInMillis(), CalendarEntity.nextMonth));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

}

CalendarEntity.java


import java.io.Serializable;

/**
 * Created by cynthia on 2018/4/15.
 */
public class CalendarEntity implements Serializable {
    public static final int nowMonth = 1;//本月
    public static final int lastMonth = 2;//上个月
    public static final int nextMonth = 3;//下个月
    private int year;
    private int month;
    private int week;
    private int day;
    private String dateStr;//yyyy-MM-dd
    private T data; //外部数据类型
    private long time;//时间戳
    private int dateType;//日期类型

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getWeek() {
        return week;
    }

    public int getDateType() {
        return dateType;
    }

    public void setDateType(int dateType) {
        this.dateType = dateType;
    }

    public int getYear() {
        return year;
    }

    public long getTime() {
        return time;
    }

    public String getDateStr() {
        return dateStr;
    }

    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public void setWeek(int week) {
        this.week = week;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public CalendarEntity() {
    }

    public CalendarEntity(int year, int month, int week, int day, String dateStr, long time, int dateType) {
        this.year = year;
        this.month = month;
        this.week = week;
        this.day = day;
        this.dateStr = dateStr;
        this.time = time;
        this.dateType = dateType;
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

数据源有了,什么样的ui都好调了,之前在网上看的demo 都是画布画的ui 感觉特别麻烦,我还是默默的选择了xml布局

你可能感兴趣的:(Android自定义日历控件)