查找过各种技术文档,发现android价格日历这一部分貌似还是空白,我就把真实项目中的代码给大家分析一下
有兴趣的朋友可以去下载源码
http://download.csdn.net/detail/qq_28195645/9358249
一、先给大家看效果图,左右滑动翻月,如果在某天之前没有可售的日子,那么会提示“xxxx-xx-xx之前无...”,并不能翻月。颜色,文字都可以自定义,
二、接下来我们讲解代码
直接上价格日历的控件,下面是关键代码
/** * 填充日历(包含日期、标记、背景等) */ private void setCalendarDate() { // 根据日历的日子获取这一天是星期几 int weekday = calendarday.getDay(); // 每个月第一天 int firstDay = 1; // 每个月中间号,根据循环会自动++ int day = firstDay; // 每个月的最后一天 int lastDay = getDateNum(calendarday.getYear(), calendarday.getMonth()); // 下个月第一天 int nextMonthDay = 1; int lastMonthDay = 1; // 填充每一个空格 for (int i = 0; i < ROWS_TOTAL; i++) { for (int j = 0; j < COLS_TOTAL; j++) { // 这个月第一天不是礼拜天,则需要绘制上个月的剩余几天 if (i == 0 && j == 0 && weekday != 0) { int year = 0; int month = 0; int lastMonthDays = 0; // 如果这个月是1月,上一个月就是去年的12月 if (calendarday.getMonth() == 0) { year = calendarday.getYear() - 1; month = Calendar.DECEMBER; } else { year = calendarday.getYear(); month = calendarday.getMonth() - 1; } // 上个月的最后一天是几号 lastMonthDays = getDateNum(year, month); // 第一个格子展示的是几号 int firstShowDay = lastMonthDays - weekday + 1; // 上月 for (int k = 0; k < weekday; k++) { lastMonthDay = firstShowDay + k; LinearLayout group = getDateView(0, k); group.setGravity(Gravity.CENTER); LinearLayout view = null; if (group.getChildCount() > 0) { view = (LinearLayout) group.getChildAt(0); } else { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, -1); view = new LinearLayout(getContext()); view.setLayoutParams(params); view.setGravity(Gravity.CENTER); view.setOrientation(LinearLayout.VERTICAL); // view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.calendar_day_item, null); group.addView(view); } //日历中的日期 // TextView txtvDay = (TextView) view.findViewById(R.id.txtv_day_calendaritem); TextView txtvDay = null; TextView txtvPrice = null; if (view. getChildCount() > 0) { txtvDay = (TextView) view.getChildAt(0); txtvPrice = (TextView) view.getChildAt(1); } else { LinearLayout.LayoutParams paramsDay = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.5f); txtvDay = new TextView(getContext()); txtvPrice = new TextView(getContext()); txtvDay.setLayoutParams(paramsDay); txtvDay.setGravity(Gravity.CENTER); txtvDay.setTextSize(10); LinearLayout.LayoutParams paramsPrice = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f); txtvPrice.setLayoutParams(paramsDay); txtvPrice.setGravity(Gravity.CENTER); txtvPrice.setTextSize(10); view.addView(txtvDay); view.addView(txtvPrice); } txtvDay.setText(Integer.toString(lastMonthDay)); txtvDay.setTextColor(COLOR_TX_OTHER_MONTH_DAY); txtvPrice.setTextColor(COLOR_TX_OTHER_MONTH_DAY); dates[0][k] = format(new Date(year, month, lastMonthDay)); // 设置日期背景色 if (dayBgColorMap.get(dates[0][k]) != null) { // view.setBackgroundResource(dayBgColorMap // .get(dates[i][j])); } else { // view.setBackgroundColor(Color.TRANSPARENT); ((LinearLayout) view.getParent()).setBackgroundResource(R.drawable.calendar_day_bg); } // 设置标记 setMarker(group, 0, k); } j = weekday - 1; // 这个月第一天是礼拜天,不用绘制上个月的日期,直接绘制这个月的日期 } else { LinearLayout group = getDateView(i, j); group.setGravity(Gravity.CENTER); LinearLayout view = null; if (group. getChildCount() > 0) { view = (LinearLayout) group.getChildAt(0); } else { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, -1); view = new LinearLayout(getContext()); view.setLayoutParams(params); view.setGravity(Gravity.CENTER); view.setOrientation(LinearLayout.VERTICAL); group.addView(view); // view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.calendar_day_item, null); } // 本月 if (day <= lastDay) { dates[i][j] = format(new Date(calendarday.getYear(), calendarday.getMonth(), day)); TextView txtvDay = null; TextView txtvPrice = null; if (view. getChildCount() > 0) { txtvDay = (TextView) view.getChildAt(0); txtvPrice = (TextView) view.getChildAt(1); } else { LinearLayout.LayoutParams paramsDay = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.5f); txtvDay = new TextView(getContext()); txtvPrice = new TextView(getContext()); txtvDay.setLayoutParams(paramsDay); txtvDay.setGravity(Gravity.CENTER); txtvDay.setTextSize(10); LinearLayout.LayoutParams paramsPrice = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f); txtvPrice.setLayoutParams(paramsDay); txtvPrice.setGravity(Gravity.CENTER); txtvPrice.setTextSize(10); // view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.calendar_day_item, null); view.addView(txtvDay); view.addView(txtvPrice); } txtvPrice.setText(""); /** * 获得本月的group */ if (groups != null && groups.size() != 0) { List<GroupDeadline> thisMonths = new ArrayList<GroupDeadline>(); for (int m = 0; m < groups.size(); m++) { //dates.add(groups.get(i).getDate()); int thisyear = getYear(groups.get(m).getDate()); int thismonth = getMonth(groups.get(m).getDate()); if (thisyear - 1900 == calendarday.getYear()) { if (thismonth - 1 == calendarday.getMonth()) { thisMonths.add(groups.get(m)); } } } for (int d = 0; d < thisMonths.size(); d++) { if (day == getDay(thisMonths.get(d).getDate())) { // if (thisMonths.get(d).getState() == 0) { // txtvPrice.setText(""); // } else if (Integer.parseInt(thisMonths.get(d).getStock()) > 0) { txtvPrice.setText("¥" + thisMonths.get(d).getSell_price_adult()); } else { txtvPrice.setText("售罄"); } } } } txtvDay.setText(Integer.toString(day)); // 当天 if (thisday.getDate() == day && thisday.getMonth() == calendarday.getMonth() && thisday.getYear() == calendarday.getYear()) { txtvDay.setText("今天"); txtvDay.setTextColor(COLOR_TX_WEEK_TITLE); txtvPrice.setTextColor(COLOR_TX_PRICE); ((LinearLayout) view.getParent()).setBackgroundResource(R.drawable.calendar_day_bg); } else { txtvDay.setTextColor(COLOR_TX_THIS_MONTH_DAY); txtvPrice.setTextColor(COLOR_TX_PRICE); ((LinearLayout) view.getParent()).setBackgroundResource(R.drawable.calendar_day_bg); } // 上面首先设置了一下默认的"当天"背景色,当有特殊需求时,才给当日填充背景色 // 设置日期背景色 if (dayBgColorMap.get(dates[i][j]) != null) { txtvDay.setTextColor(Color.WHITE); txtvPrice.setTextColor(Color.WHITE); ((LinearLayout) view.getParent()).setBackgroundColor(dayBgColorMap .get(dates[i][j])); } // 设置标记 setMarker(group, i, j); day++; // 下个月 } else { TextView txtvDay = null; TextView txtvPrice = null; if (view. getChildCount() > 0) { txtvDay = (TextView) view.getChildAt(0); txtvPrice = (TextView) view.getChildAt(1); } else { LinearLayout.LayoutParams paramsDay = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.5f); txtvDay = new TextView(getContext()); txtvPrice = new TextView(getContext()); txtvDay.setLayoutParams(paramsDay); txtvDay.setGravity(Gravity.CENTER); txtvDay.setTextSize(10); LinearLayout.LayoutParams paramsPrice = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f); txtvPrice.setLayoutParams(paramsDay); txtvPrice.setGravity(Gravity.CENTER); txtvPrice.setTextSize(10); // view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.calendar_day_item, null); view.addView(txtvDay); view.addView(txtvPrice); } // TextView txtvDay = (TextView) view.findViewById(R.id.txtv_day_calendaritem); if (calendarday.getMonth() == Calendar.DECEMBER) { dates[i][j] = format(new Date( calendarday.getYear() + 1, Calendar.JANUARY, nextMonthDay)); } else { dates[i][j] = format(new Date( calendarday.getYear(), calendarday.getMonth() + 1, nextMonthDay)); } txtvDay.setText(Integer.toString(nextMonthDay)); txtvDay.setTextColor(COLOR_TX_OTHER_MONTH_DAY); txtvPrice.setTextColor(COLOR_TX_OTHER_MONTH_DAY); // 设置日期背景色 if (dayBgColorMap.get(dates[i][j]) != null) { // view.setBackgroundResource(dayBgColorMap // .get(dates[i][j])); } else { ((LinearLayout) view.getParent()).setBackgroundResource(R.drawable.calendar_day_bg); } // 设置标记 setMarker(group, i, j); nextMonthDay++; } } } } }