Merge pull request #14 from TouchInstinct/calendar_new_field_in_header

add an year to a header
This commit is contained in:
Gavriil 2016-12-05 12:35:22 +03:00 committed by GitHub
commit 5d8abbc31a
3 changed files with 27 additions and 10 deletions

View File

@ -174,10 +174,14 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
* Bind data to a Header ViewHolder.
*
* @param viewHolder ViewHolder for binding;
* @param year year;
* @param monthName Name of month;
* @param firstMonth True if bind called for the first month in calendar.
*/
protected abstract void bindHeaderItem(@NonNull final THeaderViewHolder viewHolder, @NonNull final String monthName, final boolean firstMonth);
protected abstract void bindHeaderItem(@NonNull final THeaderViewHolder viewHolder,
final int year,
@NonNull final String monthName,
final boolean firstMonth);
/**
* Bind data to an Empty ViewHolder.
@ -212,9 +216,10 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
final String monthName = monthsNames != null ? monthsNames[((CalendarHeaderItem) calendarItem).getMonth()]
: String.valueOf(((CalendarHeaderItem) calendarItem).getMonth());
bindHeaderItem((THeaderViewHolder) holder, monthName, position == 0);
final CalendarHeaderItem calendarHeaderItem = (CalendarHeaderItem) calendarItem;
final String monthName = monthsNames != null ? monthsNames[calendarHeaderItem.getMonth()]
: String.valueOf(calendarHeaderItem.getMonth());
bindHeaderItem((THeaderViewHolder) holder, calendarHeaderItem.getYear(), monthName, position == 0);
} else if (calendarItem instanceof CalendarEmptyItem) {
if (startSelectionPosition != null && endSelectionPosition != null
&& position >= startSelectionPosition && position <= endSelectionPosition) {

View File

@ -25,16 +25,27 @@ package ru.touchin.templates.calendar;
*/
public class CalendarHeaderItem implements CalendarItem {
private final int year;
private final int month;
private final int startRange;
private final int endRange;
public CalendarHeaderItem(final int month, final int startRange, final int endRange) {
public CalendarHeaderItem(final int year, final int month, final int startRange, final int endRange) {
this.year = year;
this.month = month;
this.startRange = startRange;
this.endRange = endRange;
}
/**
* Returns year.
*
* @return year.
*/
public int getYear() {
return year;
}
/**
* Returns number of month (where 0 is January and 11 is December).
*

View File

@ -59,7 +59,7 @@ public final class CalendarUtils {
* @param calendarItems List of {@link CalendarItem} where need to find specific element;
* @param position Position of adapter;
* @return Position of Header that respond to requested position.
* Returns null if Header or related CalendarItem was not found for specified position.
* Returns null if Header or related CalendarItem was not found for specified position.
*/
@Nullable
public static Integer findPositionOfSelectedMonth(@NonNull final List<CalendarItem> calendarItems, final long position) {
@ -76,7 +76,7 @@ public final class CalendarUtils {
* @param calendarItems List of {@link CalendarItem} where need to find specific element;
* @param date Requested date in milliseconds.
* @return Position of Calendar cell that that has specific date.
* Returns null if CalendarItem was not found for specified position.
* Returns null if CalendarItem was not found for specified position.
*/
@Nullable
public static Integer findPositionByDate(@NonNull final List<CalendarItem> calendarItems, final long date) {
@ -162,7 +162,7 @@ public final class CalendarUtils {
shift += (DAYS_IN_WEEK - firstDayInWeek);
}
calendarItems.add(new CalendarHeaderItem(tempTime.getMonthOfYear() - 1, shift + daysEnded, shift + daysEnded));
calendarItems.add(new CalendarHeaderItem(tempTime.getYear(), tempTime.getMonthOfYear() - 1, shift + daysEnded, shift + daysEnded));
shift += 1;
if (firstDayInWeek != 0) {
@ -225,7 +225,8 @@ public final class CalendarUtils {
final int firstDate = temp.getDayOfMonth() - 1; //?? - 1 ?
// add first month header
calendarItems.add(new CalendarHeaderItem(temp.get(DateTimeFieldType.monthOfYear()) - 1, shift, shift)); // is Month starts from 1 or 0 ?
calendarItems.add(new CalendarHeaderItem(temp.getYear(),
temp.get(DateTimeFieldType.monthOfYear()) - 1, shift, shift)); // is Month starts from 1 or 0 ?
temp = temp.withDayOfMonth(1);
shift += 1;
@ -261,7 +262,7 @@ public final class CalendarUtils {
final int firstDayInNextMonth = nextMonthFirstDay.getDayOfWeek() - 1;
calendarItems.add(new CalendarEmptyItem(shift + 1, shift + (7 - firstDayInNextMonth)));
shift += 7 - firstDayInNextMonth + 1;
calendarItems.add(new CalendarHeaderItem(nextMonthFirstDay.getMonthOfYear() - 1, shift, shift));
calendarItems.add(new CalendarHeaderItem(nextMonthFirstDay.getYear(), nextMonthFirstDay.getMonthOfYear() - 1, shift, shift));
shift += 1;
calendarItems.add(new CalendarEmptyItem(shift, shift + firstDayInNextMonth - 1));
}