1.先看下运行效果吧
运行效果图
2.再看下代码结构图吧
代码结构图
3.下面再看下具体的实现步骤
<1>.自定义日历控件HuaCalendarView类
public class HuaCalendarView extends LinearLayout implements OnClickListener,OnItemClickListener{
private Context mContext;
private LayoutInflater mLayoutInflater;
private GridView mGv_Calendar;
private TextView mTv_Current_Time;
private CalendarViewAdapter mCalendarViewAdapter;
private onClickSignInCallBack mClickSignInCallBack;
private int mCurrentTime ;
public HuaCalendarView(Context context,onClickSignInCallBack callBack){
super(context);
this.mContext = context;
this.mClickSignInCallBack = callBack;
initView();
initData();
}
public HuaCalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initView();
initData();
}
public HuaCalendarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
initView();
initData();
}
public void initView(){
mLayoutInflater = LayoutInflater.from(mContext);
mLayoutInflater.inflate(R.layout.view_calendar,this);
mTv_Current_Time = (TextView)findViewById(R.id.tv_current_time);
mGv_Calendar = (GridView)findViewById(R.id.gv_calendar);
}
public void initData(){
mTv_Current_Time.setText(Utils.getTime(System.currentTimeMillis()));
mCalendarViewAdapter = new CalendarViewAdapter(mContext, Utils.getCurrentMonthDay(),mTv_Current_Time);
mCalendarViewAdapter.setonClickSignInCallBack(mClickSignInCallBack);
mGv_Calendar.setAdapter(mCalendarViewAdapter);
mGv_Calendar.setOnItemClickListener(this);
getCurrentTimePosition();
}
@Override
public void onClick(View v) {
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
mCalendarViewAdapter.onRefresh(position, true);
android.util.Log.e("POSITION", getCurrentTimePosition() + "");
}
public void setOnClickSignInCallBack(onClickSignInCallBack callBack){
this.mClickSignInCallBack = callBack;
mCalendarViewAdapter.setonClickSignInCallBack(mClickSignInCallBack);
}
/**
* 获取当前时间
* @return
*/
public int getCurrentTime(){
return Utils.getDayOfMonth();
}
/**
* 获取当前日前的位置
* @return
*/
public int getCurrentTimePosition(){
int position ;
if (Utils.getCurrentMonthStart() == 7) {
position = 0;
}else
position= Utils.getCurrentMonthStart();
return getCurrentTime() + 7 + position ;
}
}
<2>.自定义日历控件的适配器CalendarViewAdapter
public class CalendarViewAdapter extends BaseAdapter {
private Context mContext;
private int mCountDay = 49;
private int mCurrent_mouth_Countday;
private int mCurrent_Week;
private int id[] = { R.string.week7, R.string.week1, R.string.week2,
R.string.week3, R.string.week4, R.string.week5, R.string.week6 };
private ViewHolder mViewHolder;
private List<Boolean> booleans;
private HuahuaLoadPop mLoadPop;
private View mView;
private onClickSignInCallBack mCallBack;
public CalendarViewAdapter(Context context, int countday,View view) {
this.mContext = context;
this.mCurrent_Week = Utils.getCurrentMonthStart();
this.mCurrent_mouth_Countday = countday;
booleans = new ArrayList<Boolean>();
mView = view;
mLoadPop = new HuahuaLoadPop(mContext, mView);
for (int i = 0; i < mCountDay; i++) {
booleans.add(i, false);
}
}
public void setonClickSignInCallBack(onClickSignInCallBack callBack){
this.mCallBack = callBack;
}
@Override
public int getCount() {
return mCountDay;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
mViewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(
R.layout.item_calendar, null);
mViewHolder.mTv_calendar_day = (TextView) convertView
.findViewById(R.id.tv_calendar_day);
mViewHolder.mFl_calendar = (FrameLayout)convertView.findViewById(R.id.fl_calendar);
convertView.setTag(mViewHolder);
} else
mViewHolder = (ViewHolder) convertView.getTag();
if (position <= 6) {
mViewHolder.mTv_calendar_day.setTextColor(Color.BLACK);
mViewHolder.mTv_calendar_day.setTextSize(mContext.getResources()
.getDimension(R.dimen.text_size_7));
mViewHolder.mTv_calendar_day.setText(mContext.getResources().getString(
id[position]));
} else {
if (mCurrent_Week == 7 && (position -6) <= mCurrent_mouth_Countday) {
mViewHolder.mTv_calendar_day.setText(position-6 + "");
} else if (position -7>= mCurrent_Week
&& position - mCurrent_Week -6 <= mCurrent_mouth_Countday) {
mViewHolder.mTv_calendar_day.setText(position - mCurrent_Week -6
+ "");
}
}
if (position % 7 == 6) {
mViewHolder.mFl_calendar.setBackgroundResource(R.drawable.line_right);
}else if (position % 7 == 0) {
mViewHolder.mFl_calendar.setBackgroundResource(R.drawable.line_left);
}
if (booleans.get(position)) {
mViewHolder.mTv_calendar_day.setBackground(mContext.getResources().getDrawable(R.drawable.icon_sign_in));
}else {
mViewHolder.mTv_calendar_day.setBackground(null);
}
return convertView;
}
public void onRefresh(int position ,Boolean isClick){
if (position <= 6) {
} else {
if (mCurrent_Week == 7 && (position -6) <= mCurrent_mouth_Countday) {
if (!booleans.get(position)) {
booleans.set(position, isClick);
mCallBack.onSucess();
}
} else if (position -7>= mCurrent_Week
&& position - mCurrent_Week -6 <= mCurrent_mouth_Countday) {
if (!booleans.get(position)) {
booleans.set(position, isClick);
mCallBack.onSucess();
}
}
}
notifyDataSetChanged();
}
public class ViewHolder {
TextView mTv_calendar_day;
FrameLayout mFl_calendar;
}
public interface onClickSignInCallBack {
public void onSucess();
public void onFail(String error);
public void onTimeOut(String msg);
}
}
<3>.适配器中需要的HuahuaLoadPop类,此类是自定义的PopupWindow
public class HuahuaLoadPop{
private PopupWindow mPopupWindow;
private LayoutInflater mInflater;
private View mLlMain;
private Context mContext;
public HuahuaLoadPop(Context context,View view){
this.mContext = context;
this.mLlMain = view;
initPop();
}
public void initPop() {
mInflater = LayoutInflater.from(mContext);
View view = mInflater.inflate(R.layout.pop_loading, null);
mPopupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
}
public void showPop() {
mPopupWindow.showAtLocation(mLlMain, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
public void dismissPop() {
mPopupWindow.dismiss();
}
public void setAnimationStyle(int style){
mPopupWindow.setAnimationStyle(style);
}
public void setOutSideTouchable(boolean b){
mPopupWindow.setFocusable(b);
mPopupWindow.setOutsideTouchable(b);
}
}
<4>.在实现日历类控件的工具类
public class Utils {
/**
* 获取当月的 天数
* */
public static int getCurrentMonthDay() {
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
public static int getCurrentWeek(){
Date date = new Date(System.currentTimeMillis());
int week = date.getDay();
return week;
}
public static String getTime(long time){
DateFormat format = new DateFormat();
String str= (String) format.format("yyyy年MM月", time);
return str;
}
public static int getCurrentMonthStart(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat format = new SimpleDateFormat("E");
System.out.println("本月第一天是:" + format.format(calendar.getTime()) + "===="+calendar.get(Calendar.DAY_OF_WEEK));
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == 1)
day = 7;
else
day = day - 1;
return day;
}
public static int getDayOfMonth(){
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
}
<5>.下面需要给出具体的资源文件,具体是日历控件的布局文件
view_calendar.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dddddd"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:background="@android:color/white"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日历"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textColor="#929292"
android:textSize="@dimen/text_size_5" />
<TextView
android:id="@+id/tv_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center"
android:textColor="#929292"
android:textSize="@dimen/text_size_5"
/>
</RelativeLayout>
<GridView
android:id="@+id/gv_calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/white"
android:listSelector="@null"
android:numColumns="7"
></GridView>
</LinearLayout>
item_calendar.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fl_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/line_all"
>
<TextView
android:id="@+id/tv_calendar_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="@dimen/text_size_6"
android:textColor="@color/t_929292"
/>
</FrameLayout>
</LinearLayout>