1 大致思路
1) 初始化日历数据,作为list传入到RecyclerView.Adapter
2) 重写RecyclerView的onTouchEvent方法,监听手势的改变,然后更改list数据,重新显示UI
2 关键代码
那么整个项目的重点在于如何获取到正确的日期数据,这个在网上找了一个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 mDataList = new ArrayList<>();//日期数组
private ArrayList 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 initDateList() {
return initDateList(mYear, mMonth);
}
public void initRecordList(ArrayList recordList) {
mRecordList = recordList;
}
/**
* 通过年月获取当前页面的日期集合
*/
private ArrayList 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方法,会根据当前传入的年月数据来计算当前日历该显示的数据,包括显示前一个月的日期以及后一个月的日期。那么APP要做的就只是不断的传入年月信息,重新去计算日历数据
。这里重写了RecyclerView方法的onTouchEvent来判断用户的触摸方向,后来因为要实现每一个日历的点击事件,与监听的滑动事件冲突了,
所以也重写了onInterceptTouchEvent方法来拦截子控件的点击事件。
public class CalendarRecycleView extends RecyclerView {
private CalendarRecycleViewAdapter mAdapter;
private Context mContext;
private CalendarTool mCalendarTool;
private OnCalendarDateListener mDateListener;
private float motion_x;
public CalendarRecycleView(Context context) {
super(context);
mContext = context;
init();
}
public CalendarRecycleView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public CalendarRecycleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
private void init() {
setLayoutManager(new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL));
mCalendarTool = new CalendarTool();
mAdapter = new CalendarRecycleViewAdapter(mContext, mCalendarTool.initDateList());
setAdapter(mAdapter);
mAdapter.setOnItemListener(new CalendarRecycleViewAdapter.OnItemListener() {
@Override
public void onItemClick(DateEntity dateEntity) {
if (mDateListener != null) {
mDateListener.onDateItemClick(dateEntity);
}
}
});
}
public void initRecordList(ArrayList list) {
mCalendarTool.initRecordList(list);
mCalendarTool.initDateList();
mAdapter.notifyDataSetChanged();
}
public void setOnCalendarDateListener(OnCalendarDateListener listener) {
this.mDateListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
motion_x = event.getX();
Log.i("onTouchEvent", "ACTION_DOWN: " + event.getX() + " " + motion_x);
break;
case MotionEvent.ACTION_MOVE:
Log.i("onTouchEvent", "ACTION_MOVE: " + event.getX() + " " + motion_x);
float x = event.getX() - motion_x;
if (Math.abs(x) > CalendarTool.FLING_MIN_DISTANCE && motion_x != 0) {
mCalendarTool.flushDate(x);
mAdapter.notifyDataSetChanged();
motion_x = 0;
if (mDateListener != null) {
mDateListener.onDateChange(mCalendarTool.getNowCalendar(),
mCalendarTool.getStartDay(),
mCalendarTool.getEndDay(),
mCalendarTool.isStartBelong(),
mCalendarTool.isEndBelong());
}
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
motion_x = event.getX();
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(motion_x - event.getX()) >= mCalendarTool.FLING_MIN_DISTANCE) {
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onInterceptTouchEvent(event);
}
}
3 结论
实现起来很简单,就是细节方面要多注意,然后因为没有用到ViewPager,所以滑动起来没有动画效果,加上去也简单,关键是要把ViewPager fragment的list数据计算好就成。
下载:
http://download.csdn.net/detail/u012521570/9879095