▲ Android 使用RecycleView自定义日历签到效果

▲ Android 使用RecycleView自定义日历签到效果

最近公司又要求做一个签到日历效果,我为啥加个又是之前我实现了一个日历签到效果,而这次我使用的则是RecycleView去实现。

先上效果图
在这里插入图片描述

实现思路
初始化日历数据,把数据传入到适配器中并显示。
至于左右滑动页面刷新,重写RecyclerView的onTouchEvent方法,监听手势的改变,然后更改list数据,重新显示UI。

在这里借鉴了一下 ToMyBestLove 所写的博客,并完善了一下方法,方便定制化处理。

核心代码
CalendarTool 这个工具类确实不错,可以获取正确的日期,很棒的算法可以减少大家不必要的时间。

public class CalendarTool<T extends BaseDateEntity> {

	private final String TAG = CalendarTool.class.getSimpleName();

	public static int FLING_MIN_DISTANCE = 100;

	private final int[] weekDayRow = {0, 1, 2, 3, 4, 5, 6};

	private ArrayList<DateEntity> mDataList = new ArrayList<>();//日期数组
	private ArrayList<T> mRecordList;//事件记录数组
	private DateEntity   mDateEntity;
	private int          mYear;
	private int          mMonth;

	private boolean mEndBelong;
	private boolean mStartBelong;
	private int     mStartDay;
	private int     mEndDay;

	/**
	 * 当前年月日
	 */
	private int mCurrenYear;
	private int mCurrenMonth;
	private int mCurrenDay;

	/**
	 * 平年月天数数组
	 */
	int commonYearMonthDay[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	/**
	 * 闰年月天数数组
	 */
	int leapYearMonthDay[]   = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	public CalendarTool() {
		/** 初始化当前系统的日期 */
		Calendar calendar = Calendar.getInstance();
		mCurrenYear = calendar.get(Calendar.YEAR);
		mCurrenMonth = calendar.get(Calendar.MONTH) + 1;
		mCurrenDay = calendar.get(Calendar.DAY_OF_MONTH);
		this.mYear = mCurrenYear;
		this.mMonth = mCurrenMonth;
	}

	/**
	 * 获取当前日历的年月 x为年,y为月
	 */
	public Point getNowCalendar() {
		Point p = new Point(mYear, mMonth);
		return p;
	}

	/**
	 * 判断第一天属不属于本月
	 */
	public boolean isStartBelong() {
		return mStartBelong;
	}

	/**
	 * 判断最后一天属不属于本月
	 */
	public boolean isEndBelong() {
		return mEndBelong;
	}

	/**
	 * 获取日历第一天的日期
	 */
	public int getStartDay() {
		return mStartDay;
	}

	/**
	 * 获取日历最后一天的日期
	 */
	public int getEndDay() {
		return mEndDay;
	}

	public ArrayList<DateEntity> initDateList() {
		return initDateList(mYear, mMonth);
	}

	public void initRecordList(ArrayList<T> recordList) {
		mRecordList = recordList;
	}

	/**
	 * 通过年月获取当前页面的日期集合
	 */
	private ArrayList<DateEntity> initDateList(int year, int month) {

		Log.i(TAG, "initDateList: year = " + year + " month = " + month);

		mDataList.clear();

		/** 修改部分 */
		int endDate = 0;// 得到上一个月的天数,作为上一个月在本日历的结束日期
		if ((year - 1) == this.mYear || month == 1) {// 说明向前翻了一年,那么上个月的天数就应该是上一年的12月的天数,或者到翻到一月份的时候,那么上一个月的天数也是上一年的12月份的天数
			endDate = this.getDays(year - 1, 12);
		} else {// 得到上一个月的天数,作为上一个月在本日历的结束日期
			endDate = this.getDays(year, month - 1);
		}
		/** 修改部分结束 */

		this.mYear = year;// 当前日历上显示的年
		this.mMonth = month;// 当前日历上显示的月

		int days = this.getDays(year, month);// 得到本月的总共天数
		int dayOfWeek = this.getWeekDay(year, month);//得到当前年月的第一天为星期几
		int selfDaysEndWeek = 0;// 本月的最后一天是星期几

		mStartBelong = true;

		/** 先添加前面不属于本月的 */
		if (dayOfWeek != 0) {
			int startDate = endDate - dayOfWeek + 1;// 当前月的上一个月在本日历的开始日期
			for (int i = startDate, j = 0; i <= endDate; i++, j++) {

				mDateEntity = new DateEntity(year, month - 1, i);
				mDateEntity.date = mDateEntity.year * 10000 + mDateEntity.month * 100 + i;
				if (startDate == i) {
					mStartBelong = false;
					mStartDay = mDateEntity.date;
				}

				mDateEntity.isSelfMonthDate = false;
				mDateEntity.weekDay = weekDayRow[j];
				mDateEntity.hasRecord = hasRecord(mDateEntity.date);
				mDataList.add(mDateEntity);
			}
		}

		/** 添加本月的 */
		for (int i = 1, j = dayOfWeek; i <= days; i++, j++) {

			mDateEntity = new DateEntity(year, month, i);
			mDateEntity.date = mDateEntity.year * 10000 + mDateEntity.month * 100 + i;
			if (mStartBelong && i == 1) {
				mStartDay = mDateEntity.date;
			}
			if (i == days) {
				mEndDay = mDateEntity.date;
			}
			mDateEntity.isSelfMonthDate = true;
			if (j >= 7) {
				j = 0;
			}
			selfDaysEndWeek = j;
			mDateEntity.weekDay = weekDayRow[j];
			if (year == mCurrenYear && month == mCurrenMonth && i == mCurrenDay) {
				mDateEntity.isNowDate = true;
			}
			mDateEntity.hasRecord = hasRecord(mDateEntity.date);
			mDataList.add(mDateEntity);
		}

		mEndBelong = true;

		/*** 添加后面下一个月的 */
		for (int i = 1, j = selfDaysEndWeek + 1; i < 7; i++, j++) {

			if (j >= 7) {
				break;
			}
			mEndBelong = false;

			mDateEntity = new DateEntity(year, month + 1, i);

			if (mDateEntity.month > 12) {
				mDateEntity.year = year + 1;
				mDateEntity.month = 1;
			}
			mDateEntity.date = mDateEntity.year * 10000 + mDateEntity.month * 100 + i;
			mDateEntity.isSelfMonthDate = false;
			mDateEntity.weekDay = weekDayRow[j];
			mDateEntity.hasRecord = hasRecord(mDateEntity.date);
			mDataList.add(mDateEntity);

			mEndDay = mDateEntity.year * 10000 + mDateEntity.month * 100 + i;
		}
		return mDataList;
	}

	/**
	 * 通过年月,获取这个月一共有多少天
	 */
	private int getDays(int year, int month) {
		int days = 0;

		if ((year % 4 == 0 && (year % 100 != 0)) || (year % 400 == 0)) {
			if (month > 0 && month <= 12) {
				days = leapYearMonthDay[month - 1];
			}
		} else {
			if (month > 0 && month <= 12) {
				days = commonYearMonthDay[month - 1];
			}
		}
		return days;
	}

	private boolean hasRecord(int date) {
		if (mRecordList != null) {
			for (T baseDateEntity : mRecordList) {
				if (baseDateEntity.year * 10000 + baseDateEntity.month * 100 + baseDateEntity.day == date) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 通过年,月获取当前月的第一天为星期几 ,返回0是星期天,1是星期一,依次类推
	 */
	private int getWeekDay(int year, int month) {
		int dayOfWeek;
		int goneYearDays = 0;
		int thisYearDays = 0;
		boolean isLeapYear = false;//闰年
		int commonYearMonthDay[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		int leapYearMonthDay[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		for (int i = 1900; i < year; i++) {// 从1900年开始算起,1900年1月1日为星期一
			if ((i % 4 == 0 && (i % 100 != 0)) || (i % 400 == 0)) {
				goneYearDays = goneYearDays + 366;
			} else {
				goneYearDays = goneYearDays + 365;
			}
		}
		if ((year % 4 == 0 && (year % 100 != 0)) || (year % 400 == 0)) {
			isLeapYear = true;
			for (int i = 0; i < month - 1; i++) {
				thisYearDays = thisYearDays + leapYearMonthDay[i];
			}
		} else {
			isLeapYear = false;
			for (int i = 0; i < month - 1; i++) {
				thisYearDays = thisYearDays + commonYearMonthDay[i];
			}
		}
		dayOfWeek = (goneYearDays + thisYearDays + 1) % 7;

		Log.d(this.getClass().getName(), "从1990到现在有" + (goneYearDays + thisYearDays + 1) + "天");
		Log.d(this.getClass().getName(), year + "年" + month + "月" + 1 + "日是星期" + dayOfWeek);
		return dayOfWeek;
	}

	public void flushDate(float distance_x) {
		if (distance_x < 0) {// Fling right
			if (mMonth + 1 > 12) {
				mDataList = initDateList(mYear + 1, 1);
			} else {
				mDataList = initDateList(mYear, mMonth + 1);
			}
		} else {// Fling left
			if (mMonth - 1 <= 0) {
				mDataList = initDateList(mYear - 1, 12);
			} else {
				mDataList = initDateList(mYear, mMonth - 1);
			}
		}
	}
}

initDateList方法,会根据当前传入的年月数据来计算当前日历该显示的数据。

因为我的需求是点击按钮完成签到即可,不用点击日历中的日期(item),只需要把当前的日期传入即可

               Calendar calendar = Calendar.getInstance();
                list.add(new BaseDateEntity(calendar.get(Calendar.YEAR), (calendar.get(Calendar.MONTH) + 1),calendar.get(Calendar.DAY_OF_MONTH)));
                rcDate.initRecordList(list);

initRecordList 已经封装adapter刷新,不用担心传值后没有刷新。

这个Demo即使是新手直接可以使用,省去了大家阅读的时间,毕竟大家的时间宝贵,干就完了

GitHub源码地址

如果您觉得功能对您有所帮助,麻烦给我一颗小星星。 谢谢大家

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2811.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab求两向量夹角_MATLAB基础练习(一)

    matlab求两向量夹角_MATLAB基础练习(一)1、按要求写出实现该功能的代码(1)使用方括号“[]”操作符产生一个列向量x,内容为1,2,4,7(2)使用方括号“[]”操作符产生一个行向量x,内容为1,2,4,7(3)使用冒号“:”操作符产生一个行向量x,内容为9,7,5,3,1(4)使用方括号“[]”操作符产生一个二维数组A,第1行为9,4,5,1;第2行为1,0,4,7(5)使用zeros函数产生一个3*2的二维数组A,使用one…

    2022年8月30日
    2
  • nl2br()与nl2p()函数,php在字符串中的新行(\n)之前插入换行符

    nl2br()与nl2p()函数,php在字符串中的新行(\n)之前插入换行符

    2021年9月18日
    34
  • NOIP2011计算系数详解[通俗易懂]

    NOIP2011计算系数详解[通俗易懂]原题见洛谷(https://www.luogu.org/problem/show?pid=1313)想看稍微简单点的就是NOIP2016的组合数问题,小飞机~(http://blog.csdn.net/a1351937368/article/details/76907902)先说一下这道题需要用到:组合数(杨辉三角),乘方做这道题的感受:题目中说(by+ax)^k,而输入顺序是先a后b搞

    2022年9月25日
    0
  • 神经网络的基本原理[通俗易懂]

    神经网络的基本原理[通俗易懂]1.神经网络1.1.神经元概述神经网络是由一个个的被称为“神经元”的基本单元构成,单个神经元的结构如下图所示:对于上述的神经元,其输入为x1x_1x1​,x2x_2x2​,x3x_3x3​以及截距+1+1+1,其输出为:hW,b(x)=f(WTx)=f(∑i=13Wixi+b)h_{\mathbf{W},b}\left(\mathbf{x}\right)=f\left(\mathbf{W}^T\mathbf{x}\right)=f\left(\sum_{i=1}^{3}W_

    2022年7月20日
    8
  • 软件测试:系统测试之因果图方法

    软件测试:系统测试之因果图方法来源:http://blog.csdn.net/vincetest一.    方法简介1.定义:是一种利用图解法分析输入的各种组合情况,从而设计测试用例的方法,它适合于检查程序输入条件的各种组合情况。2.因果图法产生的背景:等价类划分法和边界值分析方法都是着重考虑输入条件,但没有考虑输入条件的各种组合、输入条件之间的相互制约关系。这样虽然各种输入条件可能出错的情况已经测试到了,但多个输入条件组合起…

    2022年8月14日
    1
  • 外挂基础_开挂的正确姿势

    外挂基础_开挂的正确姿势一、前言  所谓游戏外挂,其实是一种游戏外辅程序,它可以协助玩家自动产生游戏动作、修改游戏网络数据包以及修改游戏内存数据等,以实现玩家用最少的时间和金钱去完成功力升级和过关斩将。虽然,现在对游戏外挂程序的“合法”身份众说纷纭,在这里我不想对此发表任何个人意见,让时间去

    2022年10月8日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号