Closed ggggxiaolong closed 6 years ago
package com.front.front.ui.common;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import com.ldf.calendar.Utils;
import com.ldf.calendar.component.CalendarAttr;
import com.ldf.calendar.component.CalendarViewAdapter;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.interf.OnSelectDateListener;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.Calendar;
import com.ldf.calendar.view.MonthPager;
import java.util.ArrayList;
/**
* @author mrtan on 11/17/17.
*/
public class SmartCalendarAdapter extends CalendarViewAdapter {
private int currentPosition;
private int rowCount;
private CalendarDate seedDate;
private CalendarAttr.CalendayType calendarType;
public SmartCalendarAdapter(
Context context,
OnSelectDateListener onSelectDateListener,
CalendarAttr.CalendayType calendarType,
IDayRenderer dayView,
CalendarDate calendarDate) {
super(context, onSelectDateListener, calendarType, dayView);
this.rowCount = 0;
this.calendarType = calendarType;
this.init(calendarDate);
}
private void init(CalendarDate calendarDate) {
saveDate(calendarDate);
this.seedDate = calendarDate;
}
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
this.currentPosition = position;
}
@Override @NonNull public Object instantiateItem(ViewGroup container, int position) {
if(position < 2) {
throw new UnsupportedOperationException("Required method instantiateItem was not overridden");
} else {
ArrayList<Calendar> pagers = getPagers();
Calendar calendar = pagers.get(position % pagers.size());
CalendarDate current;
if(calendarType == CalendarAttr.CalendayType.MONTH) {
current = this.seedDate.modifyMonth(position - MonthPager.CURRENT_DAY_INDEX);
current.setDay(1);
calendar.showDate(current);
} else {
current = this.seedDate.modifyWeek(position - MonthPager.CURRENT_DAY_INDEX);
if(weekArrayType == 1) {
calendar.showDate(Utils.getSaturday(current));
} else {
calendar.showDate(Utils.getSunday(current));
}
calendar.updateWeek(this.rowCount);
}
if(container.getChildCount() == pagers.size()) {
container.removeView((View) pagers.get(position % 3));
}
if(container.getChildCount() < pagers.size()) {
container.addView(calendar, 0);
} else {
container.addView(calendar, position % 3);
}
return calendar;
}
}
public void invalidateCurrentCalendar() {
ArrayList<Calendar> pagers = getPagers();
for(int i = 0; i < pagers.size(); ++i) {
Calendar calendar = pagers.get(i);
calendar.update();
if(calendar.getCalendarType() == CalendarAttr.CalendayType.WEEK) {
calendar.updateWeek(this.rowCount);
}
}
}
public void switchToMonth() {
if(getPagers() != null && getPagers().size() > 0 && calendarType != CalendarAttr.CalendayType.MONTH) {
calendarType = CalendarAttr.CalendayType.MONTH;
MonthPager.CURRENT_DAY_INDEX = this.currentPosition;
Calendar v = getPagers().get(this.currentPosition % 3);
this.seedDate = v.getSeedDate();
Calendar v1 = getPagers().get(this.currentPosition % 3);
v1.switchCalendarType(CalendarAttr.CalendayType.MONTH);
v1.showDate(this.seedDate);
Calendar v2 = getPagers().get((this.currentPosition - 1) % 3);
v2.switchCalendarType(CalendarAttr.CalendayType.MONTH);
CalendarDate last = this.seedDate.modifyMonth(-1);
last.setDay(1);
v2.showDate(last);
Calendar v3 = getPagers().get((this.currentPosition + 1) % 3);
v3.switchCalendarType(CalendarAttr.CalendayType.MONTH);
CalendarDate next = this.seedDate.modifyMonth(1);
next.setDay(1);
v3.showDate(next);
}
}
public void switchToWeek(int rowIndex) {
this.rowCount = rowIndex;
if(getPagers() != null && getPagers().size() > 0 && calendarType != CalendarAttr.CalendayType.WEEK) {
calendarType = CalendarAttr.CalendayType.WEEK;
MonthPager.CURRENT_DAY_INDEX = this.currentPosition;
Calendar v = getPagers().get(this.currentPosition % 3);
this.seedDate = v.getSeedDate();
this.rowCount = v.getSelectedRowIndex();
Calendar v1 = getPagers().get(this.currentPosition % 3);
v1.switchCalendarType(CalendarAttr.CalendayType.WEEK);
v1.showDate(this.seedDate);
v1.updateWeek(rowIndex);
Calendar v2 = getPagers().get((this.currentPosition - 1) % 3);
v2.switchCalendarType(CalendarAttr.CalendayType.WEEK);
CalendarDate last = this.seedDate.modifyWeek(-1);
if(weekArrayType == 1) {
v2.showDate(Utils.getSaturday(last));
} else {
v2.showDate(Utils.getSunday(last));
}
v2.updateWeek(rowIndex);
Calendar v3 = getPagers().get((this.currentPosition + 1) % 3);
v3.switchCalendarType(CalendarAttr.CalendayType.WEEK);
CalendarDate next = this.seedDate.modifyWeek(1);
if(weekArrayType == 1) {
v3.showDate(Utils.getSaturday(next));
} else {
v3.showDate(Utils.getSunday(next));
}
v3.updateWeek(rowIndex);
}
}
public void notifyDataChanged(CalendarDate date) {
this.seedDate = date;
saveDate(date);
Calendar v1;
Calendar v2;
CalendarDate last;
Calendar v3;
CalendarDate next;
ArrayList<Calendar> pagers = getPagers();
if(calendarType == CalendarAttr.CalendayType.WEEK) {
MonthPager.CURRENT_DAY_INDEX = this.currentPosition;
v1 = pagers.get(this.currentPosition % 3);
v1.showDate(this.seedDate);
v1.updateWeek(this.rowCount);
v2 = pagers.get((this.currentPosition - 1) % 3);
last = this.seedDate.modifyWeek(-1);
if(weekArrayType == 1) {
v2.showDate(Utils.getSaturday(last));
} else {
v2.showDate(Utils.getSunday(last));
}
v2.updateWeek(this.rowCount);
v3 = pagers.get((this.currentPosition + 1) % 3);
next = this.seedDate.modifyWeek(1);
if(weekArrayType == 1) {
v3.showDate(Utils.getSaturday(next));
} else {
v3.showDate(Utils.getSunday(next));
}
v3.updateWeek(this.rowCount);
} else {
MonthPager.CURRENT_DAY_INDEX = this.currentPosition;
v1 = pagers.get(this.currentPosition % 3);
v1.showDate(this.seedDate);
v2 = pagers.get((this.currentPosition - 1) % 3);
last = this.seedDate.modifyMonth(-1);
last.setDay(1);
v2.showDate(last);
v3 = pagers.get((this.currentPosition + 1) % 3);
next = this.seedDate.modifyMonth(1);
next.setDay(1);
v3.showDate(next);
}
}
@Override public CalendarAttr.CalendayType getCalendarType() {
return calendarType;
}
}
你把库下载下来 修改源代码
有人知道怎么修改seedDate吗? private 属性怎么修改? 难道用反射?