




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】自定義日歷控件-——Android
這幾天公司出了簽到的需求,讓我來做簽到這一塊,剛開始以為非常好做,不就是調(diào)用系統(tǒng)的日歷嗎。系統(tǒng)的日歷與簽到標(biāo)簽基于足夠完成了。我首先用Android5.0以上的機(jī)器調(diào)用了日歷控件,發(fā)現(xiàn)還不錯(cuò),但是用了一臺4.2的機(jī)器調(diào)用日歷。我靠!這丑不拉幾,看著都要吐了還能用?沒有辦法,我只能嘗試自定義日歷控件了。
下面是設(shè)計(jì)給的我第一張圖:
剛開始我想的是可以畫為6行7列的網(wǎng)格,GridView可以實(shí)現(xiàn),正準(zhǔn)備做,可是下午設(shè)計(jì)又給了我一張圖:
大白圈是今天的日期,小白點(diǎn)是這個(gè)月已簽過到的日期,黑色字體是已經(jīng)過去的日期,灰色是還沒有到來的日期。這變化有點(diǎn)大啊,這要的話用GridView就得區(qū)分好幾種不同的Type了,如果以后還有其他加的不是會更麻煩,然后只能自己畫View這樣,自己對這個(gè)View很熟悉,以后要添加什么功能可以很清楚的加上代碼。
于是我就在網(wǎng)上搜Android自定義日歷控件,當(dāng)我在CSDN上看到zou128865寫的AndroidUI-自定義日歷控件正好能滿足我目前的需求,于是我就把他的代碼下載下來,仔細(xì)的研究。在他的代碼上修改變成我自己的日歷控件。好啦廢話不多說了,開始我們的日歷控件之旅吧。
當(dāng)我要做完這個(gè)控件的時(shí)候,設(shè)計(jì)發(fā)來了最終稿的動態(tài)圖:不能上傳完整的動態(tài)不然老板搞死我,見諒哈。
不會上傳動態(tài)圖呀?。。。。。?/p>
來開始代碼:1.CalendarView:package
oct.mama.view;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.ViewConfiguration;
import
java.util.List;
import
oct.mama.R;
import
oct.mama.model.CustomDateModel;
import
oct.mama.utils.CheckInUtils;
import
oct.mama.utils.CountDownTimerUtils;
/**
*
日歷展示控件
*
Created
by
Administrator
on
2015/10/29.
*/
public
class
CalendarView
extends
View
{
private
static
final
int
TOTAL_COL
=
7;
//
7列
private
static
final
int
TOTAL_ROW
=
6;
//
6行
private
Paint
mCirclePaint;
//
繪制圓形的畫筆
private
Paint
mTextPaint;
//
繪制文本的畫筆
private
int
mViewWidth;
//
視圖的寬度
private
int
mViewHeight;
//
視圖的高度
private
int
mCellSpace;
//
單元格列間距
private
int
mCellRowSpace;
//
單元行間距
private
Paint
mRowTextPaint;
private
Row
rows[]
=
new
Row[TOTAL_ROW];
//
行數(shù)組,每個(gè)元素代表一行
private
static
CustomDateModel
mShowDate;
//
自定義的日期,包括year,month,day
private
OnCellClickListener
mCellClickListener;
//
單元格點(diǎn)擊回調(diào)事件
private
int
touchSlop;
//事件觸發(fā)最小距離
private
Cell
mClickCell;
private
float
mDownX;
private
float
mDownY;
private
int
circleRadius;
private
float
currentDayX;
private
float
currentDayY;
private
int
currentMonthDays;
//
當(dāng)前月天數(shù)
private
List<Long>
checkInDays;
//
服務(wù)器傳來已簽過的days
private
int
checkIndex
=
0;
//
遍歷checkInDays角標(biāo),每次onDarw時(shí)要將腳表歸0,不然有時(shí)候會出現(xiàn)閃爍問題
private
long
serverTime;
//服務(wù)器傳來時(shí)間
private
CheckInUtils
mCheckInUtils;
/**
*
單元格點(diǎn)擊的回調(diào)接口
*
*
@author
wuwenjie
*
*/
public
interface
OnCellClickListener
{
void
changeDate(CustomDateModel
date);
//
回調(diào)滑動ViewPager改變的日期
}
public
CalendarView(Context
context,
OnCellClickListener
listener,
long
serverTime)
{
super(context);
this.mCellClickListener
=
listener;
this.serverTime
=
serverTime;
this.mCheckInUtils
=
CheckInUtils.getInstance();
init(context);
}
private
void
init(Context
context)
{
if
(mCheckInUtils
!=
null){
mCheckInUtils.setServerTime(serverTime);
}
mTextPaint
=
new
Paint(Paint.ANTI_ALIAS_FLAG);
mRowTextPaint
=
new
Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint
=
new
Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setColor(getResources().getColor(R.color.log_today_white_circle));
//
白色圓圈
touchSlop
=
ViewConfiguration.get(context).getScaledTouchSlop();
this.setBackgroundResource(R.color.log_today_calendar_bg);//控件背景
initDate();
}
private
void
initDate()
{
mShowDate
=
new
CustomDateModel(serverTime);//系統(tǒng)時(shí)間可能被更改,所以從服務(wù)器獲取時(shí)間,(防刷)
fillDate();//
}
/**
*
填充數(shù)據(jù)
*/
private
void
fillDate()
{
int
monthDay
=
mCheckInUtils.getCurrentMonthDay();
//
今天
int
lastMonthDays
=
mCheckInUtils.getMonthDays(mShowDate.year,
mShowDate.month
-
1);
//
上個(gè)月的天數(shù)
currentMonthDays
=
mCheckInUtils.getMonthDays(mShowDate.year,
mShowDate.month);
//
當(dāng)前月的天數(shù)
int
firstDayWeek
=
mCheckInUtils.getWeekDayFromDate(mShowDate.year,
mShowDate.month);
boolean
isCurrentMonth
=
false;
if
(mCheckInUtils.isCurrentMonth(mShowDate))
{
isCurrentMonth
=
true;
}
int
day
=
0;
/*
*判斷當(dāng)月位置,并對其設(shè)置對應(yīng)的狀態(tài)、數(shù)字。
*/
for
(int
j
=
0;
j
<
TOTAL_ROW;
j++)
{//6行
rows[j]
=
new
Row(j);
for
(int
i
=
0;
i
<
TOTAL_COL;
i++)
{//7列
int
position
=
i
+
j
*
TOTAL_COL;
//
單元格位置
//
這個(gè)月的
if
(position
>=
firstDayWeek
&&
position
<
firstDayWeek
+
currentMonthDays)
{
day++;
rows[j].cells[i]
=
new
Cell(CustomDateModel.modifiDayForObject(
mShowDate,
day),
State.CURRENT_MONTH_DAY,
i,
j);
//還沒到時(shí)間的字體顏色都比較淡
if
(mShowDate.getYear()
>
mCheckInUtils.getYear()
||
(mShowDate.getYear()
==
mCheckInUtils.getYear()
&&
mShowDate.getMonth()
>
mCheckInUtils.getMonth())){
rows[j].cells[i]
=
new
Cell(
CustomDateModel.modifiDayForObject(mShowDate,
day),
State.UNREACH_DAY,
i,
j);
}
//
今天
if
(isCurrentMonth
&&
day
==
monthDay
)
{
CustomDateModel
date
=
CustomDateModel.modifiDayForObject(mShowDate,
day);
rows[j].cells[i]
=
new
Cell(date,
State.TODAY,
i,
j);
}
if
(isCurrentMonth
&&
day
>
monthDay)
{
//
如果比這個(gè)月的今天要大,表示還沒到
rows[j].cells[i]
=
new
Cell(
CustomDateModel.modifiDayForObject(mShowDate,
day),
State.UNREACH_DAY,
i,
j);
}
//
過去一個(gè)月
}
else
if
(position
<
firstDayWeek)
{
rows[j].cells[i]
=
new
Cell(new
CustomDateModel(mShowDate.year,
mShowDate.month
-
1,
lastMonthDays
-
(firstDayWeek
-
position
-
1)),
State.PAST_MONTH_DAY,
i,
j);
//
下個(gè)月
}
else
if
(position
>=
firstDayWeek
+
currentMonthDays)
{
rows[j].cells[i]
=
new
Cell((new
CustomDateModel(mShowDate.year,
mShowDate.month
+
1,
position
-
firstDayWeek
-
currentMonthDays
+
1)),
State.NEXT_MONTH_DAY,
i,
j);
}
}
}
}
@Override
protected
void
onSizeChanged(int
w,
int
h,
int
oldw,
int
oldh)
{
super.onSizeChanged(w,
h,
oldw,
oldh);
mViewWidth
=
w;
mViewHeight
=
h;
mCellSpace
=
mViewWidth
/
TOTAL_COL;
//每個(gè)框格寬度
mCellRowSpace
=
mViewHeight/TOTAL_ROW;//每個(gè)框格高度
circleRadius
=
mCellSpace
/
3;
mTextPaint.setTextSize(mCellSpace
/
4);
mRowTextPaint.setTextSize(mCellRowSpace
/
4);
}
@Override
protected
void
onDraw(Canvas
canvas)
{
super.onDraw(canvas);
checkIndex
=
0;
for
(int
i
=
0;
i
<
TOTAL_ROW;
i++)
{
if
(rows[i]
!=
null)
{
rows[i].drawCells(canvas);
}
}
}
/**
*目前還沒用到為以后補(bǔ)簽做準(zhǔn)備
*
*/
@Override
public
boolean
onTouchEvent(MotionEvent
event)
{
switch
(event.getAction())
{
case
MotionEvent.ACTION_DOWN:
mDownX
=
event.getX();
mDownY
=
event.getY();
break;
case
MotionEvent.ACTION_UP:
float
disX
=
event.getX()
-
mDownX;
float
disY
=
event.getY()
-
mDownY;
if
(Math.abs(disX)
<
touchSlop
&&
Math.abs(disY)
<
touchSlop)
{
int
col
=
(int)
(mDownX
/
mCellSpace);
int
row
=
(int)
(mDownY
/
mCellRowSpace);
measureClickCell(col,
row);
}
break;
default:
break;
}
return
true;
}
/**
*
計(jì)算點(diǎn)擊的單元格
*
@param
col
*
@param
row
*/
private
void
measureClickCell(int
col,
int
row)
{
if
(col
>=
TOTAL_COL
||
row
>=
TOTAL_ROW)
return;
if
(mClickCell
!=
null)
{
rows[mClickCell.j].cells[mClickCell.i]
=
mClickCell;
}
if
(rows[row]
!=
null)
{
mClickCell
=
new
Cell(rows[row].cells[col].date,
rows[row].cells[col].state,
rows[row].cells[col].i,
rows[row].cells[col].j);
CustomDateModel
date
=
rows[row].cells[col].date;
date.week
=
col;
//
刷新界面
update();
}
}
/**
*
組元素
*
*/
class
Row
{
public
int
j;
Row(int
j)
{
this.j
=
j;
}
public
Cell[]
cells
=
new
Cell[TOTAL_COL];
//
繪制單元格
public
void
drawCells(Canvas
canvas)
{
for
(int
i
=
0;
i
<
cells.length;
i++)
{
if
(cells[i]
!=
null)
{
cells[i].drawSelf(canvas);
}
}
}
}
/**
*
單元格元素
*
*
@author
wuwenjie
*
*/
class
Cell
{
public
CustomDateModel
date;
public
State
state;
public
int
i;
public
int
j;
public
Cell(CustomDateModel
date,
State
state,
int
i,
int
j)
{
super();
this.date
=
date;
this.state
=
state;
this.i
=
i;
this.j
=
j;
}
public
void
drawSelf(Canvas
canvas)
{
String
content
=
String.valueOf(date.day);
switch
(state)
{
case
TODAY:
//
今天
mTextPaint.setColor(getResources().getColor(R.color.log_today_current_text));
currentDayX
=
(float)
(mCellSpace
*
(i
+
0.55));
currentDayY
=
(float)
((j
+
0.48)
*
mCellRowSpace);
canvas.drawCircle(currentDayX,
currentDayY,
circleRadius
-10,
mCirclePaint);
break;
case
CURRENT_MONTH_DAY:
//
當(dāng)前月日期
mTextPaint.setColor(getResources().getColor(R.color.log_today_current_text));
if
(checkInDays
!=
null)
{
if
(checkIndex
<
checkInDays.size())
{
if
((checkInDays.get(checkIndex)
%
100
==
date.day))
{
canvas.drawCircle((float)
(mCellSpace
*
(i
+
0.55)),
(float)
(j
+
0.9)
*
mCellRowSpace,
4,
mCirclePaint);
checkIndex++;
}
}
}
break;
case
PAST_MONTH_DAY:
//
過去一個(gè)月
case
NEXT_MONTH_DAY:
//
下一個(gè)月
mTextPaint.setColor(getResources().getColor(R.color.log_today_calendar_bg));
break;
case
UNREACH_DAY:
//
還未到的天
mTextPaint.setColor(Color.GRAY);
break;
default:
break;
}
//
繪制文字
canvas.drawText(content,
(float)
((i+0.55)
*
mCellSpace
-
mTextPaint
.measureText(content)
/
2),
(float)
((j
+
0.7)
*
mCellRowSpace
-
mRowTextPaint
.measureText(content,
0,
1)
/
2),
mTextPaint);
}
}
/**
*
*
@author
wuwenjie
單元格的狀態(tài)
當(dāng)前月日期,過去的月的日期,下個(gè)月的日期
*/
enum
State
{
TODAY,CURRENT_MONTH_DAY,
PAST_MONTH_DAY,
NEXT_MONTH_DAY,
UNREACH_DAY;
}
//
從左往右劃,上一個(gè)月
public
void
leftSlide()
{
checkInDays
=
null;
if
(mShowDate.month
==
1)
{
mShowDate.month
=
12;
mShowDate.year
-=
1;
}
else
{
mShowDate.month
-=
1;
}
if(mCellClickListener
!=
null){
mCellClickListener.changeDate(mShowDate);
}
update();
}
//
從右往左劃,下一個(gè)月
public
void
rightSlide()
{
checkInDays
=
null;
if
(mShowDate.month
==
12)
{
mShowDate.month
=
1;
mShowDate.year
+=
1;
}
else
{
mShowDate.month
+=
1;
}
if(mCellClickListener
!=
null){
mCellClickListener.changeDate(mShowDate);
}
update();
}
public
void
update(){
checkIndex
=
0;
fillDate();
invalidate();
}
public
void
currentUpdate(){
initDate();
if(mCellClickListener
!=
null){
mCellClickListener.changeDate(mShowDate);
}
invalidate();
}
/**
*
當(dāng)月已簽days(正確時(shí)間)
*/
public
void
setCurrentCheckInDays(List<Long>
currentLogs){
this.checkInDays
=
currentLogs;
initDate();
if(mCellClickListener
!=
null){
mCellClickListener.changeDate(mShowDate);
}
invalidate();
}
/**
*
本月已簽
*
@param
logs
*/
public
void
setCheckInDays(List<Long>
logs){
this.checkInDays
=
logs;
update();
}
/**
*
簽到刷新方法,并返回當(dāng)前日期坐標(biāo)(放大)
*
@return
*/
public
void
SignIn(){
new
CountDownTimerUtils(350,
25)
{
@Override
public
void
onTick(long
millisUntilFinished)
{
fillDate();
circleRadius
=
circleRadius
+1;
invalidate();
}
@Override
public
void
onFinish()
{
cancel();
SignInt();
}
}.start();
}
/**
*
(縮小)
*/
private
void
SignInt(){
new
CountDownTimerUtils(350
,
25)
{
@Override
public
void
onTick(long
millisUntilFinished)
{
fillDate();
circleRadius
=
circleRadius
-1;
invalidate();
}
@Override
public
void
onFinish()
{
cancel();
}
}.start();
}
}2.CheckInUtils
工具類,主要是從Calendar中獲取日期,注意不要因?yàn)閙Calender=Calender.getInstance就以為Calender是單例,我這這地方吃過虧。package
oct.mama.utils;
import
android.annotation.SuppressLint;
import
android.content.Context;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
import
java.util.Date;
import
oct.mama.model.CustomDateModel;
/**
*
簽到日期工具,
調(diào)用除設(shè)置或獲取參數(shù)外得先調(diào)用setServerTime函數(shù);
*
Created
by
Administrator
on
2015/10/29.
*/
public
class
CheckInUtils
{
private
static
CheckInUtils
checkInUtils
=
new
CheckInUtils();
public
static
final
String
CHECK_IN_KEY
=
"check_in_time";//簽到key
public
Calendar
mCalender;
private
CheckInUtils(){
}
public
static
CheckInUtils
getInstance(){
return
checkInUtils;
}
public
void
setServerTime(long
time){
mCalender
=
Calendar.getInstance();
mCalender.setTimeInMillis(time*1000);
}
public
int
getMonthDays(int
year,
int
month)
{
if
(month
>
12)
{
month
=
1;
year
+=
1;
}
else
if
(month
<
1)
{
month
=
12;
year
-=
1;
}
//月份對應(yīng)的天數(shù)
int[]
arr
=
{
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
};
int
days
=
0;
if
((year
%
4
==
0
&&
year
%
100
!=
0)
||
year
%
400
==
0)
{
arr[1]
=
29;
//
閏年2月29天
}
try
{
days
=
arr[month
-
1];
}
catch
(Exception
e)
{
e.getStackTrace();
}
return
days;
}
public
int
getYear()
{
return
mCalender.get(Calendar.YEAR);
}
public
String
getCurrentTime(){
return
mCalender.get(Calendar.YEAR)+"-"+
checkInUtils.getMonth();
}
public
int
getMonth()
{
return
mCalender.get(Calendar.MONTH)
+
1;
}
public
int
getCurrentMonthDay()
{
return
mCalender.get(Calendar.DAY_OF_MONTH);
}
/**
*
當(dāng)月1號是第一個(gè)星期的第幾天
*
@param
year
*
@param
month
*
@return
*/
public
int
getWeekDayFromDate(int
year,
int
month)
{
Calendar
cal
=
Calendar.getInstance();
cal.setTime(getDateFromString(year,
month));
int
week_index
=
cal.get(Calendar.DAY_OF_WEEK)
-
1;
if
(week_index
<
0)
{
week_index
=
0;
}
return
week_index;
}
@SuppressLint("SimpleDateFormat")
public
Date
getDateFromString(int
year,
int
month)
{
String
dateString
=
year
+
"-"
+
(month
>
9
?
month
:
("0"
+
month))
+
"-01";
Date
date
=
null;
try
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat("yyyy-MM-dd");
date
=
sdf.parse(dateString);
}
catch
(ParseException
e)
{
System.out.println(e.getMessage());
}
return
date;
}
public
boolean
isCurrentMonth(CustomDateModel
date){
return(date.year
==
checkInUtils.getYear()
&&
date.month
==
checkInUtils.getMonth());
}
/**
*
本地保存簽到記錄
*/
public
static
void
savePreference(Context
context){
SharedPreferencesUtils.setPreferenceLong(context,
CHECK_IN_KEY,
System.currentTimeMillis()
/
1000
/
60
/
60
/
24);
}
public
static
long
getPreference(Context
context){
return
SharedPreferencesUtils.getPreferenceLong(context,
CHECK_IN_KEY,
0);
}
}3.CustomDateModelpackage
oct.mama.model;
import
oct.mama.utils.CheckInUtils;
/**
*
簽到日期實(shí)體類
*
Created
by
Administrator
on
2015/10/29.
*/
public
class
CustomDateModel
{
public
int
year;
public
int
month;
public
int
day;
public
int
week;
public
CustomDateModel(int
year,
int
month,
int
day){
if(month
>
12){
month
=
1;
year++;
}else
if(month
<1){
month
=
12;
year--;
}
this.year
=
year;
this.month
=
month;
this.day
=
day;
}
public
CustomDateModel(long
serverTime){
CheckInUtils
mCheckInUtils
=
CheckInUtils.getInstance();
mCheckInUtils.setServerTime(serverTime);
this.year
=
mCheckInUtils.getYear();
this.month
=
mCheckInUtils.getMonth();
this.day
=
mCheckInUtils.getCurrentMonthDay();
}
public
static
CustomDateModel
modifiDayForObject(CustomDateModel
date,int
day){
CustomDateModel
modifiDate
=
new
CustomDateModel(date.year,date.month,day);
return
modifiDate;
}
@Override
public
String
toString()
{
return
year+"-"+month+"-"+day;
}
public
String
getTime(){
return
year+"-"+month;
}
public
int
getYear()
{
return
year;
}
public
void
setYear(int
year)
{
this.year
=
year;
}
public
int
getMonth()
{
return
month;
}
public
void
setMonth(int
month)
{
this.month
=
month;
}
public
int
getDay()
{
return
day;
}
public
void
setDay(int
day)
{
this.day
=
day;
}
public
int
getWeek()
{
return
week;
}
public
void
setWeek(int
week)
{
this.week
=
week;
}
}4.Activity:注意在我請求數(shù)據(jù)的那些地方大家要填充自己的值。刪除了許多跟公司相關(guān)的東西。package
oct.mama.activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.support.v4.view.ViewPager;
import
android.text.SpannableStringBuilder;
import
android.text.Spanned;
import
android.text.style.ForegroundColorSpan;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.animation.AlphaAnimation;
import
android.view.animation.Animation;
import
android.view.animation.AnimationSet;
import
android.view.animation.TranslateAnimation;
import
android.widget.ImageView;
import
android.widget.TextView;
import
com.loopj.android.http.RequestParams;
import
java.text.MessageFormat;
import
java.util.List;
import
oct.mama.R;
import
oct.mama.adapter.CalendarViewAdapter;
import
oct.mama.alert.CheckInRuleDialog;
import
oct.mama.alert.ConfirmDialog;
import
oct.mama.api.ApiInvoker;
import
oct.mama.apiInterface.IMemberApi;
import
oct.mama.apiResult.GenericResult;
import
oct.mama.connect.GenericResultResponseHandler;
import
oct.mama.model.CheckInResult;
import
oct.mama.model.CouponInfoModel;
import
oct.mama.model.CustomDateModel;
import
oct.mama.model.MonthSignLogModel;
import
oct.mama.model.ShowCheckInResult;
import
oct.mama.utils.CheckInUtils;
import
oct.mama.view.CalendarView;
/**
*
簽到頁面
*/
public
class
CheckInActivity
extends
BaseTitleActivity
implements
CalendarView.OnCellClickListener,
View.OnClickListener
{
private
ViewPager
mViewPager;
public
List<Long>
currentLogs;
private
TextView
couponRmb;
private
TextView
couponArea;
private
ImageView
logTodayBgImageView;
private
TextView
monthText;
private
ImageView
signInImageView;
private
TextView
animText;
private
int
mCurrentIndex
=
498;
private
CalendarView[]
mShowViews;
private
CalendarViewAdapter<CalendarView>
adapter;
private
SildeDirection
mDirection
=
SildeDirection.NO_SILDE;
private
CheckInRuleDialog
checkInRuleDialog;
private
IMemberApi
mIMemberApi;
private
int
continuousDays;
private
CheckInUtils
mCheckInUtils;
private
String
rules;
private
String
currentAmount;
private
String
currentArea;
enum
SildeDirection
{
RIGHT,
LEFT,
NO_SILDE;
}
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_today);
setTitleText(R.string.log_have_gift);
init();
mCheckInUtils
=CheckInUtils.getInstance();
CheckInUtils.savePreference(this);//本地保存簽到記錄
getCheckInData();
}
private
void
init(){
ImageView
preImgBtn
=
(ImageView)
findViewById(R.id.last_month);
ImageView
nextImgBtn
=
(ImageView)
findViewById(R.id.next_month);
monthText
=
(TextView)
findViewById(R.id.log_today_time);
signInImageView
=
(ImageView)
findViewById(R.id.log_today_check);
mViewPager
=
(ViewPager)
this.findViewById(R.id.vp_calendar);
animText
=
(TextView)
findViewById(R.id.check_in_anim);
preImgBtn.setOnClickListener(this);
nextImgBtn.setOnClickListener(this);
signInImageView.setOnClickListener(this);
}
/**
*
獲取是否已簽到數(shù)據(jù),以及當(dāng)前時(shí)間,
已簽過日期
*/
public
void
getCheckInData()
{
//
這里是請求數(shù)據(jù)庫的值,沒個(gè)公司,每個(gè)人要的數(shù)據(jù)是不一樣的,到時(shí)候大家修改,大家編譯的時(shí)候要對Activity好好修改,我是懶得改了
//
請求成功
@Override
public
void
onSuccess(ShowCheckInResult
genericResult)
{
super.onSuccess(genericResult);
if
(genericResult.getCode()
==
GenericResult.SUCCESS)
{
if
(genericResult.getRulesText()
!=
null)
{
rules
=
genericResult.getRulesText();
}
invalidateOptionsMenu();
signInImageView.setVisibility(View.VISIBLE);
mCheckInUtils.setServerTime(genericResult.getServerTime());
viewpager(genericResult.getServerTime());
//當(dāng)前月已簽過days
currentLogs
=
genericResult.getLogs();
//今天已簽過到
if
(genericResult.isHasCheckIn())
{
signInImageView.setEnabled(false);
signInImageView.setImageResource(R.drawable.icon_checked_logtoday);
logTodayBgImageView.setImageResource(R.drawable.bg_logtoday_flower_open);
}
else
{
signInImageView.setEnabled(true);
}
}
else
{
super.onFailure(genericResult);
}
}
};
}
/**
*
設(shè)置ViewPager數(shù)
*
@param
serverTime
*/
private
void
viewpager(long
serverTime){
CalendarView[]
views
=
new
CalendarView[3];
for
(int
i
=
0;
i
<
3;
i++)
{
views[i]
=
new
CalendarView(this,
this,
serverTime);
}
adapter
=
new
CalendarViewAdapter<>(views);
mShowViews
=
adapter.getAllItems();
setViewPager();
//(解決請求玩數(shù)據(jù)第一頁不顯示問題:沒有對當(dāng)前CalenderView執(zhí)行onDraw方法,是因?yàn)镃alenderView沒有在UI線程)
mViewPager.post(new
Runnable()
{
@Override
public
void
run()
{
mShowViews[0].setCurrentCheckInDays(currentLogs);
}
});
}
/**
*
設(shè)置ViewPager其他特性
*/
private
void
setViewPager()
{
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(498);
mViewPager.setOnPageChangeListener(new
ViewPager.OnPageChangeListener()
{
@Override
public
void
onPageSelected(int
position)
{
measureDirection(position);
updateCalendarView(position);
}
@Override
public
void
onPageScrolled(int
arg0,
float
arg1,
int
arg2)
{
}
@Override
public
void
onPageScrollStateChanged(int
arg0)
{
}
});
}
/**
*
計(jì)算方向
*
*
@param
arg0
*/
private
void
measureDirection(int
arg0)
{
if
(arg0
>
mCurrentIndex)
{
mDirection
=
SildeDirection.RIGHT;
}
else
if
(arg0
<
mCurrentIndex)
{
mDirection
=
SildeDirection.LEFT;
}
mCurrentIndex
=
arg0;
}
//
更新日歷視圖
private
void
updateCalendarView(int
arg0)
{
if
(mDirection
==
SildeDirection.RIGHT)
{
mShowViews[arg0
%
mShowViews.length].rightSlide();
}
else
if
(mDirection
==
SildeDirection.LEFT)
{
mShowViews[arg0
%
mShowViews.length].leftSlide();
}
mDirection
=
SildeDirection.NO_SILDE;
}
/**
*
月份切換
*
@param
date
*/
@Override
public
void
changeDate(CustomDateModel
date)
{
monthText.setText(date.getYear()
+
"-"
+
date.getMonth());
getMonthLogs(date);
}
/**
*
當(dāng)前頁面簽到日期查詢
*
@param
date
*
@return
*/
private
void
getMonthLogs(CustomDateModel
date){
//請求成功
public
void
onSuccess(MonthSignLogModel
genericResult)
{
super.onSuccess(genericResult);
if
(genericResult.getCode()
==
GenericResult.SUCCESS)
{
if
(genericResult.getLogs()
!=
null)
{
mShowViews[mViewPager.getCurrentItem()
%
mShowViews.length].setCheckInDays(genericResult.getLogs());
}
}
else
{
super.onFailure(genericResult);
}
}
});
}
@Override
public
void
onClick(View
v)
{
switch
(v.getId()){
case
R.id.last_month:
mViewPager.setCurrentItem(mViewPager.getCurrentItem()
-
1);
break;
case
R.id.next_month:
mViewPager.setCurrentItem(mViewPager.getCurrentItem()
+
1);
break;
case
R.id.log_today_check:
if
(mViewPager.getCurrentItem()
!=
498)
{
if
(currentLogs
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 六年級下冊數(shù)學(xué)教學(xué)設(shè)計(jì)-總復(fù)習(xí) 雞兔同籠|北師大版
- 三年級下冊數(shù)學(xué)教案-6.1 面積的初步認(rèn)識 丨蘇教版
- 六年級下冊數(shù)學(xué)教案-1.2 百分?jǐn)?shù)和分?jǐn)?shù)、小數(shù)的互化 ︳西師大版
- 2025年學(xué)習(xí)雷鋒精神62周年主題活動方案 合計(jì)3份
- 2024年槽鋼項(xiàng)目資金需求報(bào)告代可行性研究報(bào)告
- 2025年河北司法警官職業(yè)學(xué)院單招職業(yè)技能測試題庫完美版
- 專題21 信息的傳遞-2025年中考《物理》一輪復(fù)習(xí)知識清單與解題方法
- 2025年廣西自然資源職業(yè)技術(shù)學(xué)院單招職業(yè)傾向性測試題庫參考答案
- 2025年度代養(yǎng)大型豬群養(yǎng)殖基地合作協(xié)議
- 2025年度專業(yè)瓷磚鋪貼班組勞務(wù)合同
- 上消化道異物的內(nèi)鏡處理
- 健康教育學(xué)全套課件完整版
- 2024年遼寧省中考語文真題含解析
- 農(nóng)產(chǎn)品食品檢驗(yàn)員二級技師技能理論考試題含答案
- 《財(cái)政學(xué)》財(cái)政及經(jīng)管類專業(yè)全套教學(xué)課件
- 質(zhì)量管理體系相關(guān)標(biāo)準(zhǔn)知識講解(一)-質(zhì)量管理-組織的質(zhì)量-實(shí)現(xiàn)持續(xù)成功指南-答案
- 《事業(yè)單位工作人員年度考核登記表》
- 物業(yè)管理退場通知書(模板)
- 江蘇省蘇州市2025屆高考仿真模擬歷史試卷含解析
- DL∕T 5210.6-2019 電力建設(shè)施工質(zhì)量驗(yàn)收規(guī)程 第6部分:調(diào)整試驗(yàn)
- DL∕T 712-2010 發(fā)電廠凝汽器及輔機(jī)冷卻器管選材導(dǎo)則
評論
0/150
提交評論