CalendarPickerView 源码导读

来源:互联网 发布:2017淘宝详情页尺寸 编辑:程序博客网 时间:2024/06/05 12:02
CalendarPickerView 源码解析

项目相关
TimesSquare是一个开源的日历控件。
其项目托管地址是:https://github.com/square/android-times-square

使用方法可参见:http://blog.csdn.net/songzhiyong1121/article/details/8620670

先看一下程序Sample运行截图:

libs的程序结构如下:

很简单,基本上从命名上就可以区分每个类的作用。下面我用一张图来帮大家梳理一下这些类的作用:


上面的图片中我们可以看出CalendarPickerViewListView的子类,那么就一定会有一个Adapter用来为它适配数据,我们就先从这个适配器说起——MonthAdapter 

  1. private class MonthAdapter extends BaseAdapter {
  2.                 private final LayoutInflater inflater;
  3.                 ... ...

  4.                 @Override
  5.                 public View getView(int position, View convertView, ViewGroup parent) {
  6.                         MonthView monthView = (MonthView) convertView;
  7.                         if (monthView == null) {
  8.                                 monthView = MonthView.create(parent, inflater, weekdayNameFormat,
  9.                                                 listener, today);
  10.                         }
  11.                         monthView.init(months.get(position), cells.get(position));
  12.                         return monthView;
  13.                 }
  14. }
复制代码



正如上图所示getView返回的视图就是MonthView。下面我们看一下MonthViewcreate()方法:

  1. public static MonthView create(ViewGroup parent, LayoutInflater inflater,
  2.                         DateFormat weekdayNameFormat, Listener listener, Calendar today) {
  3.                 final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent,
  4.                                 false);

  5.                 final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

  6.                 //========
  7.                 // 配置grid header,比如:周一、周日这些文字
  8.                 //========
  9.                 int firstDayOfWeek = today.getFirstDayOfWeek();
  10.                 final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
  11.                 for (int offset = 0; offset < 7; offset++) {
  12.                         today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
  13.                         final TextView textView = (TextView) headerRow.getChildAt(offset);
  14.                         textView.setText(weekdayNameFormat.format(today.getTime()));
  15.                 }
  16.                 ... ...
  17.         }
复制代码


create方法里面只是配置了grid header的文字,并没有把相应的日期对应到布局上去,那么是在什么地方真正负责日期与布局的对应呢?在上面CalendarPickerView的适配器的getView方法中除了调用MonthViewcreate方法创建了MonthView 对象,还调用了init方法,我们来看一看这个方法:


  1. public void init(MonthDescriptor month, List<List<MonthCellDescriptor>> cells) {
  2.         ... ...
  3.                 final int numRows = cells.size();
  4.                 grid.setNumRows(numRows);
  5.                 for (int i = 0; i < 6; i++) {//at most 5 rows per month
  6.                         CalendarRowView weekRow = (CalendarRowView) grid.getChildAt(i + 1);
  7.                         weekRow.setListener(listener);
  8.                         if (i < numRows) {
  9.                                 weekRow.setVisibility(VISIBLE);
  10.                                 List<MonthCellDescriptor> week = cells.get(i);
  11.                                 for (int c = 0; c < week.size(); c++) {
  12.                                         MonthCellDescriptor cell = week.get(c);
  13.                                         CalendarCellView cellView = (CalendarCellView) weekRow.getChildAt(c);

  14.                                         cellView.setText(Integer.toString(cell.getValue()));
  15.                                         ... ...
  16.                                 }
  17.                         } else {
  18.                                 weekRow.setVisibility(GONE);
  19.                         }
  20.                 }
  21.                 
  22.         }
复制代码


果然这里就是日期与布局绑定的地方。这里cells是一个List<List<MonthCellDescriptor>>类型的数据,其中MonthCellDescriptor这个就是一个对每月中的每一天进行描述的实体类,cells表示一整个月的日期(为了填满这个表格我们还会显示上个月和下个月的日期),而cells列表中的每一项恰好是一个星期的日期。这样我们就可以把日期绑定到CalendarCellView 这个最小的TextView显示日期了。

至此我们就简单的了解一下TimesSquare源码结构。本套开源框架实现起来并不复杂,很容易理解,只要细心阅读大家都可以读懂,也可以在此基础上开发满足自己需求的控件。

最后提供一下最近下载的项目文件,建议大家还是项目托管处下载最新源码。
 android-times-square-master.zip (242.91 KB, 下载次数: 186)

0 0
原创粉丝点击