From 23073efbb4bdbfc7702a40e9c9e61109eb687e00 Mon Sep 17 00:00:00 2001 From: songzhiyong Date: Wed, 23 Sep 2015 11:19:03 +0800 Subject: [PATCH 1/2] add setSelectedDay() method in DayPickerView --- .../library/DayPickerView.java | 75 +++++++++---------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/library/src/main/java/com/andexert/calendarlistview/library/DayPickerView.java b/library/src/main/java/com/andexert/calendarlistview/library/DayPickerView.java index 1584aee..b34d76b 100644 --- a/library/src/main/java/com/andexert/calendarlistview/library/DayPickerView.java +++ b/library/src/main/java/com/andexert/calendarlistview/library/DayPickerView.java @@ -29,56 +29,48 @@ import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; -public class DayPickerView extends RecyclerView -{ +public class DayPickerView extends RecyclerView { protected Context mContext; - protected SimpleMonthAdapter mAdapter; - private DatePickerController mController; + protected SimpleMonthAdapter mAdapter; + private DatePickerController mController; protected int mCurrentScrollState = 0; - protected long mPreviousScrollPosition; - protected int mPreviousScrollState = 0; + protected long mPreviousScrollPosition; + protected int mPreviousScrollState = 0; private TypedArray typedArray; private OnScrollListener onScrollListener; - public DayPickerView(Context context) - { + public DayPickerView(Context context) { this(context, null); } - public DayPickerView(Context context, AttributeSet attrs) - { + public DayPickerView(Context context, AttributeSet attrs) { this(context, attrs, 0); } - public DayPickerView(Context context, AttributeSet attrs, int defStyle) - { + public DayPickerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - if (!isInEditMode()) - { + if (!isInEditMode()) { typedArray = context.obtainStyledAttributes(attrs, R.styleable.DayPickerView); setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); init(context); } } - public void setController(DatePickerController mController) - { + public void setController(DatePickerController mController) { this.mController = mController; setUpAdapter(); setAdapter(mAdapter); } - public void init(Context paramContext) { + public void init(Context paramContext) { setLayoutManager(new LinearLayoutManager(paramContext)); - mContext = paramContext; - setUpListView(); + mContext = paramContext; + setUpListView(); - onScrollListener = new OnScrollListener() - { + onScrollListener = new OnScrollListener() { @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) - { + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); final SimpleMonthView child = (SimpleMonthView) recyclerView.getChildAt(0); if (child == null) { @@ -89,34 +81,37 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) mPreviousScrollState = mCurrentScrollState; } }; - } + } + + protected void setUpAdapter() { + if (mAdapter == null) { + mAdapter = new SimpleMonthAdapter(getContext(), mController, typedArray); + } + mAdapter.notifyDataSetChanged(); + } - protected void setUpAdapter() { - if (mAdapter == null) { - mAdapter = new SimpleMonthAdapter(getContext(), mController, typedArray); + public void setSelectedDay(SimpleMonthAdapter.CalendarDay calendarDay) { + if (mAdapter != null) { + mAdapter.setSelectedDay(calendarDay); } - mAdapter.notifyDataSetChanged(); - } + } - protected void setUpListView() { - setVerticalScrollBarEnabled(false); - setOnScrollListener(onScrollListener); - setFadingEdgeLength(0); - } + protected void setUpListView() { + setVerticalScrollBarEnabled(false); + setOnScrollListener(onScrollListener); + setFadingEdgeLength(0); + } - public SimpleMonthAdapter.SelectedDays getSelectedDays() - { + public SimpleMonthAdapter.SelectedDays getSelectedDays() { return mAdapter.getSelectedDays(); } - protected DatePickerController getController() - { + protected DatePickerController getController() { return mController; } - protected TypedArray getTypedArray() - { + protected TypedArray getTypedArray() { return typedArray; } } \ No newline at end of file From 2a786fedc8696c060157d4de6a2a1e427f99b9d9 Mon Sep 17 00:00:00 2001 From: songzhiyong Date: Sat, 26 Sep 2015 09:20:20 +0800 Subject: [PATCH 2/2] format code to read --- .../library/CalendarUtils.java | 7 +- .../library/SimpleMonthAdapter.java | 181 +++++++--------- .../library/SimpleMonthView.java | 203 +++++++++--------- .../com/andexert/sample/MainActivity.java | 10 +- 4 files changed, 183 insertions(+), 218 deletions(-) diff --git a/library/src/main/java/com/andexert/calendarlistview/library/CalendarUtils.java b/library/src/main/java/com/andexert/calendarlistview/library/CalendarUtils.java index b319554..3bfb0bb 100644 --- a/library/src/main/java/com/andexert/calendarlistview/library/CalendarUtils.java +++ b/library/src/main/java/com/andexert/calendarlistview/library/CalendarUtils.java @@ -26,9 +26,8 @@ import java.util.Calendar; -public class CalendarUtils -{ - public static int getDaysInMonth(int month, int year) { +public class CalendarUtils { + public static int getDaysInMonth(int month, int year) { switch (month) { case Calendar.JANUARY: case Calendar.MARCH: @@ -48,5 +47,5 @@ public static int getDaysInMonth(int month, int year) { default: throw new IllegalArgumentException("Invalid Month"); } - } + } } diff --git a/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthAdapter.java b/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthAdapter.java index 20e1b0e..27cd2d6 100644 --- a/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthAdapter.java +++ b/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthAdapter.java @@ -39,34 +39,32 @@ public class SimpleMonthAdapter extends RecyclerView.Adapter implements SimpleMonthView.OnDayClickListener { protected static final int MONTHS_IN_YEAR = 12; private final TypedArray typedArray; - private final Context mContext; - private final DatePickerController mController; + private final Context mContext; + private final DatePickerController mController; private final Calendar calendar; private final SelectedDays selectedDays; private final Integer firstMonth; private final Integer lastMonth; - public SimpleMonthAdapter(Context context, DatePickerController datePickerController, TypedArray typedArray) { + public SimpleMonthAdapter(Context context, DatePickerController datePickerController, TypedArray typedArray) { this.typedArray = typedArray; calendar = Calendar.getInstance(); firstMonth = typedArray.getInt(R.styleable.DayPickerView_firstMonth, calendar.get(Calendar.MONTH)); lastMonth = typedArray.getInt(R.styleable.DayPickerView_lastMonth, (calendar.get(Calendar.MONTH) - 1) % MONTHS_IN_YEAR); selectedDays = new SelectedDays<>(); - mContext = context; - mController = datePickerController; - init(); - } + mContext = context; + mController = datePickerController; + init(); + } @Override - public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) - { + public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { final SimpleMonthView simpleMonthView = new SimpleMonthView(mContext, typedArray); return new ViewHolder(simpleMonthView, this); } @Override - public void onBindViewHolder(ViewHolder viewHolder, int position) - { + public void onBindViewHolder(ViewHolder viewHolder, int position) { final SimpleMonthView v = viewHolder.simpleMonthView; final HashMap drawingParams = new HashMap(); int month; @@ -82,15 +80,13 @@ public void onBindViewHolder(ViewHolder viewHolder, int position) int selectedFirstYear = -1; int selectedLastYear = -1; - if (selectedDays.getFirst() != null) - { + if (selectedDays.getFirst() != null) { selectedFirstDay = selectedDays.getFirst().day; selectedFirstMonth = selectedDays.getFirst().month; selectedFirstYear = selectedDays.getFirst().year; } - if (selectedDays.getLast() != null) - { + if (selectedDays.getLast() != null) { selectedLastDay = selectedDays.getLast().day; selectedLastMonth = selectedDays.getLast().month; selectedLastYear = selectedDays.getLast().year; @@ -112,12 +108,11 @@ public void onBindViewHolder(ViewHolder viewHolder, int position) } public long getItemId(int position) { - return position; - } + return position; + } @Override - public int getItemCount() - { + public int getItemCount() { int itemCount = (((mController.getMaxYear() - calendar.get(Calendar.YEAR)) + 1) * MONTHS_IN_YEAR); if (firstMonth != -1) @@ -129,12 +124,10 @@ public int getItemCount() return itemCount; } - public static class ViewHolder extends RecyclerView.ViewHolder - { + public static class ViewHolder extends RecyclerView.ViewHolder { final SimpleMonthView simpleMonthView; - public ViewHolder(View itemView, SimpleMonthView.OnDayClickListener onDayClickListener) - { + public ViewHolder(View itemView, SimpleMonthView.OnDayClickListener onDayClickListener) { super(itemView); simpleMonthView = (SimpleMonthView) itemView; simpleMonthView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); @@ -143,97 +136,90 @@ public ViewHolder(View itemView, SimpleMonthView.OnDayClickListener onDayClickLi } } - protected void init() { + protected void init() { if (typedArray.getBoolean(R.styleable.DayPickerView_currentDaySelected, false)) onDayTapped(new CalendarDay(System.currentTimeMillis())); - } + } - public void onDayClick(SimpleMonthView simpleMonthView, CalendarDay calendarDay) { - if (calendarDay != null) { - onDayTapped(calendarDay); + public void onDayClick(SimpleMonthView simpleMonthView, CalendarDay calendarDay) { + if (calendarDay != null) { + onDayTapped(calendarDay); } - } + } - protected void onDayTapped(CalendarDay calendarDay) { - mController.onDayOfMonthSelected(calendarDay.year, calendarDay.month, calendarDay.day); - setSelectedDay(calendarDay); - } + protected void onDayTapped(CalendarDay calendarDay) { + mController.onDayOfMonthSelected(calendarDay.year, calendarDay.month, calendarDay.day); + setSelectedDay(calendarDay); + } - public void setSelectedDay(CalendarDay calendarDay) { - if (selectedDays.getFirst() != null && selectedDays.getLast() == null) - { + public void setSelectedDay(CalendarDay calendarDay) { + if (selectedDays.getFirst() != null && selectedDays.getLast() == null) { selectedDays.setLast(calendarDay); - if (selectedDays.getFirst().month < calendarDay.month) - { + if (selectedDays.getFirst().month < calendarDay.month) { for (int i = 0; i < selectedDays.getFirst().month - calendarDay.month - 1; ++i) mController.onDayOfMonthSelected(selectedDays.getFirst().year, selectedDays.getFirst().month + i, selectedDays.getFirst().day); } mController.onDateRangeSelected(selectedDays); - } - else if (selectedDays.getLast() != null) - { + } else if (selectedDays.getLast() != null) { selectedDays.setFirst(calendarDay); selectedDays.setLast(null); - } - else + } else selectedDays.setFirst(calendarDay); - notifyDataSetChanged(); - } + notifyDataSetChanged(); + } - public static class CalendarDay implements Serializable - { + public static class CalendarDay implements Serializable { private static final long serialVersionUID = -5456695978688356202L; private Calendar calendar; - int day; - int month; - int year; + int day; + int month; + int year; - public CalendarDay() { - setTime(System.currentTimeMillis()); - } + public CalendarDay() { + setTime(System.currentTimeMillis()); + } - public CalendarDay(int year, int month, int day) { - setDay(year, month, day); - } + public CalendarDay(int year, int month, int day) { + setDay(year, month, day); + } - public CalendarDay(long timeInMillis) { - setTime(timeInMillis); - } + public CalendarDay(long timeInMillis) { + setTime(timeInMillis); + } - public CalendarDay(Calendar calendar) { - year = calendar.get(Calendar.YEAR); - month = calendar.get(Calendar.MONTH); - day = calendar.get(Calendar.DAY_OF_MONTH); - } + public CalendarDay(Calendar calendar) { + year = calendar.get(Calendar.YEAR); + month = calendar.get(Calendar.MONTH); + day = calendar.get(Calendar.DAY_OF_MONTH); + } - private void setTime(long timeInMillis) { - if (calendar == null) { - calendar = Calendar.getInstance(); + private void setTime(long timeInMillis) { + if (calendar == null) { + calendar = Calendar.getInstance(); } - calendar.setTimeInMillis(timeInMillis); - month = this.calendar.get(Calendar.MONTH); - year = this.calendar.get(Calendar.YEAR); - day = this.calendar.get(Calendar.DAY_OF_MONTH); - } - - public void set(CalendarDay calendarDay) { - year = calendarDay.year; - month = calendarDay.month; - day = calendarDay.day; - } - - public void setDay(int year, int month, int day) { - this.year = year; - this.month = month; - this.day = day; - } - - public Date getDate() - { + calendar.setTimeInMillis(timeInMillis); + month = this.calendar.get(Calendar.MONTH); + year = this.calendar.get(Calendar.YEAR); + day = this.calendar.get(Calendar.DAY_OF_MONTH); + } + + public void set(CalendarDay calendarDay) { + year = calendarDay.year; + month = calendarDay.month; + day = calendarDay.day; + } + + public void setDay(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public Date getDate() { if (calendar == null) { calendar = Calendar.getInstance(); } @@ -242,8 +228,7 @@ public Date getDate() } @Override - public String toString() - { + public String toString() { final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("{ year: "); stringBuilder.append(year); @@ -257,34 +242,28 @@ public String toString() } } - public SelectedDays getSelectedDays() - { + public SelectedDays getSelectedDays() { return selectedDays; } - public static class SelectedDays implements Serializable - { + public static class SelectedDays implements Serializable { private static final long serialVersionUID = 3942549765282708376L; private K first; private K last; - public K getFirst() - { + public K getFirst() { return first; } - public void setFirst(K first) - { + public void setFirst(K first) { this.first = first; } - public K getLast() - { + public K getLast() { return last; } - public void setLast(K last) - { + public void setLast(K last) { this.last = last; } } diff --git a/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthView.java b/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthView.java index f61bdef..dc79891 100644 --- a/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthView.java +++ b/library/src/main/java/com/andexert/calendarlistview/library/SimpleMonthView.java @@ -43,8 +43,7 @@ import java.util.HashMap; import java.util.Locale; -class SimpleMonthView extends View -{ +class SimpleMonthView extends View { public static final String VIEW_PARAMS_HEIGHT = "height"; public static final String VIEW_PARAMS_MONTH = "month"; @@ -60,13 +59,13 @@ class SimpleMonthView extends View private static final int SELECTED_CIRCLE_ALPHA = 128; protected static int DEFAULT_HEIGHT = 32; protected static final int DEFAULT_NUM_ROWS = 6; - protected static int DAY_SELECTED_CIRCLE_SIZE; - protected static int DAY_SEPARATOR_WIDTH = 1; - protected static int MINI_DAY_NUMBER_TEXT_SIZE; - protected static int MIN_HEIGHT = 10; - protected static int MONTH_DAY_LABEL_TEXT_SIZE; - protected static int MONTH_HEADER_SIZE; - protected static int MONTH_LABEL_TEXT_SIZE; + protected static int DAY_SELECTED_CIRCLE_SIZE; + protected static int DAY_SEPARATOR_WIDTH = 1; + protected static int MINI_DAY_NUMBER_TEXT_SIZE; + protected static int MIN_HEIGHT = 10; + protected static int MONTH_DAY_LABEL_TEXT_SIZE; + protected static int MONTH_HEADER_SIZE; + protected static int MONTH_LABEL_TEXT_SIZE; protected int mPadding = 0; @@ -108,18 +107,17 @@ class SimpleMonthView extends View protected int mYear; final Time today; - private final Calendar mCalendar; - private final Calendar mDayLabelCalendar; + private final Calendar mCalendar; + private final Calendar mDayLabelCalendar; private final Boolean isPrevDayEnabled; private int mNumRows = DEFAULT_NUM_ROWS; - private DateFormatSymbols mDateFormatSymbols = new DateFormatSymbols(); + private DateFormatSymbols mDateFormatSymbols = new DateFormatSymbols(); private OnDayClickListener mOnDayClickListener; - public SimpleMonthView(Context context, TypedArray typedArray) - { + public SimpleMonthView(Context context, TypedArray typedArray) { super(context); Resources resources = context.getResources(); @@ -160,9 +158,9 @@ private int calculateNumRows() { int dividend = (offset + mNumCells) / mNumDays; int remainder = (offset + mNumCells) % mNumDays; return (dividend + (remainder > 0 ? 1 : 0)); - } + } - private void drawMonthDayLabels(Canvas canvas) { + private void drawMonthDayLabels(Canvas canvas) { int y = MONTH_HEADER_SIZE - (MONTH_DAY_LABEL_TEXT_SIZE / 2); int dayWidthHalf = (mWidth - mPadding * 2) / (mNumDays * 2); @@ -172,64 +170,62 @@ private void drawMonthDayLabels(Canvas canvas) { mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay); canvas.drawText(mDateFormatSymbols.getShortWeekdays()[mDayLabelCalendar.get(Calendar.DAY_OF_WEEK)].toUpperCase(Locale.getDefault()), x, y, mMonthDayLabelPaint); } - } + } - private void drawMonthTitle(Canvas canvas) { + private void drawMonthTitle(Canvas canvas) { int x = (mWidth + 2 * mPadding) / 2; int y = (MONTH_HEADER_SIZE - MONTH_DAY_LABEL_TEXT_SIZE) / 2 + (MONTH_LABEL_TEXT_SIZE / 3); StringBuilder stringBuilder = new StringBuilder(getMonthAndYearString().toLowerCase()); stringBuilder.setCharAt(0, Character.toUpperCase(stringBuilder.charAt(0))); canvas.drawText(stringBuilder.toString(), x, y, mMonthTitlePaint); - } + } - private int findDayOffset() { + private int findDayOffset() { return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart) - mWeekStart; - } + } - private String getMonthAndYearString() { + private String getMonthAndYearString() { int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY; mStringBuilder.setLength(0); long millis = mCalendar.getTimeInMillis(); return DateUtils.formatDateRange(getContext(), millis, millis, flags); } - private void onDayClick(SimpleMonthAdapter.CalendarDay calendarDay) { - if (mOnDayClickListener != null && (isPrevDayEnabled || !((calendarDay.month == today.month) && (calendarDay.year == today.year) && calendarDay.day < today.monthDay))) { - mOnDayClickListener.onDayClick(this, calendarDay); + private void onDayClick(SimpleMonthAdapter.CalendarDay calendarDay) { + if (mOnDayClickListener != null && (isPrevDayEnabled || !((calendarDay.month == today.month) && (calendarDay.year == today.year) && calendarDay.day < today.monthDay))) { + mOnDayClickListener.onDayClick(this, calendarDay); } - } + } - private boolean sameDay(int monthDay, Time time) { - return (mYear == time.year) && (mMonth == time.month) && (monthDay == time.monthDay); - } + private boolean sameDay(int monthDay, Time time) { + return (mYear == time.year) && (mMonth == time.month) && (monthDay == time.monthDay); + } private boolean prevDay(int monthDay, Time time) { - return ((mYear < time.year)) || (mYear == time.year && mMonth < time.month) || ( mMonth == time.month && monthDay < time.monthDay); + return ((mYear < time.year)) || (mYear == time.year && mMonth < time.month) || (mMonth == time.month && monthDay < time.monthDay); } - protected void drawMonthNums(Canvas canvas) { - int y = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH + MONTH_HEADER_SIZE; - int paddingDay = (mWidth - 2 * mPadding) / (2 * mNumDays); - int dayOffset = findDayOffset(); - int day = 1; - - while (day <= mNumCells) { - int x = paddingDay * (1 + dayOffset * 2) + mPadding; - if ((mMonth == mSelectedBeginMonth && mSelectedBeginDay == day && mSelectedBeginYear == mYear) || (mMonth == mSelectedLastMonth && mSelectedLastDay == day && mSelectedLastYear == mYear)) { - if (mDrawRect) - { - RectF rectF = new RectF(x - DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) - DAY_SELECTED_CIRCLE_SIZE, x + DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) + DAY_SELECTED_CIRCLE_SIZE); - canvas.drawRoundRect(rectF, 10.0f, 10.0f,mSelectedCirclePaint); - } - else + protected void drawMonthNums(Canvas canvas) { + int y = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH + MONTH_HEADER_SIZE; + int paddingDay = (mWidth - 2 * mPadding) / (2 * mNumDays); + int dayOffset = findDayOffset(); + int day = 1; + + while (day <= mNumCells) { + int x = paddingDay * (1 + dayOffset * 2) + mPadding; + if ((mMonth == mSelectedBeginMonth && mSelectedBeginDay == day && mSelectedBeginYear == mYear) || (mMonth == mSelectedLastMonth && mSelectedLastDay == day && mSelectedLastYear == mYear)) { + if (mDrawRect) { + RectF rectF = new RectF(x - DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) - DAY_SELECTED_CIRCLE_SIZE, x + DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) + DAY_SELECTED_CIRCLE_SIZE); + canvas.drawRoundRect(rectF, 10.0f, 10.0f, mSelectedCirclePaint); + } else canvas.drawCircle(x, y - MINI_DAY_NUMBER_TEXT_SIZE / 3, DAY_SELECTED_CIRCLE_SIZE, mSelectedCirclePaint); } if (mHasToday && (mToday == day)) { mMonthNumPaint.setColor(mCurrentDayTextColor); mMonthNumPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); } else { - mMonthNumPaint.setColor(mDayNumColor); + mMonthNumPaint.setColor(mDayNumColor); mMonthNumPaint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); } @@ -246,66 +242,61 @@ protected void drawMonthNums(Canvas canvas) { if ((mSelectedBeginDay != -1 && mSelectedLastDay != -1 && mSelectedBeginYear == mSelectedLastYear && mSelectedBeginYear == mYear) && (((mMonth == mSelectedBeginMonth && mSelectedLastMonth == mSelectedBeginMonth) && ((mSelectedBeginDay < mSelectedLastDay && day > mSelectedBeginDay && day < mSelectedLastDay) || (mSelectedBeginDay > mSelectedLastDay && day < mSelectedBeginDay && day > mSelectedLastDay))) || - ((mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedBeginMonth && day > mSelectedBeginDay) || (mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedLastMonth && day < mSelectedLastDay)) || - ((mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedBeginMonth && day < mSelectedBeginDay) || (mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedLastMonth && day > mSelectedLastDay)))) - { + ((mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedBeginMonth && day > mSelectedBeginDay) || (mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedLastMonth && day < mSelectedLastDay)) || + ((mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedBeginMonth && day < mSelectedBeginDay) || (mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedLastMonth && day > mSelectedLastDay)))) { mMonthNumPaint.setColor(mSelectedDaysColor); } if ((mSelectedBeginDay != -1 && mSelectedLastDay != -1 && mSelectedBeginYear != mSelectedLastYear && ((mSelectedBeginYear == mYear && mMonth == mSelectedBeginMonth) || (mSelectedLastYear == mYear && mMonth == mSelectedLastMonth)) && (((mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedBeginMonth && day < mSelectedBeginDay) || (mSelectedBeginMonth < mSelectedLastMonth && mMonth == mSelectedLastMonth && day > mSelectedLastDay)) || - ((mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedBeginMonth && day > mSelectedBeginDay) || (mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedLastMonth && day < mSelectedLastDay))))) - { + ((mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedBeginMonth && day > mSelectedBeginDay) || (mSelectedBeginMonth > mSelectedLastMonth && mMonth == mSelectedLastMonth && day < mSelectedLastDay))))) { mMonthNumPaint.setColor(mSelectedDaysColor); } if ((mSelectedBeginDay != -1 && mSelectedLastDay != -1 && mSelectedBeginYear == mSelectedLastYear && mYear == mSelectedBeginYear) && ((mMonth > mSelectedBeginMonth && mMonth < mSelectedLastMonth && mSelectedBeginMonth < mSelectedLastMonth) || - (mMonth < mSelectedBeginMonth && mMonth > mSelectedLastMonth && mSelectedBeginMonth > mSelectedLastMonth))) - { + (mMonth < mSelectedBeginMonth && mMonth > mSelectedLastMonth && mSelectedBeginMonth > mSelectedLastMonth))) { mMonthNumPaint.setColor(mSelectedDaysColor); } if ((mSelectedBeginDay != -1 && mSelectedLastDay != -1 && mSelectedBeginYear != mSelectedLastYear) && ((mSelectedBeginYear < mSelectedLastYear && ((mMonth > mSelectedBeginMonth && mYear == mSelectedBeginYear) || (mMonth < mSelectedLastMonth && mYear == mSelectedLastYear))) || - (mSelectedBeginYear > mSelectedLastYear && ((mMonth < mSelectedBeginMonth && mYear == mSelectedBeginYear) || (mMonth > mSelectedLastMonth && mYear == mSelectedLastYear))))) - { + (mSelectedBeginYear > mSelectedLastYear && ((mMonth < mSelectedBeginMonth && mYear == mSelectedBeginYear) || (mMonth > mSelectedLastMonth && mYear == mSelectedLastYear))))) { mMonthNumPaint.setColor(mSelectedDaysColor); } - if (!isPrevDayEnabled && prevDay(day, today) && today.month == mMonth && today.year == mYear) - { + if (!isPrevDayEnabled && prevDay(day, today) && today.month == mMonth && today.year == mYear) { mMonthNumPaint.setColor(mPreviousDayColor); mMonthNumPaint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC)); } - canvas.drawText(String.format("%d", day), x, y, mMonthNumPaint); + canvas.drawText(String.format("%d", day), x, y, mMonthNumPaint); - dayOffset++; - if (dayOffset == mNumDays) { - dayOffset = 0; - y += mRowHeight; - } - day++; - } - } + dayOffset++; + if (dayOffset == mNumDays) { + dayOffset = 0; + y += mRowHeight; + } + day++; + } + } - public SimpleMonthAdapter.CalendarDay getDayFromLocation(float x, float y) { - int padding = mPadding; - if ((x < padding) || (x > mWidth - mPadding)) { - return null; - } + public SimpleMonthAdapter.CalendarDay getDayFromLocation(float x, float y) { + int padding = mPadding; + if ((x < padding) || (x > mWidth - mPadding)) { + return null; + } - int yDay = (int) (y - MONTH_HEADER_SIZE) / mRowHeight; - int day = 1 + ((int) ((x - padding) * mNumDays / (mWidth - padding - mPadding)) - findDayOffset()) + yDay * mNumDays; + int yDay = (int) (y - MONTH_HEADER_SIZE) / mRowHeight; + int day = 1 + ((int) ((x - padding) * mNumDays / (mWidth - padding - mPadding)) - findDayOffset()) + yDay * mNumDays; if (mMonth > 11 || mMonth < 0 || CalendarUtils.getDaysInMonth(mMonth, mYear) < day || day < 1) return null; - return new SimpleMonthAdapter.CalendarDay(mYear, mMonth, day); - } + return new SimpleMonthAdapter.CalendarDay(mYear, mMonth, day); + } - protected void initView() { + protected void initView() { mMonthTitlePaint = new Paint(); mMonthTitlePaint.setFakeBoldText(true); mMonthTitlePaint.setAntiAlias(true); @@ -345,23 +336,23 @@ protected void initView() { mMonthNumPaint.setStyle(Style.FILL); mMonthNumPaint.setTextAlign(Align.CENTER); mMonthNumPaint.setFakeBoldText(false); - } + } - protected void onDraw(Canvas canvas) { - drawMonthTitle(canvas); - drawMonthDayLabels(canvas); - drawMonthNums(canvas); - } + protected void onDraw(Canvas canvas) { + drawMonthTitle(canvas); + drawMonthDayLabels(canvas); + drawMonthNums(canvas); + } - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mRowHeight * mNumRows + MONTH_HEADER_SIZE); - } + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mRowHeight * mNumRows + MONTH_HEADER_SIZE); + } - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - mWidth = w; - } + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + mWidth = w; + } - public boolean onTouchEvent(MotionEvent event) { + public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { SimpleMonthAdapter.CalendarDay calendarDay = getDayFromLocation(event.getX(), event.getY()); if (calendarDay != null) { @@ -369,18 +360,18 @@ public boolean onTouchEvent(MotionEvent event) { } } return true; - } + } - public void reuse() { + public void reuse() { mNumRows = DEFAULT_NUM_ROWS; - requestLayout(); - } + requestLayout(); + } - public void setMonthParams(HashMap params) { + public void setMonthParams(HashMap params) { if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) { throw new InvalidParameterException("You must specify month and year for this view"); } - setTag(params); + setTag(params); if (params.containsKey(VIEW_PARAMS_HEIGHT)) { mRowHeight = params.get(VIEW_PARAMS_HEIGHT); @@ -413,10 +404,10 @@ public void setMonthParams(HashMap params) { mHasToday = false; mToday = -1; - mCalendar.set(Calendar.MONTH, mMonth); - mCalendar.set(Calendar.YEAR, mYear); - mCalendar.set(Calendar.DAY_OF_MONTH, 1); - mDayOfWeekStart = mCalendar.get(Calendar.DAY_OF_WEEK); + mCalendar.set(Calendar.MONTH, mMonth); + mCalendar.set(Calendar.YEAR, mYear); + mCalendar.set(Calendar.DAY_OF_MONTH, 1); + mDayOfWeekStart = mCalendar.get(Calendar.DAY_OF_WEEK); if (params.containsKey(VIEW_PARAMS_WEEK_START)) { mWeekStart = params.get(VIEW_PARAMS_WEEK_START); @@ -436,13 +427,13 @@ public void setMonthParams(HashMap params) { } mNumRows = calculateNumRows(); - } + } - public void setOnDayClickListener(OnDayClickListener onDayClickListener) { - mOnDayClickListener = onDayClickListener; - } + public void setOnDayClickListener(OnDayClickListener onDayClickListener) { + mOnDayClickListener = onDayClickListener; + } - public static abstract interface OnDayClickListener { - public abstract void onDayClick(SimpleMonthView simpleMonthView, SimpleMonthAdapter.CalendarDay calendarDay); - } + public static abstract interface OnDayClickListener { + public abstract void onDayClick(SimpleMonthView simpleMonthView, SimpleMonthAdapter.CalendarDay calendarDay); + } } \ No newline at end of file diff --git a/sample/src/main/java/com/andexert/sample/MainActivity.java b/sample/src/main/java/com/andexert/sample/MainActivity.java index ca37b63..702eb67 100644 --- a/sample/src/main/java/com/andexert/sample/MainActivity.java +++ b/sample/src/main/java/com/andexert/sample/MainActivity.java @@ -44,21 +44,17 @@ public boolean onOptionsItemSelected(MenuItem item) { } @Override - public int getMaxYear() - { + public int getMaxYear() { return 2015; } @Override - public void onDayOfMonthSelected(int year, int month, int day) - { + public void onDayOfMonthSelected(int year, int month, int day) { Log.e("Day Selected", day + " / " + month + " / " + year); } @Override - public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays selectedDays) - { - + public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays selectedDays) { Log.e("Date range selected", selectedDays.getFirst().toString() + " --> " + selectedDays.getLast().toString()); } }