Android自定义日历并随心所欲改变界面样式附源码下载地址

目前好多界面需要以日历方式显示或者是弹出日历去选择日期等,在网上搜了一下有很多可以用,但是基本都是绘制view什么的,界面不容易修改,不太容易能改成自己需要的样式,所以决定自己写一个来用,先看效果:

下面看下动画效果:

里面界面效果都可以在代码里设置,先来说下主要思路:

一、通过viewPager来展示年份和月份方便左右滑动;

二、取数据设置给月份、月份适配器,实现数据展示。

这里面主要是数据获取部分:

一、每个月天数不确定;

二、每个月月初第一天是周几?月末是周几?

其实解决这两个问题数据部分就搞定了,我们不用自己去绘制view,用适配器显示数据了这就很简单了,不但逻辑清晰界面更是想怎么定就怎么定,数据我们写一个方法,传入年和月份来获取这个月包括补齐月初、月末天数就行了:


    public static List<MyDay> getMonthDayList(int year, int month) {
        List<MyDay> dayList = new ArrayList<MyDay>();
        int[] currentValue = getMonthDay(year, month);
        int currentMonthDays = currentValue[0];
        int currentMonthStartIndexOfWeek = currentValue[1];
        int currentMonthEndIndexOfWeek = currentValue[2];

        int addStart = 6 - (7 - currentMonthStartIndexOfWeek);
        int addEnd = 7 - currentMonthEndIndexOfWeek;

        int lastYear = year;
        int lastMonth = month - 1;
        if (month == 1) {
            lastYear--;
            lastMonth = 12;
        }
        int[] lastValue = getMonthDay(lastYear, lastMonth);

        int lastMonthDays = lastValue[0];

        for (int i = 0; i < addStart; i++) {
            MyDay accessDay = new MyDay();
            int day = lastMonthDays - addStart + i + 1;
            accessDay.setDay(day);
            accessDay.setCurrentMonth(false);
            //            accessDay.setDate(lastYear + "-" + lastMonth + "-" + day);
            accessDay.setDate(FormatYearMonthDay(lastYear + "", lastMonth + "", day + ""));
            dayList.add(accessDay);
        }
        for (int i = 1; i <= currentMonthDays; i++) {
            MyDay accessDay = new MyDay();
            accessDay.setDay(i);
            accessDay.setCurrentMonth(true);
            //            accessDay.setDate(year + "-" + month + "-" + i);
            accessDay.setDate(FormatYearMonthDay(year + "", month + "", i + ""));
            dayList.add(accessDay);
        }

        int nextYear = year;
        int nextMonth = month + 1;
        if (month == 12) {
            nextYear++;
            nextMonth = 1;
        }
        for (int i = 1; i <= addEnd; i++) {
            MyDay accessDay = new MyDay();
            accessDay.setDay(i);
            accessDay.setCurrentMonth(false);
            //            accessDay.setDate(nextYear + "-" + nextMonth + "-" + i);
            accessDay.setDate(FormatYearMonthDay(nextYear + "", nextMonth + "", i + ""));
            dayList.add(accessDay);
        }

        System.out.println(dayList.toString());

        return dayList;

    }

这只是其中一个关键方法,需要完整代码的小伙伴可以下载demo:

日历Demo下载地址

另外也可以加交流群579699145去群文件下载文件calendardemo.zip

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值