Android使用GridView实现日历的简单功能
程序员文章站
2024-02-25 18:43:15
简单的日历实现,只是显示了每一个月,没有显示当天和记事这些功能
主要是计算月初是周几,月末是周几,然后相应的显示上一月多少天和下一月多少天。
先看一下关于日期的用到...
简单的日历实现,只是显示了每一个月,没有显示当天和记事这些功能
主要是计算月初是周几,月末是周几,然后相应的显示上一月多少天和下一月多少天。
先看一下关于日期的用到的几个工具类
/** * 获取该月天数 */ public static int getcurrentmonthday(long millsec) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(millsec); calendar.set(calendar.date, 1); calendar.roll(calendar.date, -1); int datecount = calendar.get(calendar.date); return datecount; } /** * 获取当月第一天 */ public static long getfirofmonth(long millsec) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(millsec); calendar.set(calendar.date, 1); return calendar.gettimeinmillis(); } /** * 获取当前时间戳 */ public static long getcurrenttime() { calendar calendar = calendar.getinstance(); return calendar.gettimeinmillis(); } /** * 获取上一月/下一月 */ public static long getlastornextmonth(long millsec, int count) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(millsec); calendar.add(calendar.month, count); return calendar.gettimeinmillis(); } /** * 格式化到月份 */ public static string long2str(long millsec) { simpledateformat sdf = new simpledateformat("yyyy-mm"); return sdf.format(new date(millsec)); }
用一个list存放每一天,因为之前考虑到每一天有记事这些,所以封装了一个类,如果不需要的话可以直接一个时间戳就可以。
打开之后先获取当前的时间戳,然后初始化到当前月份的第一天,通过工具类中方法。
然后就是计算了,把上月结余的、本月所有、下月的一起添加到list中。
private void setcalendarlist(long millsecs) { beans.clear(); calendar calendar = calendar.getinstance(); calendar.settimeinmillis(millsecs); // int nor = today.getday(); int max = dateutil.getcurrentmonthday(millsecs); /** * 当月日历 */ for (int i = 1; i <= max; i++) { daybean bean = new daybean(); calendar cc = calendar.getinstance(); cc.settimeinmillis(millsecs); cc.add(calendar.day_of_month, i - 1); setbean(bean, cc); bean.setiscurrentmonth(true); beans.add(bean); } //上月结余 int fir_day_of_week = beans.get(0).getcalendar().get(calendar.day_of_week); log.e("aaa", "week_last:" + fir_day_of_week); if (fir_day_of_week != 2) { if(fir_day_of_week == 1){ for (int i = 0; i < 6; i++) { daybean bean = new daybean(); calendar cc = calendar.getinstance(); cc.settimeinmillis(millsecs); cc.add(calendar.day_of_month, -i -1); setbean(bean, cc); log.e("aaa", "last:" + bean.getday()); bean.setiscurrentmonth(false); beans.add(0, bean); } }else{ for (int i = 0; i < fir_day_of_week-2; i++) { daybean bean = new daybean(); calendar cc = calendar.getinstance(); cc.settimeinmillis(millsecs); cc.add(calendar.day_of_month, -i-1); setbean(bean, cc); log.e("aaa", "last:" + bean.getday()); bean.setiscurrentmonth(false); beans.add(0, bean); } } } //下月 int last_day_of_week = beans.get(beans.size() - 1).getcalendar().get(calendar.day_of_week); log.e("aaa", "week_next:" + last_day_of_week); if (last_day_of_week != 1) { for (int i = last_day_of_week; i < 8; i++) { daybean bean = new daybean(); calendar cc = calendar.getinstance(); cc.settimeinmillis(millsecs); cc.add(calendar.day_of_month, i - last_day_of_week); setbean(bean, cc); log.e("aaa", "next:" + bean.getday()); bean.setiscurrentmonth(false); beans.add(bean); } } gridviewadapter adapter = new gridviewadapter(this, beans); mgridview.setadapter(adapter); } private void setbean(daybean bean, calendar cc) { bean.setcalendar(cc); bean.setday(cc.get(calendar.day_of_month)); }
设置每一天时设置了一个字段为是否为本月,如果是,则设置为黑色,否则设为灰色显示,在adapter中的getview方法中设置即可。
@override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = inflater.inflate(r.layout.item_layout, null); holder = new viewholder(); holder.day_text = (textview) convertview.findviewbyid(r.id.day_text); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.day_text.settext(beans.get(position).getday() + ""); if (beans.get(position).istoday()) { holder.day_text.setbackgroundcolor(color.red); } else { holder.day_text.setbackgroundcolor(color.white); } if (beans.get(position).iscurrentmonth()) { holder.day_text.settextcolor(color.black); } else { holder.day_text.settextcolor(color.gray); } return convertview; } class viewholder { textview day_text; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。