Skip to content

Commit

Permalink
确保数据正确
Browse files Browse the repository at this point in the history
  • Loading branch information
骆大峰 committed Jun 28, 2017
1 parent f8706db commit fbb234c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 42 deletions.
9 changes: 4 additions & 5 deletions calendar/src/main/java/com/ldf/calendar/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;
Expand Down Expand Up @@ -163,9 +162,9 @@ public static int getTouchSlop(Context context) {
return ViewConfiguration.get(context).getScaledTouchSlop();
}

public static CalendarDate getSunday(int year, int month, int day) {// TODO: 16/12/12 得到一个CustomDate对象
public static CalendarDate getSunday(CalendarDate seedDate) {// TODO: 16/12/12 得到一个CustomDate对象
Calendar c = Calendar.getInstance();
String dateString = year + "-" + month + "-" + day;
String dateString = seedDate.toString();
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
Expand All @@ -182,9 +181,9 @@ public static CalendarDate getSunday(int year, int month, int day) {// TODO: 16/
c.get(Calendar.DAY_OF_MONTH));
}

public static CalendarDate getSaturday(int year, int month, int day) {// TODO: 16/12/12 得到一个CustomDate对象
public static CalendarDate getSaturday(CalendarDate seedDate) {// TODO: 16/12/12 得到一个CustomDate对象
Calendar c = Calendar.getInstance();
String dateString = year + "-" + month + "-" + day;
String dateString = seedDate.toString();
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.ldf.calendar.component.CalendarViewAdapter;
Expand All @@ -13,6 +14,7 @@

public class MonthPagerBehavior extends CoordinatorLayout.Behavior<MonthPager> {
private int top;
private int initRecyclerViewTop;
private int touchSlop = 24;

@Override
Expand All @@ -37,9 +39,9 @@ public boolean onDependentViewChanged(CoordinatorLayout parent, MonthPager child

int top = child.getTop();

if( dy > touchSlop * 2){
if( dy > touchSlop){
calendarViewAdapter.switchToMonth();
} else if(dy < - touchSlop * 2){
} else if(dy < - touchSlop){
calendarViewAdapter.switchToWeek(child.getRowIndex());
}

Expand All @@ -52,10 +54,30 @@ public boolean onDependentViewChanged(CoordinatorLayout parent, MonthPager child
}

child.offsetTopAndBottom(dy);
} else {
initRecyclerViewTop = dependency.getTop();
}

dependentViewTop = dependency.getTop();
top = child.getTop();

if((initRecyclerViewTop - dependentViewTop) >= child.getCellHeight()) {
calendarViewAdapter.switchToWeek(child.getRowIndex());
initRecyclerViewTop = dependentViewTop;
}
if((dependentViewTop - initRecyclerViewTop) >= child.getCellHeight()) {
calendarViewAdapter.switchToMonth();
initRecyclerViewTop = dependentViewTop;
}
// if(Math.abs(dependentViewTop - top) == child.getTopMovableDistance()) {
// calendarViewAdapter.switchToWeek(child.getRowIndex());
// }
// if(Math.abs(dependentViewTop - top) == child.getViewHeight()) {
// calendarViewAdapter.switchToMonth();
// }
Log.e("ldf","dependentViewTop = " + dependentViewTop);
Log.e("ldf","top = " + top);

return true;
// TODO: 16/12/8 dy为负时表示向上滑动,dy为正时表示向下滑动,dy为零时表示滑动停止
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, RecyclerView

@Override
public void onStopNestedScroll(final CoordinatorLayout parent, final RecyclerView child, View target) {
Log.e("ldf","onStopNestedScroll");
super.onStopNestedScroll(parent, child, target);
if (isGoingUp) {
if (initOffset - top > Utils.getTouchSlop(context)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;

import com.ldf.calendar.Const;
import com.ldf.calendar.Utils;
Expand All @@ -29,7 +28,6 @@ public CalendarRenderer(Calendar calendar , CalendarAttr attr , Context context)
this.calendar = calendar;
this.attr = attr;
this.context = context;
seedDate = new CalendarDate();
}

public void draw(Canvas canvas) {
Expand All @@ -44,13 +42,12 @@ public void onClickDate(int col, int row) {
return;
if (weeks[row] != null) {
if(attr.getCalendarType() == CalendarAttr.CalendayType.MONTH) {
Log.e("ldf","MONTH");
if(weeks[row].days[col].getState() == State.CURRENT_MONTH){
weeks[row].days[col].refreshState(State.SELECT);
selectedDate = weeks[row].days[col].getDate();
seedDate = weeks[row].days[col].getDate();
CalendarViewAdapter.saveDate(selectedDate);
onSelectDateListener.onSelectDate(selectedDate);
seedDate = selectedDate;
} else if (weeks[row].days[col].getState() == State.PAST_MONTH){
selectedDate = weeks[row].days[col].getDate();
CalendarViewAdapter.saveDate(selectedDate);
Expand All @@ -63,25 +60,28 @@ public void onClickDate(int col, int row) {
onSelectDateListener.onSelectDate(selectedDate);
}
} else {
Log.e("ldf","WEEK");
weeks[row].days[col].refreshState(State.SELECT);
selectedDate = weeks[row].days[col].getDate();
seedDate = weeks[row].days[col].getDate();
Log.e("ldf","saveDate = " + selectedDate.toString());
CalendarViewAdapter.saveDate(selectedDate);
onSelectDateListener.onSelectDate(selectedDate);
seedDate = selectedDate;

// if(CalendarViewAdapter.weekArrayType == 1) {
// seedDate = Utils.getSaturday(selectedDate);
// } else {
// seedDate = Utils.getSunday(selectedDate);
// }
}
}
}

public void updateWeek(int rowIndex) {
CalendarDate currentWeekLastDay;
if(CalendarViewAdapter.weekArrayType == 1) {
currentWeekLastDay = Utils.getSaturday(seedDate.year , seedDate.month , seedDate.day);
currentWeekLastDay = Utils.getSaturday(seedDate);
} else {
currentWeekLastDay = Utils.getSunday(seedDate.year , seedDate.month , seedDate.day);
currentWeekLastDay = Utils.getSunday(seedDate);
}
Log.e("ldf","currentWeekLastDay = " + currentWeekLastDay.toString());
int day = currentWeekLastDay.day;
for (int i = Const.TOTAL_COL - 1; i >= 0 ; i --) {
CalendarDate date = currentWeekLastDay.modifyDay(day);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public CalendarViewAdapter(Context context ,
}

private void init(Context context, OnSelectDateListener onSelectDateListener) {
seedDate = new CalendarDate();//初始化的种子日期为今天
Log.e("ldf","init saveDate = " + seedDate.toString());
saveDate(seedDate);
saveDate(new CalendarDate());
seedDate = new CalendarDate().modifyDay(1);//初始化的种子日期为今天
for (int i = 0; i < 3; i++) {
Calendar calendar = new Calendar(context , onSelectDateListener);
calendar.setOnAdapterSelectListener(new OnAdapterSelectListener() {
Expand Down Expand Up @@ -78,15 +77,16 @@ public Object instantiateItem(ViewGroup container, int position) {
Calendar calendar = calendars.get(position % calendars.size());
if(calendarType == CalendarAttr.CalendayType.MONTH) {
CalendarDate current = seedDate.modifyMonth(position - MonthPager.CURRENT_DAY_INDEX);
calendar.resetSelectedRowIndex();//月切换的时候选择selected row 为第一行
Log.e("ldf","current = " + current.toString());
current.setDay(1);//每月的种子日期都是1号
calendar.showDate(current);
} else {
CalendarDate current = seedDate.modifyWeek(position - MonthPager.CURRENT_DAY_INDEX);
if(weekArrayType == 1) {
calendar.showDate(Utils.getSaturday(current.year , current.month , current.day));
calendar.showDate(Utils.getSaturday(current));
} else {
calendar.showDate(Utils.getSunday(current.year , current.month , current.day));
}
calendar.showDate(Utils.getSunday(current));
}//每周的种子日期为这一周的最后一天
calendar.updateWeek(rowCount);
}
calendar.getCellHeight();
Expand Down Expand Up @@ -126,12 +126,12 @@ public void updateAllSelectState(){
Calendar calendar = calendars.get(i);
calendar.update();
if(calendar.getCalendarType() == CalendarAttr.CalendayType.WEEK) {
Log.e("ldf","updateWeek");
calendar.updateWeek(rowCount);
}
}
}


public void setMarkData(HashMap<String, String> markData) {
Utils.setMarkData(markData);
}
Expand All @@ -149,12 +149,14 @@ public void switchToMonth() {

Calendar v2 = calendars.get((currentPosition - 1) % 3);//2
v2.switchCalendarType(CalendarAttr.CalendayType.MONTH);
CalendarDate last = seedDate.modifyMonth(-1);
CalendarDate last = seedDate.modifyMonth(-1);
last.setDay(1);
v2.showDate(last);

Calendar v3 = calendars.get((currentPosition + 1) % 3);//1
v3.switchCalendarType(CalendarAttr.CalendayType.MONTH);
CalendarDate next = seedDate.modifyMonth(1);
CalendarDate next = seedDate.modifyMonth(1);
next.setDay(1);
v3.showDate(next);
}
}
Expand All @@ -167,6 +169,8 @@ public void switchToWeek(int rowIndex) {
Calendar v = calendars.get(currentPosition % 3);
seedDate = v.getSeedDate();

rowCount = v.getSelectedRowIndex();

Calendar v1 = calendars.get(currentPosition % 3);
v1.switchCalendarType(CalendarAttr.CalendayType.WEEK);
v1.showDate(seedDate);
Expand All @@ -175,48 +179,65 @@ public void switchToWeek(int rowIndex) {
Calendar v2 = calendars.get((currentPosition - 1) % 3);
v2.switchCalendarType(CalendarAttr.CalendayType.WEEK);
CalendarDate last = seedDate.modifyWeek(-1);
v2.showDate(last);
if(weekArrayType == 1) {
v2.showDate(Utils.getSaturday(last));
} else {
v2.showDate(Utils.getSunday(last));
}//每周的种子日期为这一周的最后一天
v2.updateWeek(rowIndex);

Calendar v3 = calendars.get((currentPosition + 1) % 3);
v3.switchCalendarType(CalendarAttr.CalendayType.WEEK);
CalendarDate next = seedDate.modifyWeek(1);
v3.showDate(next);
if(weekArrayType == 1) {
v3.showDate(Utils.getSaturday(next));
} else {
v3.showDate(Utils.getSunday(next));
}//每周的种子日期为这一周的最后一天
v3.updateWeek(rowIndex);
}
}

public void notifyDataChanged(CalendarDate date){
seedDate = date;
Log.e("ldf","notifyDataChanged saveDate = " + seedDate.toString());
saveDate(date);
if(calendarType == CalendarAttr.CalendayType.WEEK) {
MonthPager.CURRENT_DAY_INDEX = currentPosition;
Calendar v1 = calendars.get(currentPosition % 3);
v1.showDate(date);
v1.showDate(seedDate);
v1.updateWeek(rowCount);

Calendar v2 = calendars.get((currentPosition - 1) % 3);
CalendarDate last = date.modifyWeek(-1);
v2.showDate(last);
CalendarDate last = seedDate.modifyWeek(-1);
if(weekArrayType == 1) {
v2.showDate(Utils.getSaturday(last));
} else {
v2.showDate(Utils.getSunday(last));
}//每周的种子日期为这一周的最后一天
v2.updateWeek(rowCount);

Calendar v3 = calendars.get((currentPosition + 1) % 3);
CalendarDate next = date.modifyWeek(1);
v3.showDate(next);
CalendarDate next = seedDate.modifyWeek(1);
if(weekArrayType == 1) {
v3.showDate(Utils.getSaturday(next));
} else {
v3.showDate(Utils.getSunday(next));
}//每周的种子日期为这一周的最后一天
v3.updateWeek(rowCount);
} else {
MonthPager.CURRENT_DAY_INDEX = currentPosition;

Calendar v1 = calendars.get(currentPosition % 3);//0
v1.showDate(date);
v1.showDate(seedDate);

Calendar v2 = calendars.get((currentPosition - 1) % 3);//2
CalendarDate last = date.modifyMonth(-1);
CalendarDate last = seedDate.modifyMonth(-1);
last.setDay(1);
v2.showDate(last);

Calendar v3 = calendars.get((currentPosition + 1) % 3);//1
CalendarDate next = date.modifyMonth(1);
CalendarDate next = seedDate.modifyMonth(1);
next.setDay(1);
v3.showDate(next);
}
}
Expand Down
4 changes: 0 additions & 4 deletions calendar/src/main/java/com/ldf/calendar/view/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

Expand Down Expand Up @@ -82,7 +81,6 @@ public boolean onTouchEvent(MotionEvent event) {
float disX = event.getX() - posX;
float disY = event.getY() - posY;
if (Math.abs(disX) < touchSlop && Math.abs(disY) < touchSlop) {
Log.e("ldf","onClickDate");
int col = (int) (posX / cellWidth);
int row = (int) (posY / cellHeight);
onAdapterSelectListener.cancelSelectState();
Expand Down Expand Up @@ -147,6 +145,4 @@ public CalendarDate getSeedDate() {
public void setWeeks(Week[] weeks){
renderer.setWeeks(weeks);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void setCurrentPosition(int currentPosition) {
public int getRowIndex() {
CalendarViewAdapter calendarViewAdapter = (CalendarViewAdapter) getAdapter();
rowIndex = calendarViewAdapter.getPagers().get(currentPosition % 3).getSelectedRowIndex();
Log.e("ldf","getRowIndex = " + rowIndex);
return rowIndex;
}

Expand Down

0 comments on commit fbb234c

Please sign in to comment.