手写创建日历

CalendarAdapter代码
package com.example.androidmangshan.tool;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.androidmangshan.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarAdapter extends BaseAdapter {
	/**是否为闰年*/
	private boolean isLeapyear = false;
	/**本月有多少天*/
	private int daysOfMonth = 0;
	/**本月第一天为周几*/
	private int dayOfWeek = 0;
	/**上个月有多少天*/
	private int lastDaysOfMonth = 0;
	/**……*/
	private Context context;
	/**此页面日历显示的数字*/
	private String[] dayNumber = new String[35];
	/**……*/
	private SpecialCalendar sc = null;
	private Resources res = null;
	private Drawable drawable = null;
	private String currentYear = "" ;
	private String currentMonth = "";
	private String currentDay = "";
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
	private int currentFlag = -1;
	private int[] schDateTagFlag = null;
	private String showYear = "";
	private String showMonth = "";
	private String animalsYear = "";
	private String sysDate = "";
	private String leapMonth = "";
	private String sys_year = "";
	private String sys_month = "";
	private String sys_day = "";
	private int jumpMonth;
	
	public CalendarAdapter(){
		Date date = new Date();
		sysDate = sdf.format(date);
		sys_year = sysDate.split("-")[0];
		sys_month = sysDate.split("-")[1];
		sys_day = sysDate.split("-")[2];
	}
	
	public CalendarAdapter(Context context, Resources res, int jumpMonth, 
			int year_c, int month_c, int day_c){
		this();
		this.jumpMonth = jumpMonth;
		this.context = context;
		sc = new SpecialCalendar();
		this.res = res;
		
		int stepYear = year_c;
		int stepMonth = month_c + jumpMonth;
		if(stepMonth > 0){
			if(stepMonth % 12 == 0){
				stepYear = year_c + stepMonth/12 - 1;
				stepMonth = 12;
			}else{
				stepYear = year_c + stepMonth/12;
				stepMonth = stepMonth%12;
			}
		}else{
			stepYear = year_c - 1 + stepMonth / 12;
			stepMonth = stepMonth % 12 + 12;
			if (stepMonth % 12 == 0) {

			}
		}
		
		currentYear = String.valueOf(stepYear);
		currentMonth = String.valueOf(stepMonth);
		currentDay = String.valueOf(day_c);
		getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth));
	}
	
	private void getCalendar(int year, int month) {
		isLeapyear = sc.isLeapYear(year);
		daysOfMonth = sc.getDaysOfMonth(isLeapyear, month);
		dayOfWeek = sc.getWeekdayOfMonth(year, month);
		lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1);
		getweek(year, month);
	}

	private void getweek(int year, int month) {
		int j = 1;
		int flag = 0;
		
		for(int i = 0; i < dayNumber.length; i++){
			if(i < dayOfWeek){
				int temp = lastDaysOfMonth - dayOfWeek + 1;
				dayNumber[i] = String.valueOf(lastDaysOfMonth - dayOfWeek + 1 + i);
			}else if(i < daysOfMonth + dayOfWeek){
				String day = String .valueOf(i - dayOfWeek + 1);
				dayNumber[i] = String .valueOf(i - dayOfWeek + 1);
				if(sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)){
					currentFlag = i;
				}
				setShowYear(String.valueOf(year));
				setShowMonth(String.valueOf(month));
				
			}else { 
				
				dayNumber[i] = String.valueOf(j);
				j++;
			}
		}
		
		String abc = "";
		for (int i = 0; i < dayNumber.length; i++) {
			abc = abc + dayNumber[i] + ":";
		}
		
	}
	public void matchScheduleDate(int year, int month, int day) {

	}
	public int getSysYear(){
		return Integer.parseInt(sys_year);
	}
	public int getSysMonth(){
		return Integer.parseInt(sys_month);
	}
	public int getSysDay(){
		return Integer.parseInt(sys_day);
	}
	/**
	 * 点击每一个item时返回item中的日期
	 * 
	 * @param position
	 * @return
	 */
	public String getDateByClickItem(int position) {
		return dayNumber[position];
	}

	/**
	 * 在点击gridView时,得到这个月中第一天的位置
	 * 
	 * @return
	 */
	public int getStartPositon() {
		return dayOfWeek + 7;
	}

	/**
	 * 在点击gridView时,得到这个月中最后一天的位置
	 * 
	 * @return
	 */
	public int getEndPosition() {
		return (dayOfWeek + daysOfMonth + 7) - 1;
	}

	public String getShowYear() {
		return showYear;
	}

	public void setShowYear(String showYear) {
		this.showYear = showYear;
	}

	public String getShowMonth() {
		return showMonth;
	}

	public void setShowMonth(String showMonth) {
		this.showMonth = showMonth;
	}

	public String getLeapMonth() {
		return leapMonth;
	}

	public void setLeapMonth(String leapMonth) {
		this.leapMonth = leapMonth;
	}

	

	@Override
	public int getCount() {
		return dayNumber.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.calendar_item, parent, false);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tvtext);
		textView.setText(dayNumber[position]);
		textView.setTextColor(Color.GRAY);
		
		if(position < daysOfMonth + dayOfWeek && position >= dayOfWeek){
			textView.setTextColor(Color.BLACK);
			drawable = new ColorDrawable(res.getColor(R.color.app_theme));
			if(position % 7 == 0 || position % 7 == 6){
				textView.setTextColor(res.getColor(R.color.app_theme));
				drawable = new ColorDrawable(res.getColor(R.color.app_theme));
			}
		}
		if(currentFlag != -1){
			if(currentFlag > position){
				textView.setTextColor(Color.GRAY);
				String text = textView.getText().toString();
				textView.setText(text + "\n" + "不可选");
			}
			
		}
		if(currentFlag == position){
			String text = textView.getText().toString();
			textView.setText(text + "\n" + "今日");
		}
		
		return convertView;
	}

}

CalendarView代码

package com.example.androidmangshan.tool;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.example.androidmangshan.R;
import com.example.androidmangshan.inteface.HotelActivityCalendarCallBack;

import android.content.Context;
import android.content.res.Resources;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ViewFlipper;


public class CalendarView  {
	private View convertView;
	private Resources resources;
	private Context context;
	private ViewFlipper flipper = null;
	private GridView gridView;
	private String currentDate = "";
	private int year_c = 0;
	private int month_c = 0;
	private int gvFlag = 0;
	private int day_c = 0;
	private static int jumpMonth = 0;
	private TextView currentMonth;
	private GestureDetector gestureDetector = null;
	private CalendarAdapter calV = null;
	private WindowManager windowManager;
	private int[] clickDate = {-1, -1, -1};
	private HotelActivityCalendarCallBack callBack;
	public CalendarView(Context context, Resources resources, WindowManager windowManager) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resources = resources;
		this.windowManager = windowManager;
		convertView = LayoutInflater.from(context).inflate(R.layout.calendar_layout, null, false);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
		currentDate = sdf.format(date);
		year_c = Integer.parseInt(currentDate.split("-")[0]);
		month_c = Integer.parseInt(currentDate.split("-")[1]);
		day_c = Integer.parseInt(currentDate.split("-")[2]);
		currentMonth = (TextView) convertView.findViewById(R.id.current_month_tv);
		gestureDetector = new GestureDetector(context, new MyGestureListener());
		flipper = (ViewFlipper) convertView.findViewById(R.id.flipper);
		flipper.removeAllViews();
		calV = new CalendarAdapter(context, resources, jumpMonth, year_c, month_c, day_c);
		addGridView();
		gridView.setAdapter(calV);
		flipper.addView(gridView, 0);
		addTextToTopTextView(currentMonth);
	}
	
    public void setOnCalendarCallBack(HotelActivityCalendarCallBack callBack){
    	this.callBack = callBack;
    }

	private void addTextToTopTextView(TextView currentMonth) {
		// TODO Auto-generated method stub
		StringBuffer textDate = new StringBuffer();
		textDate.append(calV.getShowYear()).append("年").append(calV.getShowMonth()).append("月");
		currentMonth.setText(textDate);
	}

	private class MyGestureListener extends SimpleOnGestureListener{

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			int gvFlag = 0;
			if(e1.getX() - e2.getX() > 120) {
				//向左滑动
				enterNextMonth(gvFlag);
			} else if(e1.getX() - e2.getX() < -120){
				//向右滑动
				enterPrevMonth(gvFlag);
			}
			return super.onFling(e1, e2, velocityX, velocityY);
		}
    	
    }
	
	private void enterNextMonth(int gvFlag){
		addGridView();
		jumpMonth++;
		calV = new CalendarAdapter(context, resources, jumpMonth, year_c, month_c, day_c);
		gridView.setAdapter(calV);
		addTextToTopTextView(currentMonth);
		gvFlag++;
		flipper.addView(gridView, gvFlag);
		flipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_left_out));
		flipper.showNext();
		flipper.removeViewAt(0);
	}
	
	private void enterPrevMonth(int gvFlag){
		if(jumpMonth == 0){
			return;
		}
		addGridView(); 
		
		jumpMonth--; 

		calV = new CalendarAdapter(context, resources, jumpMonth, year_c, month_c, day_c);
		gridView.setAdapter(calV);
		gvFlag++;
		addTextToTopTextView(currentMonth);
		flipper.addView(gridView, gvFlag);

		flipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.push_right_out));
		flipper.showPrevious();
		flipper.removeViewAt(0);
	}
	private void addGridView(){
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		Display display = windowManager.getDefaultDisplay();
		int Width = display.getWidth();
		int Height = display.getHeight();
		gridView = new GridView(context);
		gridView.setNumColumns(7);
		gridView.setGravity(Gravity.CENTER_VERTICAL);
		//gridView.setSelector(Color.TRANSPARENT);
		gridView.setVerticalSpacing(1);
		gridView.setHorizontalSpacing(1);
		gridView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				return CalendarView.this.gestureDetector.onTouchEvent(arg1);
			}
		});
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				// TODO Auto-generated method stub
				//如果点选日期在今日之前,直接退出
				if(calV.getSysYear() == Integer.valueOf(calV.getShowYear()) && calV.getSysMonth() >= Integer.valueOf(calV.getShowMonth()) 
						&& calV.getSysDay() > Integer.valueOf(calV.getDateByClickItem(position))){
							return;
				}else{
					clickDate[0] = Integer.parseInt(calV.getShowYear());
					clickDate[1] = Integer.parseInt(calV.getShowMonth());
					clickDate[2] = Integer.parseInt(calV.getDateByClickItem(position));
					callBack.CallBackDate(clickDate);
				}
			}
			
		});
		
	}
	
	public View getCalendarView(){
		return convertView;
	}
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值