这是之情在网上找的一个例子,具体是谁写的忘记了。之后自己稍稍做了一些改动。设计了一个关于考勤的日历显示方案。

Android 自定义日历 库 安卓自定义日历控件_自定义

日历星期一到星期日的抬头自定义view 

1 public class WeekDayView extends View {
  2     //上横线颜色
  3     private int mTopLineColor = Color.parseColor("#CCE4F2");
  4     //下横线颜色
  5     private int mBottomLineColor = Color.parseColor("#CCE4F2");
  6     //周一到周五的颜色
  7     private int mWeedayColor = Color.parseColor("#1FC2F3");
  8     //周六、周日的颜色
  9     private int mWeekendColor = Color.parseColor("#fa4451");
 10     //线的宽度
 11     private int mStrokeWidth = 4;
 12     private int mWeekSize = 14;
 13     private Paint paint;
 14     private DisplayMetrics mDisplayMetrics;
 15     private String[] weekString = new String[]{"日","一","二","三","四","五","六"};
 16 
 17     public WeekDayView(Context context) {
 18         super(context);
 19         mDisplayMetrics = getResources().getDisplayMetrics();
 20         paint = new Paint();
 21     }
 22 
 23     public WeekDayView(Context context, AttributeSet attrs) {
 24         super(context, attrs);
 25         mDisplayMetrics = getResources().getDisplayMetrics();
 26         paint = new Paint();
 27     }
 28 
 29     @Override
 30     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 31         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 32         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 33         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
 34 
 35         int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 36         int heightMode = MeasureSpec.getMode(heightMeasureSpec);
 37 
 38 //        int width = getMySize(100, widthMeasureSpec);
 39 //        int height = getMySize(100, heightMeasureSpec);
 40 
 41         if(heightMode == MeasureSpec.AT_MOST){
 42             heightSize = mDisplayMetrics.densityDpi * 30;
 43         }
 44         if(widthMode == MeasureSpec.AT_MOST){
 45             widthSize = mDisplayMetrics.densityDpi * 300;
 46         }
 47         setMeasuredDimension(widthSize, heightSize);
 48     }
 49 
 50     private int getMySize(int defaultSize, int measureSpec) {
 51         int mySize = defaultSize;
 52 
 53         int mode = MeasureSpec.getMode(measureSpec);
 54         int size = MeasureSpec.getSize(measureSpec);
 55 
 56         switch (mode) {
 57             case MeasureSpec.UNSPECIFIED: {//如果没有指定大小,就设置为默认大小
 58                 mySize = defaultSize;
 59                 break;
 60             }
 61             case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size
 62                 //我们将大小取最大值,你也可以取其他值
 63                 mySize = size;
 64                 break;
 65             }
 66             case MeasureSpec.EXACTLY: {//如果是固定的大小,那就不要去改变它
 67                 mySize = size;
 68                 break;
 69             }
 70         }
 71         return mySize;
 72     }
 73 
 74     @Override
 75     protected void onDraw(Canvas canvas) {
 76         int width = getWidth();
 77         int height = getHeight();
 78         //进行画上下线
 79         paint.setStyle(Paint.Style.STROKE);
 80         paint.setColor(mTopLineColor);
 81         paint.setStrokeWidth(mStrokeWidth);
 82 //        canvas.drawLine(0, 0, width, 0, paint);
 83 
 84         //画下横线
 85         paint.setColor(mBottomLineColor);
 86 //        canvas.drawLine(0, height, width, height, paint);
 87         paint.setStyle(Paint.Style.FILL);
 88         paint.setTextSize(mWeekSize * mDisplayMetrics.scaledDensity);
 89         int columnWidth = width / 7;
 90         for(int i=0;i < weekString.length;i++){
 91             String text = weekString[i];
 92             int fontWidth = (int) paint.measureText(text);
 93             int startX = columnWidth * i + (columnWidth - fontWidth)/2;
 94             int startY = (int) (height/2 - (paint.ascent() + paint.descent())/2);
 95             if(text.indexOf("日") > -1|| text.indexOf("六") > -1){
 96                 paint.setColor(mWeekendColor);
 97             }else{
 98                 paint.setColor(mWeedayColor);
 99             }
100             canvas.drawText(text, startX, startY, paint);
101         }
102     }
103 
104     /**
105      * 设置顶线的颜色
106      * @param mTopLineColor
107      */
108     public void setmTopLineColor(int mTopLineColor) {
109         this.mTopLineColor = mTopLineColor;
110     }
111 
112     /**
113      * 设置底线的颜色
114      * @param mBottomLineColor
115      */
116     public void setmBottomLineColor(int mBottomLineColor) {
117         this.mBottomLineColor = mBottomLineColor;
118     }
119 
120     /**
121      * 设置周一-五的颜色
122      * @return
123      */
124     public void setmWeedayColor(int mWeedayColor) {
125         this.mWeedayColor = mWeedayColor;
126     }
127 
128     /**
129      * 设置周六、周日的颜色
130      * @param mWeekendColor
131      */
132     public void setmWeekendColor(int mWeekendColor) {
133         this.mWeekendColor = mWeekendColor;
134     }
135 
136     /**
137      * 设置边线的宽度
138      * @param mStrokeWidth
139      */
140     public void setmStrokeWidth(int mStrokeWidth) {
141         this.mStrokeWidth = mStrokeWidth;
142     }
143     /**
144      * 设置字体的大小
145      * @param mWeekSize
146      */
147     public void setmWeekSize(int mWeekSize) {
148         this.mWeekSize = mWeekSize;
149     }
150     /**
151      * 设置星期的形式
152      * @param weekString
153      * 默认值 "日","一","二","三","四","五","六"
154      */
155     public void setWeekString(String[] weekString) {
156         this.weekString = weekString;
157     }
158 }

日历的自定义veiw 

1 public class MonthDateView extends View {
  2     private static final int NUM_COLUMNS = 7;
  3     private static final int NUM_ROWS = 6;
  4     private Paint mPaint;
  5     private int textColor = Color.parseColor("#232323");
  6     private int mDayColor = Color.parseColor("#e7e7e7");//灰色
  7     private int mSelectDayColor = Color.parseColor("#a6f297");//绿色
  8     private int mSelectBGColor = Color.parseColor("#b5ecff");//蓝色
  9     private int mCurrentColor = Color.parseColor("#ffdeb4");//橙色
 10     private int mTextColor = Color.parseColor("#333333");//灰色
 11     private int mTextColor1 = Color.parseColor("#FFFFFF");//黑色
 12     private int mCurrYear,mCurrMonth,mCurrDay;
 13     private int mSelYear,mSelMonth,mSelDay;
 14     private int mColumnSize,mRowSize;
 15     private DisplayMetrics mDisplayMetrics;
 16     private int mDaySize = 14;
 17     private TextView tv_date,tv_week;
 18     private int weekRow;
 19     private int [][] daysString;
 20     private int mCircleRadius = 6;
 21     private DateClick dateClick;
 22     private int mCircleColor = Color.parseColor("#ff0000");
 23     private List<Integer> daysHasThingList;
 24     private Map<String,String> valuesMap;
 25     private String[] sd;
 26     private String[] ed;
 27     private DayOnClickLinser linser;
 28 
 29 
 30     public MonthDateView(Context context, AttributeSet attrs) {
 31         super(context, attrs);
 32         mDisplayMetrics = getResources().getDisplayMetrics();
 33         Calendar calendar = Calendar.getInstance();
 34         calendar.setTime(new Date());
 35         mPaint = new Paint();
 36         //去锯齿
 37         mPaint.setAntiAlias(true);
 38         mPaint.setStrokeWidth((float) 4.0);
 39         mPaint.setStyle(Paint.Style.STROKE);
 40         mCurrYear = calendar.get(Calendar.YEAR);
 41         mCurrMonth = calendar.get(Calendar.MONTH)+1;
 42         mCurrDay = calendar.get(Calendar.DATE);
 43         setSelectYearMonth(mCurrYear,mCurrMonth,mCurrDay);
 44     }
 45 
 46     @Override
 47     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 48         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 49         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
 50 
 51         int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 52         int heightMode = MeasureSpec.getMode(heightMeasureSpec);
 53 
 54         if(heightMode == MeasureSpec.AT_MOST){
 55             heightSize = mDisplayMetrics.densityDpi * 200;
 56         }
 57         if(widthMode == MeasureSpec.AT_MOST){
 58             widthSize = mDisplayMetrics.densityDpi * 300;
 59         }
 60         setMeasuredDimension(widthSize, heightSize);
 61     }
 62 
 63     @Override
 64     protected void onDraw(Canvas canvas) {
 65         initSize();
 66         daysString = new int[6][7];//日期数字6行7列
 67         mPaint.setTextSize(mDaySize*mDisplayMetrics.scaledDensity);
 68         mPaint.setColor(mTextColor1);
 69         int dayString;
 70         int mMonthDays = DateUtil.getMonthDays(mSelYear, mSelMonth);
 71         int weekNumber = DateUtil.getFirstDayWeek(mSelYear, mSelMonth);
 72         Log.d("DateView", "DateView:" + mSelMonth+"月1号周" + weekNumber);
 73         for(int day = 0;day < mMonthDays;day++){
 74             dayString = day + 1;
 75             int column = (day+weekNumber) % 7;
 76             int row = (day+weekNumber ) / 7;
 77             daysString[row][column]=day + 1;
 78             int startX = (int) (mColumnSize * column + (mColumnSize - mPaint.measureText(dayString+""))/2);
 79             int startY = (int) (mRowSize * row + mRowSize/2 - (mPaint.ascent() + mPaint.descent())/2);
 80 
 81             //绘制背景色矩形
 82             int startRecX = mColumnSize * column;
 83             int startRecY = mRowSize * row;
 84             int cx = startRecX+mColumnSize/2;
 85             int cy = startRecY+mRowSize/2;
 86 //            if(dayString==mSelDay){
 87 //                int endRecX = startRecX + mColumnSize;
 88 //                int endRecY = startRecY + mRowSize;
 89 //                mPaint.setColor(mSelectBGColor);
 90 //                canvas.drawRect(startRecX, startRecY, endRecX, endRecY, mPaint);
 91 //                //绘制圆形
 92 //                canvas.drawCircle(cx, cy, 30, mPaint);
 93 //                //记录第几行,即第几周
 94 //                weekRow = row + 1;
 95 //            }
 96 // 绘制事务圆形标志
 97 //            drawCircle(row,column,day + 1,canvas);
 98             String time = null;
 99             if (dayString<=mCurrDay||mSelMonth<mCurrMonth||mSelYear<mCurrYear){
100                 String day1 =null;
101                 String month = null;
102                 if (dayString<10){
103                     day1 = "0"+dayString;
104                 }else {
105                     day1 = dayString+"";
106                 }
107                 if (mSelMonth<10){
108                     month = "0"+mSelMonth;
109                 }else {
110                     month = mSelMonth+"";
111                 }
112                 time = mSelYear+"-"+month+"-"+day1;
113                 String value = null;
114                 if(valuesMap!=null) {
115                     value  = valuesMap.get(time);
116                 }
117                 if(dayString == mSelDay){
118                     mPaint.setColor(mSelectDayColor);
119                     mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
120                     canvas.drawCircle(cx, cy, 40, mPaint);
121                     weekRow = row + 1;
122                 }else if(value!=null&&value.equals("2")){//未签到日期为灰色圈
123                     mPaint.setColor(mDayColor);
124                     mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
125                     canvas.drawCircle(cx, cy, 40, mPaint);
126                 }else if (value!=null&&value.equals("3")){//外部签到日期为橙色圈
127                     mPaint.setColor(mCurrentColor);
128                     mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
129                     canvas.drawCircle(cx, cy, 40, mPaint);
130                 }else if (value!=null&&value.equals("1")){//内部签到日期为蓝色圈
131                     mPaint.setColor(mSelectBGColor);
132                     mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
133                     canvas.drawCircle(cx, cy, 40, mPaint);
134                 }else {//否则没有圈
135 
136                 }
137             }
138             mPaint.setColor(textColor);
139             mPaint.setStyle(Paint.Style.FILL);
140             canvas.drawText(dayString+"", startX, startY, mPaint);
141             if (sd!=null&&sd.length>=3){
142                 if (mSelYear==Integer.valueOf(sd[0])&&mSelMonth==Integer.valueOf(sd[1])&&(day+1)==Integer.valueOf(sd[2])){
143                     mPaint.setTextSize(10*mDisplayMetrics.scaledDensity);
144                     mPaint.setColor(mTextColor);
145                     int x = (int) (mPaint.measureText("开始")/2);
146                     canvas.drawText("开始",startX-x,startY+40,mPaint);
147                     mPaint.setTextSize(mDaySize*mDisplayMetrics.scaledDensity);
148                 }
149             }
150             if (ed!=null&&ed.length>=3){
151                 if (mSelYear==Integer.valueOf(ed[0])&&mSelMonth==Integer.valueOf(ed[1])&&(day+1)==Integer.valueOf(ed[2])){
152                     mPaint.setTextSize(10*mDisplayMetrics.scaledDensity);
153                     mPaint.setColor(mTextColor);
154                     canvas.drawText("结束",startX-5,startY+40,mPaint);
155                     mPaint.setTextSize(mDaySize*mDisplayMetrics.scaledDensity);
156                 }
157             }
158 
159             if(tv_date != null){
160                 tv_date.setText(mSelYear + "年" + (mSelMonth + 1) + "月");
161             }
162             if(tv_week != null){
163                 tv_week.setText("第" + weekRow +"周");
164             }
165         }
166     }
167 
168     private void drawCircle(int row,int column,int day,Canvas canvas){
169         if(daysHasThingList != null && daysHasThingList.size() >0){
170             if(!daysHasThingList.contains(day))return;
171             mPaint.setColor(mCircleColor);
172             float circleX = (float) (mColumnSize * column + mColumnSize*0.8);
173             float circley = (float) (mRowSize * row + mRowSize*0.2);
174             canvas.drawCircle(circleX, circley, mCircleRadius, mPaint);
175         }
176     }
177 
178     public DayOnClickLinser getLinser() {
179         return linser;
180     }
181 
182     public void setLinser(DayOnClickLinser linser) {
183         this.linser = linser;
184     }
185 
186     @Override
187     public boolean performClick() {
188         return super.performClick();
189     }
190 
191     private int downX = 0,downY = 0;
192     @Override
193     public boolean onTouchEvent(MotionEvent event) {
194         int eventCode= event.getAction();
195         switch(eventCode){
196             case MotionEvent.ACTION_DOWN:
197                 downX = (int) event.getX();
198                 downY = (int) event.getY();
199                 break;
200             case MotionEvent.ACTION_MOVE:
201                 break;
202             case MotionEvent.ACTION_UP:
203                 int upX = (int) event.getX();
204                 int upY = (int) event.getY();
205                 if(Math.abs(upX-downX) < 10 && Math.abs(upY - downY) < 10){//点击事件
206                     performClick();
207                     doClickAction((upX + downX)/2,(upY + downY)/2);
208                     if (isCurrent()){
209                         linser.isDayClick(isCurrent());
210                     }
211                 }
212                 break;
213         }
214         return true;
215     }
216 
217     /**
218      * 初始化列宽行高
219      */
220     private void initSize(){
221         mColumnSize = getWidth() / NUM_COLUMNS;
222         mRowSize = getHeight() / NUM_ROWS;
223     }
224 
225     /**
226      * 设置年月
227      * @param year
228      * @param month
229      */
230     public void setSelectYearMonth(int year,int month,int day){
231         mSelYear = year;
232         mSelMonth = month;
233         mSelDay = day;
234     }
235     /**
236      * 执行点击事件
237      * @param x
238      * @param y
239      */
240     private void doClickAction(int x,int y){
241         int row = y / mRowSize;
242         int column = x / mColumnSize;
243         if (row<daysString.length&&column<daysString[row].length){
244             setSelectYearMonth(mSelYear,mSelMonth,daysString[row][column]);
245             invalidate();
246             //执行activity发送过来的点击处理事件
247             if(dateClick != null){
248                 dateClick.onClickOnDate(mSelYear,mSelMonth,mSelDay);
249             }
250         }else {
251             return;
252         }
253 
254     }
255 
256     /**
257      * 左点击,日历向后翻页
258      */
259     public void onLeftClick(){
260         int year = mSelYear;
261         int month = mSelMonth;
262         int day = mSelDay;
263         if(month == 1){//若果是1月份,则变成12月份
264             year = mSelYear-1;
265             month = 12;
266         }else if(DateUtil.get(year, month) == day){
267             //如果当前日期为该月最后一点,当向前推的时候,就需要改变选中的日期
268             month = month-1;
269             day = DateUtil.getMonthDays(year, month);
270         }else{
271             month = month-1;
272         }
273         setSelectYearMonth(year,month,day);
274         invalidate();
275     }
276 
277     /**
278      * 右点击,日历向前翻页
279      */
280     public void onRightClick(){
281         int year = mSelYear;
282         int month = mSelMonth;
283         int day = mSelDay;
284         if(month == 12){//若果是12月份,则变成1月份
285             year = mSelYear+1;
286             month = 1;
287         }else if(DateUtil.getMonthDays(year, month) == day){
288             //如果当前日期为该月最后一天,当向前推的时候,就需要改变选中的日期
289             month = month + 1;
290             day = DateUtil.getMonthDays(year, month);
291         }else{
292             month = month + 1;
293         }
294         setSelectYearMonth(year,month,day);
295         invalidate();
296     }
297 
298     public Boolean isCurrent(){
299         boolean isc = false;
300         if (mCurrYear==mSelYear&&mCurrMonth==mSelMonth&&mCurrDay==mSelDay){
301             isc=true;
302         }else {
303             isc=false;
304         }
305         return isc;
306     }
307 
308 
309 
310 
311     public void setStartDay(String startDay) {
312         sd = startDay.split("-");
313 
314     }
315 
316 
317     public void setEndDay(String endDay) {
318         ed = endDay.split("-");
319     }
320 
321     /**
322      * 获取选择的年份
323      * @return
324      */
325     public int getmSelYear() {
326         return mSelYear;
327     }
328     /**
329      * 获取选择的月份
330      * @return
331      */
332     public int getmSelMonth() {
333         return mSelMonth;
334     }
335     /**
336      * 获取选择的日期
337      *
338      */
339     public int getmSelDay() {
340         return this.mSelDay;
341     }
342     /**
343      * 普通日期的字体颜色,默认黑色
344      * @param mDayColor
345      */
346     public void setmDayColor(int mDayColor) {
347         this.mDayColor = mDayColor;
348     }
349 
350     /**
351      * 选择日期的颜色,默认为白色
352      * @param mSelectDayColor
353      */
354     public void setmSelectDayColor(int mSelectDayColor) {
355         this.mSelectDayColor = mSelectDayColor;
356     }
357 
358     /**
359      * 选中日期的背景颜色,默认蓝色
360      * @param mSelectBGColor
361      */
362     public void setmSelectBGColor(int mSelectBGColor) {
363         this.mSelectBGColor = mSelectBGColor;
364     }
365     /**
366      * 当前日期不是选中的颜色,默认红色
367      * @param mCurrentColor
368      */
369     public void setmCurrentColor(int mCurrentColor) {
370         this.mCurrentColor = mCurrentColor;
371     }
372 
373     /**
374      * 日期的大小,默认18sp
375      * @param mDaySize
376      */
377     public void setmDaySize(int mDaySize) {
378         this.mDaySize = mDaySize;
379     }
380 
381     public Map<String, String> getValuesMap() {
382         return valuesMap;
383     }
384 
385     public void setValuesMap(Map<String, String> valuesMap) {
386         this.valuesMap = valuesMap;
387     }
388 
389     /**
390      * 设置显示当前日期的控件
391      * @param tv_date
392      * 显示日期
393      * @param tv_week
394      * 显示周
395      */
396     public void setTextView(TextView tv_date,TextView tv_week){
397         this.tv_date = tv_date;
398         this.tv_week = tv_week;
399         invalidate();
400     }
401 
402     /**
403      * 设置事务天数
404      * @param daysHasThingList
405      */
406     public void setDaysHasThingList(List<Integer> daysHasThingList) {
407         this.daysHasThingList = daysHasThingList;
408     }
409 
410     /***
411      * 设置圆圈的半径,默认为6
412      * @param mCircleRadius
413      */
414     public void setmCircleRadius(int mCircleRadius) {
415         this.mCircleRadius = mCircleRadius;
416     }
417 
418     /**
419      * 设置圆圈的半径
420      * @param mCircleColor
421      */
422     public void setmCircleColor(int mCircleColor) {
423         this.mCircleColor = mCircleColor;
424     }
425 
426     /**
427      * 设置日期的点击回调事件
428      * @author shiwei.deng
429      *
430      */
431     public interface DateClick{
432         public void onClickOnDate(int year,int month,int day);
433     }
434 
435     /**
436      * 设置日期点击事件
437      * @param dateClick
438      */
439     public void setDateClick(DateClick dateClick) {
440         this.dateClick = dateClick;
441     }
442 
443     /**
444      * 跳转至今天
445      */
446     public void setTodayToView(){
447         setSelectYearMonth(mCurrYear,mCurrMonth,mCurrDay);
448         invalidate();
449     }
450 
451     /**
452      * 日历点击回掉接口
453      */
454     public interface  DayOnClickLinser {
455         void  isDayClick(Boolean ok);
456     }
457 }

XML布局文件

1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="250dp"
10         android:background="#ffffff"
11         android:orientation="horizontal">
12         <RelativeLayout
13             android:id="@+id/layout_left"
14             android:layout_width="match_parent"
15             android:layout_height="match_parent"
16             android:layout_weight="7">
17             <ImageView
18                 android:layout_width="match_parent"
19                 android:layout_height="20dp"
20                 android:layout_gravity="center"
21                 android:src="@mipmap/yueleft"
22                 android:layout_centerInParent="true"/>
23         </RelativeLayout>
24 
25         <LinearLayout
26             android:layout_width="match_parent"
27             android:layout_height="match_parent"
28             android:orientation="vertical"
29             android:layout_weight="1">
30             <com.cgssafety.android.views.WeekDayView
31                 android:layout_width="match_parent"
32                 android:layout_height="30dp" />
33             <com.cgssafety.android.views.MonthDateView
34                 android:id="@+id/canlad_mysylfe"
35                 android:layout_width="match_parent"
36                 android:layout_height="match_parent" />
37         </LinearLayout>
38         <RelativeLayout
39             android:id="@+id/layout_right"
40             android:layout_width="match_parent"
41             android:layout_height="match_parent"
42             android:layout_weight="7">
43             <ImageView
44                 android:layout_width="match_parent"
45                 android:layout_height="20dp"
46                 android:layout_gravity="center"
47                 android:layout_centerInParent="true"
48                 android:src="@mipmap/yueright"/>
49         </RelativeLayout>
50 
51     </LinearLayout>
52 
53     <RelativeLayout
54         android:id="@+id/layout_meiqian"
55         android:layout_width="match_parent"
56         android:layout_height="wrap_content"
57         android:background="@color/white"
58         android:padding="5dp">
59         <TextView
60             android:id="@+id/tv_buqian_text"
61             android:layout_width="wrap_content"
62             android:layout_height="wrap_content"
63             android:text="请选择以下任意点签到"
64             android:layout_centerVertical="true"
65             android:textSize="14sp"
66             android:textColor="@color/text_10"
67             android:layout_marginLeft="15dp"/>
68         <TextView
69             android:id="@+id/tv_tijiao"
70             android:layout_width="60dp"
71             android:layout_height="25dp"
72             android:text="提交"
73             android:layout_alignParentRight="true"
74             android:gravity="center"
75             android:layout_marginRight="15dp"
76             android:background="@drawable/buttoncolors"/>
77     </RelativeLayout>
78     <LinearLayout
79         android:layout_width="match_parent"
80         android:layout_height="match_parent"
81         android:background="#f2f2f2">
82         <ListView
83             android:id="@+id/lv_kaqin"
84             android:layout_width="match_parent"
85             android:layout_height="match_parent"
86             android:background="#f2f2f2"
87             android:layout_marginTop="10dp"
88             android:dividerHeight="10dp"
89             android:divider="@null"
90             >
91         </ListView>
92     </LinearLayout>
93 
94 </LinearLayout>

在一个fragment中使用   CountentCalendFragment

1 public class CountentCalendFragment extends Fragment {
  2 
  3     public static final int MODE_INTERNET = 1;
  4     public static final int MODE_BENDI = 2;
  5     public MonthDateView candle;//日历控件
  6     private ListView listView;//地址列表
  7     private UserManager manager;//
  8     private List<KaoQinChelend> list;//子表数据
  9     private List<KaoQinMain> list2;//主表数据
 10     private AdderssAdapter adapter;//地址列表适配器
 11     private RelativeLayout layout;//还未签到按钮
 12     private TextView tv_buqian_text;//签到提示
 13     private TextView tv_tijiao;//提交按钮
 14     private String time ;//当前日历时间
 15     private String currentime;//当天时间
 16     private int mode;//本地或网络模式选择未用
 17     private int list_flag = -1;//列表索引
 18     private RelativeLayout layout_left;
 19     private RelativeLayout layout_right;
 20     private MachineCountentActivity preantActivity;
 21     private MachineDataForInternet machineDataForInternet;
 22     private String adderss;
 23 //    private String day;
 24     private boolean isgin = false;//是否签到标志
 25     private AlertDialog dialog;//弹框控件
 26 
 27     @Nullable
 28     @Override
 29     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 30         View view = inflater.inflate(R.layout.fragment_calend_regiser,null);
 31         initData();
 32         initview(view);
 33         initOnClick();
 34         preantActivity = (MachineCountentActivity) getActivity();
 35         upData(time);
 36         return view;
 37     }
 38     private void initData(){
 39         manager = UserManager.getUserManager(getContext());
 40         list = new ArrayList<>();
 41         list2 = new ArrayList<>();
 42         SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
 43         time = format1.format(new Date());
 44         currentime = format1.format(new Date());
 45     }
 46     private void initview(View view){
 47         listView = (ListView) view.findViewById(R.id.lv_kaqin);
 48         candle = (MonthDateView) view.findViewById(R.id.canlad_mysylfe);
 49         candle.setTodayToView();
 50         layout = (RelativeLayout) view.findViewById(R.id.layout_meiqian);
 51         layout_left = (RelativeLayout) view.findViewById(R.id.layout_left);
 52         layout_right = (RelativeLayout) view.findViewById(R.id.layout_right);
 53         tv_buqian_text = (TextView) view.findViewById(R.id.tv_buqian_text);
 54         tv_tijiao = (TextView) view.findViewById(R.id.tv_tijiao);
 55         candle.setStartDay(DateUtil.getStrTime(CgssafetyApp.xmdid.getRcdrq(),"yyyy-MM-dd"));
 56         candle.setEndDay(DateUtil.getStrTime(CgssafetyApp.xmdid.getRsdrq(),"yyyy-MM-dd"));
 57     }
 58 
 59     private void initOnClick(){
 60         candle.setDateClick(new MonthDateView.DateClick() {
 61             @Override
 62             public void onClickOnDate(int year, int month, int day) {
 63                 String day1 = null;
 64                 String moth1 = null;
 65                 if (day<10){
 66                     day1 = "0"+day;
 67                 }else{
 68                     day1 = day+"";
 69                 }
 70                 if (month<10){
 71                     moth1 = "0"+month;
 72                 }else {
 73                     moth1 = month+"";
 74                 }
 75                 time = year+"-"+moth1+"-"+day1;
 76                 if (mode==MODE_INTERNET){
 77                     upDataInternet(time);
 78                 }else {
 79                     upData(time);
 80                 }
 81                 Log.e("tag", "onClickOnDate: "+list.toString() );
 82             }
 83         });
 84         tv_tijiao.setOnClickListener(new View.OnClickListener() {
 85             @Override
 86             public void onClick(View v) {
 87                 if (list_flag!=-1){
 88                     if (dialog == null) {
 89                         dialog = new AlertDialog.Builder(getContext()).setTitle("提示")//设置对话框标题
 90                                 .setMessage("请确认签到")//设置显示的内容
 91                                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
 92                                     @Override
 93                                     public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
 94                                         SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 95                                         String buqday = DateUtil.Calendar2Str(DateUtil.str2Calendar(list.get(list_flag).getDateId(),"yyyy-MM-dd"),"yyyy-MM-dd");
 96                                         int isbq = 0;
 97                                         if (currentime.equals(buqday)){
 98                                             isbq = 0;
 99                                         }else {
100                                             isbq = 1;
101                                         }104                                     }
105                                 }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
106                                     @Override
107                                     public void onClick(DialogInterface dialog, int which) {//响应事件
108 
109                                     }
110                                 }).show();//在按键响应事件中显示此对话框
111                     } else {
112                         dialog.show();
113                     }
114                 }else {
115                     Toast.makeText(getContext(),"请选择签到地点",Toast.LENGTH_LONG).show();
116                 }
117             }
118         });
119         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
120             @Override
121             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
122                 list_flag = position;
123                 adapter.notifyDataSetChanged();
124             }
125         });
126         layout_left.setOnClickListener(new View.OnClickListener() {
127             @Override
128             public void onClick(View v) {
129                 preantActivity.OnClick_left();
130             }
131         });
132         layout_right.setOnClickListener(new View.OnClickListener() {
133             @Override
134             public void onClick(View v) {
135                 preantActivity.OnClick_right();
136             }
137         });
138         candle.setLinser(new MonthDateView.DayOnClickLinser() {
139             @Override
140             public void isDayClick(Boolean ok) {
141 
142             }
143         });
144     }
145 
146     public void setAdderss(String adderss) {
147         this.adderss = adderss;
148     }
149 
150     public void setValuseMaoForCandle(Map<String,String> map){
151         candle.setValuesMap(map);
152     }
153 
154     public void clearListView(){
155         if (list!=null&&adapter!=null){
156             list.clear();
157             adapter.notifyDataSetChanged();
158         }
159     }
160 
161     private void upData(String time){
162         list = manager.selectTableSingSon(time);
163         list2 =  manager.selectTableSignMomAboutTime(time);
164         String isok = null;
165         if (list2.size()>0){//当天有轨迹记录
166             isok = list2.get(0).getIsSign();
167             layout.setVisibility(View.VISIBLE);
168             if (isok.equals("true")){//判断是否有签到记录
169                 tv_tijiao.setBackgroundResource(R.drawable.btn_undble_colors);
170                 tv_buqian_text.setTextColor(getResources().getColor(R.color.text_unable));
171                 tv_buqian_text.setText(time+"日已签到");
172                 tv_tijiao.setText("已签");
173                 tv_tijiao.setVisibility(View.VISIBLE);
174                 isgin = true;
175                 listView.setEnabled(false);
176                 tv_tijiao.setClickable(false);
177             }else {
178                 tv_buqian_text.setText("请选择以下任意点签到");
179                 tv_tijiao.setVisibility(View.VISIBLE);
180             }
181         }else{
182             if (time.equals(currentime)){//传入时间是否为当前时间
183                 tv_buqian_text.setText(time);
184                 tv_tijiao.setText("签到");
185                 tv_tijiao.setBackgroundResource(R.drawable.buttoncolors);
186                 tv_tijiao.setVisibility(View.VISIBLE);
187                 isgin = false;
188                 tv_buqian_text.setTextColor(getResources().getColor(R.color.text_10));
189                 listView.setEnabled(true);
190                 tv_tijiao.setClickable(true);
191             }else {
192                 tv_buqian_text.setText(time+"未查询到记录");
193                 tv_tijiao.setVisibility(View.GONE);
194                 tv_buqian_text.setTextColor(getResources().getColor(R.color.text_unable));
195             }
196 
197         }
198         if (time.equals(currentime)){
199             if (preantActivity.getMyLocation()!=null){
200                 KaoQinChelend qinChelend =new KaoQinChelend();
201                 qinChelend.setIsSelect("false");
202                 qinChelend.setAddress(preantActivity.getMyLocation().getAddrStr());
203                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
204                 String day = format.format(new Date());
205                 qinChelend.setDateId(day);
206                 qinChelend.setLat(LatLogUtil.interceptionFive(preantActivity.getMyLocation().getLatitude())+"");
207                 qinChelend.setLog(LatLogUtil.interceptionFive(preantActivity.getMyLocation().getLongitude())+"");
208                 list.add(qinChelend);
209             }
210         }
211         Collections.reverse(list);
212         if (adapter==null){
213             adapter = new AdderssAdapter();
214             listView.setAdapter(adapter);
215         }else {
216             adapter.notifyDataSetChanged();
217         }
218     }
219 
220 
221     public void upDataFormActivity(){
222         upData(time);
223     }
224 
225     public void upDataInternet(String day){
226         list.clear();
227         KaoQinInternetData.SignListBean bean =MachineDataForInternet.map2.get(day);
228         if (bean!=null){
229             KaoQinChelend chelend = new KaoQinChelend();
230             chelend.setLat(bean.getLat());
231             chelend.setAddress(bean.getSignPlace());
232             chelend.setLog(bean.getLongitude());
233             chelend.setDateId(DateUtil.getStrTime(bean.getSignDate(),"yyyy-MM-dd HH:mm:ss"));
234             chelend.setIsSelect("false");
235             list.add(chelend);
236         }
237         if (adapter==null){
238             adapter = new AdderssAdapter();
239             listView.setAdapter(adapter);
240         }else {
241             adapter.notifyDataSetChanged();
242         }
243     }
244     public String getDate(){
245         String str = null;
246         String day1 = null;
247         String moth = null;
248         int year = candle.getmSelYear();
249         int month = candle.getmSelMonth();
250         int day = candle.getmSelDay();
251         if (day<10){
252             day1 = "0"+day;
253         }else {
254             day1 = day+"";
255         }
256         if (month<10){
257             moth = "0"+month;
258         }else {
259             moth = month+"";
260         }
261         str = year+"-"+moth+"-"+day1;
262         return str;
263     }
264 
265     class AdderssAdapter extends BaseAdapter {
266 
267         @Override
268         public int getCount() {
269             return list.size();
270         }
271 
272         @Override
273         public Object getItem(int position) {
274             return list.get(position);
275         }
276 
277         @Override
278         public long getItemId(int position) {
279             return position;
280         }
281 
282         @Override
283         public View getView(final int position, View convertView, ViewGroup parent) {
284             ViewHolder holder ;
285             if (convertView==null){
286                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_kaoqin_weizhi,null);
287                 holder = new ViewHolder(convertView);
288                 convertView.setTag(holder);
289             }else {
290                 holder = (ViewHolder) convertView.getTag();
291             }
292             holder.tv_address.setText(list.get(position).getAddress());
293             holder.tv_time.setText(DateUtil.getStrTime(DateUtil.getTime(list.get(position).getDateId())+"","HH:mm"));
294             holder.tv_num.setText((position+1)+"");
295             holder.tv_log.setText("经度:"+list.get(position).getLog());
296             holder.tv_lat.setText("纬度:"+list.get(position).getLat());
297             if (isgin){
298                 if (list.get(position).getIsSelect().equals("true")){
299                     holder.iv_check.setImageResource(R.mipmap.xuanzhong);
300                 }else {
301                     holder.iv_check.setImageResource(R.mipmap.weixuna);
302                     holder.iv_check.setVisibility(View.GONE);
303                 }
304             }else {
305                 if (position == list_flag){
306                     holder.iv_check.setImageResource(R.mipmap.xuanzhong);
307                 }else {
308                     holder.iv_check.setImageResource(R.mipmap.weixuna);
309                 }
310             }
311             return convertView;
312         }
313         class ViewHolder {
314             TextView tv_time;
315             ImageView iv_check;
316             TextView tv_address;
317             TextView tv_num;
318             TextView tv_log;
319             TextView tv_lat;
320             public ViewHolder(View view) {
321                 tv_time = (TextView) view.findViewById(R.id.tv_dang_temp);
322                 iv_check = (ImageView) view.findViewById(R.id.iv_kaoqin_check_ok);
323                 tv_address = (TextView) view.findViewById(R.id.kaoqin_ceurrent_address);
324                 tv_num = (TextView) view.findViewById(R.id.tv_number);
325                 tv_log = (TextView) view.findViewById(R.id.tv_log);
326                 tv_lat = (TextView) view.findViewById(R.id.tv_lat);
327             }
328         }
329     }
330 
331 }