1. 程式人生 > >手勢滑動日曆

手勢滑動日曆



CalendarGridViewAdapter.java

package com.dcs.test.Activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import com.dcs.test.R;
import android.app.Activity;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class CalendarGridViewAdapter extends BaseAdapter {
	private Calendar calStartDate = Calendar.getInstance();// 當前顯示的日曆
	private Calendar calSelected = Calendar.getInstance(); // 選擇的日曆
	public void setSelectedDate(Calendar cal)
	{
		calSelected=cal;
	}
	private Calendar calToday = Calendar.getInstance(); 
	private int iMonthViewCurrentMonth = 0; 
	private void UpdateStartDateForMonth() {
		calStartDate.set(Calendar.DATE, 1); 
		iMonthViewCurrentMonth = calStartDate.get(Calendar.MONTH);
		int iDay = 0;
		int iFirstDayOfWeek = Calendar.MONDAY;
		int iStartDay = iFirstDayOfWeek;
		if (iStartDay == Calendar.MONDAY) {
			iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
			if (iDay < 0)
				iDay = 6;
		}
		if (iStartDay == Calendar.SUNDAY) {
			iDay = calStartDate.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
			if (iDay < 0)
				iDay = 6;
		}
		calStartDate.add(Calendar.DAY_OF_WEEK, -iDay);
		calStartDate.add(Calendar.DAY_OF_MONTH, -1);
	}
	ArrayList<java.util.Date> titles;
	private ArrayList<java.util.Date> getDates() {

		UpdateStartDateForMonth();

		ArrayList<java.util.Date> alArrayList = new ArrayList<java.util.Date>();

		for (int i = 1; i <= 42; i++) {
			alArrayList.add(calStartDate.getTime());
			calStartDate.add(Calendar.DAY_OF_MONTH, 1);
		}
		return alArrayList;
	}
	private Activity activity;
	Resources resources;
	public CalendarGridViewAdapter(Activity a,Calendar cal) {
		calStartDate=cal;
		activity = a;
		resources=activity.getResources();
		titles = getDates();
	}
	public CalendarGridViewAdapter(Activity a) {
		activity = a;
		resources=activity.getResources();
	}
	@Override
	public int getCount() {
		return titles.size();
	}

	@Override
	public Object getItem(int position) {
		return titles.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout iv = new LinearLayout(activity);
		iv.setId(position + 5000);
		LinearLayout imageLayout = new LinearLayout(activity);
		imageLayout.setOrientation(0);
		iv.setGravity(Gravity.CENTER);
		iv.setOrientation(1);
		iv.setBackgroundColor(resources.getColor(R.color.white));
		Date myDate = (Date) getItem(position);
		Calendar calCalendar = Calendar.getInstance();
		calCalendar.setTime(myDate);
		final int iMonth = calCalendar.get(Calendar.MONTH);
		final int iDay = calCalendar.get(Calendar.DAY_OF_WEEK);
		// 判斷週六週日
		iv.setBackgroundColor(resources.getColor(R.color.white));
		if (iDay == 7) {
			// 週六
			iv.setBackgroundColor(resources.getColor(R.color.text_6));
		} else if (iDay == 1) {
			// 週日
			iv.setBackgroundColor(resources.getColor(R.color.text_7));
		} else {
		}
		// 判斷週六週日結束
		TextView txtToDay = new TextView(activity);
		txtToDay.setGravity(Gravity.CENTER_HORIZONTAL);
		txtToDay.setTextSize(9);
		if (equalsDate(calToday.getTime(), myDate)) {
			// 當前日期
			iv.setBackgroundColor(resources.getColor(R.color.event_center));
			txtToDay.setText("TODAY!");
		}
		// 設定背景顏色
		if (equalsDate(calSelected.getTime(), myDate)) {
			// 選擇的
			iv.setBackgroundColor(resources.getColor(R.color.selection));
		} else {
			if (equalsDate(calToday.getTime(), myDate)) {
				// 當前日期
				iv.setBackgroundColor(resources.getColor(R.color.calendar_zhe_day));
			}
		}
		// 設定背景顏色結束
		// 日期開始
		TextView txtDay = new TextView(activity);// 日期
		txtDay.setGravity(Gravity.CENTER_HORIZONTAL);
		// 判斷是否是當前月
		if (iMonth == iMonthViewCurrentMonth) {
			txtToDay.setTextColor(resources.getColor(R.color.ToDayText));
			txtDay.setTextColor(resources.getColor(R.color.Text));
		} else {
			txtDay.setTextColor(resources.getColor(R.color.noMonth));
			txtToDay.setTextColor(resources.getColor(R.color.noMonth));
		}
		int day = myDate.getDate(); // 日期
		txtDay.setText(String.valueOf(day));
		txtDay.setId(position + 500);
		iv.setTag(myDate);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		iv.addView(txtDay, lp);
		LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		iv.addView(txtToDay, lp1);
		return iv;
	}
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	private Boolean equalsDate(Date date1, Date date2) {

		if (date1.getYear() == date2.getYear()
				&& date1.getMonth() == date2.getMonth()
				&& date1.getDate() == date2.getDate()) {
			return true;
		} else {
			return false;
		}

	}

}


NumberHelper.java

public class NumberHelper {
	public static String LeftPad_Tow_Zero(int str) {
		java.text.DecimalFormat format = new java.text.DecimalFormat("00");
		return format.format(str);
	}
}

R.Java

package com.dcs.test;

public final class R {
    public static final class anim {
        public static final int slide_left_in=0x7f030000;
        public static final int slide_left_out=0x7f030001;
        public static final int slide_right_in=0x7f030002;
        public static final int slide_right_out=0x7f030003;
    }
    public static final class attr {
    }
    public static final class color {
        public static final int Noteback=0x7f040010;
        public static final int Text=0x7f04000e;
        public static final int ToDayText=0x7f04000f;
        public static final int br0=0x7f04000d;
        public static final int calendar_background=0x7f040001;
        public static final int calendar_zhe_day=0x7f040003;
        public static final int event_center=0x7f040004;
        public static final int forecast_point=0x7f04000c;
        public static final int frame=0x7f04000b;
        public static final int noMonth=0x7f040005;
        public static final int selection=0x7f040002;
        public static final int tempColor=0x7f04000a;
        public static final int text_6=0x7f040008;
        public static final int text_7=0x7f040009;
        public static final int title_text_6=0x7f040006;
        public static final int title_text_7=0x7f040007;
        public static final int white=0x7f040000;
    }
    public static final class drawable {
        public static final int bg=0x7f020000;
        public static final int icon=0x7f020001;
        public static final int jic_next=0x7f020002;
        public static final int jic_prev=0x7f020003;
        public static final int wigdet_bg=0x7f020004;
    }
    public static final class string {
        public static final int Fri=0x7f050005;
     
        public static final int Mon=0x7f050001;
        public static final int Sat=0x7f050006;
        public static final int Sun=0x7f050007;
        public static final int Thu=0x7f050004;
        public static final int Tue=0x7f050002;
        public static final int Wed=0x7f050003;
        public static final int app_name=0x7f050000;
    }
    public static final class style {
        public static final int Theme_Light=0x7f060000;
    }
}