Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 GridView 打造簡易月曆:日期計算與格子排列

編輯:關於 Android 程式設計

做一個能翻月的月曆,核心其實只有兩件事:算出該月第一天是星期幾、該月總共有幾天,然後把上個月和下個月的剩餘格子補滿。以下示範一個只負責顯示月份的版本,尚未加入當日標記與記事功能。

一、日期工具類

 /** * 獲取該月天數 */ 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)); } 

getCurrentMonthDay() 是個值得記住的小技巧:先把日期設為 1 號,再用 roll(DATE, -1) 回退一天,就能停在「上個月的最後一天」,此時的 DATE 值正好等於本月天數。閏年二月、大小月都不用自己寫判斷。

getLastOrNextMonth() 則靠 add(Calendar.MONTH, count) 一次前進或後退,傳 -1 就是上個月。

二、組出整個月的格子列表

我用一個 List 存放每一天。由於原先預期每天可能帶記事資訊,所以封裝了一個 DayBean;若不需要,直接存時間戳也可以。

 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)); } 

三、注意 DAY_OF_WEEK 的起點

這段邏輯最容易出錯的地方是 Calendar.DAY_OF_WEEK 的返回值:週日是 1、週一是 2……週六是 7。因為月曆通常把週一放第一格,所以程式碼裡用 fir_day_of_week - 2 來算前面要補幾格;當第一天剛好是週日(值為 1)時,減法會變負數,因此必須另外特例處理補滿 6 格。

月末同理:最後一天的 DAY_OF_WEEK 若不等於 1(週日),就一路補下個月的日期,直到湊滿整週。

四、Adapter 中的樣式切換

我在 DayBean 裡放了一個「是否本月」的旗標,Adapter 依此切換字色:本月黑、非本月灰;今日則加底色強調。

 @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; } 

這裡用了 ViewHolder 模式把 findViewById 的結果快取下來,避免每次捲動都重複查找視圖,是 ListView/GridView 效能的基本功。

五、還可以再加什麼

若要往下延伸,可以考慮:把「今日」的判定改成比對年月日而非僅比對日期數字、加入點擊後跳轉日誌頁的互動、用 RecyclerView 搭配 GridLayoutManager 取代 GridView 以取得更好的回收效率,以及把日期計算邏輯抽成獨立的工具類方便測試。

以上就是以 GridView 實作月曆的完整思路,核心永遠是「月初星期幾 + 本月幾天」這兩個數字,剩下的都是排列與樣式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved