Skip to content

Commit

Permalink
调试
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Nov 7, 2019
1 parent 8eb73a4 commit 19c9738
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_month_adapter);


// MonthCalendar monthCalendar = findViewById(R.id.monthCalendar);
// MonthCalendar monthCalendar = findViewById(R.id.monthCalendar);
// monthCalendar.setCalendarAdapter(new TestAdapter());

// WeekCalendar weekCalendar = findViewById(R.id.weekCalendar);

// monthCalendar.setCalendarAdapter(new TestAdapter());
//
// weekCalendar.setCalendarAdapter(new TestAdapter());


Expand All @@ -44,7 +46,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
public static class TestAdapter extends CalendarAdapter {

@Override
public View getCalendarView(Context context) {
public View getCalendarItemView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.item_calendar, null);
return view;
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_month_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
<!-- <com.necer.calendar.MonthCalendar-->
<!-- android:id="@+id/monthCalendar"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="300dp"/>-->
<!-- android:layout_height="300dp" />-->


<!-- <com.necer.calendar.WeekCalendar-->
<!-- <com.necer.calendar.WeekCalendar-->
<!-- android:id="@+id/weekCalendar"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="50dp"/>-->
Expand Down
8 changes: 7 additions & 1 deletion ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.NestedScrollingParent;

import android.util.AttributeSet;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -608,6 +611,9 @@ public void setOnCalendarMultipleChangedListener(OnCalendarMultipleChangedListen

@Override
public void updateSlideDistance(int currentDistance) {

Log.e("updateSlideDistance", "updateSlideDistance::iiii::");

monthCalendar.updateSlideDistance(currentDistance - weekHeight);
weekCalendar.updateSlideDistance(currentDistance - weekHeight);
}
Expand Down Expand Up @@ -658,7 +664,6 @@ public Attrs getAttrs() {
}



@Override
public void setCalendarAdapter(CalendarAdapter calendarAdapter) {
monthCalendar.setCalendarAdapter(calendarAdapter);
Expand Down Expand Up @@ -806,6 +811,7 @@ protected void dispatchDraw(Canvas canvas) {
childView.setY(calendarState == CalendarState.MONTH ? monthHeight : weekHeight);
isInflateFinish = true;
}
// TODO: 2019/11/7 有问题,会导致一直回调
updateSlideDistance((int) childView.getY());
}

Expand Down
77 changes: 9 additions & 68 deletions ncalendar/src/main/java/com/necer/helper/CalendarHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CalendarHelper {
private BaseCalendar mCalendar;

private CalendarType mCalendarType;
protected List<RectF> mRectFList;


protected LocalDate mStartDate;
protected LocalDate mEndDate;
Expand All @@ -49,15 +49,15 @@ public CalendarHelper(BaseCalendar calendar, LocalDate initialDate, CalendarType

mLineNum = mDateList.size() / 7;

mRectFList = getLocationRectFList();
//mRectFList = getLocationRectFList();

mAllSelectListDate = mCalendar.getAllSelectDateList();
mStartDate = mCalendar.getStartDate();
mEndDate = mCalendar.getEndDate();

mBgRectF = new RectF(0f, 0f, calendar.getMeasuredWidth(), calendar.getMeasuredHeight());

mGestureDetector = new GestureDetector(calendar.getContext(), simpleOnGestureListener);
// mGestureDetector = new GestureDetector(calendar.getContext(), simpleOnGestureListener);
}


Expand All @@ -73,8 +73,12 @@ public CalendarType getCalendarType() {
return mCalendarType;
}

public List<RectF> getRectFList() {
return mRectFList;
// public List<RectF> getRectFList() {
// return mRectFList;
// }

public int getCalendarHeight() {
return mCalendar.getMeasuredHeight();
}

public LocalDate getStartDate() {
Expand Down Expand Up @@ -154,7 +158,6 @@ public List<LocalDate> getCurrentDateList() {
}

public void dealClickDate(LocalDate localDate) {

if (mCalendarType == CalendarType.MONTH && CalendarUtil.isLastMonth(localDate, mInitialDate)) {
mCalendar.onClickLastMonthDate(localDate);
} else if (mCalendarType == CalendarType.MONTH && CalendarUtil.isNextMonth(localDate, mInitialDate)) {
Expand All @@ -173,59 +176,6 @@ public LocalDate getFirstDate() {
}
}


public GestureDetector getGestureDetector() {
return mGestureDetector;
}

private GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
for (int i = 0; i < mRectFList.size(); i++) {
RectF rectF = mRectFList.get(i);
if (rectF.contains((int) e.getX(), (int) e.getY())) {
LocalDate clickDate = mDateList.get(i);
dealClickDate(clickDate);
break;
}
}
return true;
}
};


private List<RectF> getLocationRectFList() {
List<RectF> rectFList = new ArrayList<>();
for (int i = 0; i < mLineNum; i++) {
for (int j = 0; j < 7; j++) {
RectF rectF = new RectF();
//矩形确定位置
float width = mCalendar.getMeasuredWidth();
float height = mCalendar.getMeasuredHeight();
//为每个矩形确定位置
if (mLineNum == 5 || mLineNum == 1) {
//5行的月份,5行矩形平分view的高度 mLineNum==1是周的情况
float rectHeight = height / mLineNum;
rectF.set(j * width / 7, i * rectHeight, j * width / 7 + width / 7, i * rectHeight + rectHeight);
} else {
//6行的月份,要第一行和最后一行矩形的中心分别和和5行月份第一行和最后一行矩形的中心对齐
//5行一个矩形高度 mHeight/5, 画图可知,4个5行矩形的高度等于5个6行矩形的高度 故:6行的每一个矩形高度是 (mHeight/5)*4/5
float rectHeight5 = height / 5;
float rectHeight6 = (height / 5) * 4 / 5;
rectF.set(j * width / 7, i * rectHeight6 + (rectHeight5 - rectHeight6) / 2, j * width / 7 + width / 7, i * rectHeight6 + rectHeight6 + (rectHeight5 - rectHeight6) / 2);
}
rectFList.add(rectF);
}
}
return rectFList;
}


public boolean isAvailableDate(LocalDate localDate) {
return mCalendar.isAvailable(localDate);
}
Expand All @@ -238,13 +188,4 @@ public boolean isCurrentMonthOrWeek(LocalDate localDate) {
}
}




public void notifyPager(Canvas canvas, View view) {



}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
public abstract class CalendarAdapter {


public abstract View getCalendarView(Context context);
public abstract View getCalendarItemView(Context context);
public View getCalendarBgView(Context context){
return null;
}

public abstract void onBindToadyView(View view, LocalDate localDate, List<LocalDate> selectedDateList);

Expand All @@ -24,5 +27,4 @@ public abstract class CalendarAdapter {

public abstract void onBindDisableDateView(View view, LocalDate localDate);


}
89 changes: 71 additions & 18 deletions ncalendar/src/main/java/com/necer/view/CalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ public class CalendarView extends View implements ICalendarView {

private int mCurrentDistance;//折叠日历滑动当前的距离

protected List<LocalDate> mDateList;
protected List<RectF> mRectFList;

public CalendarView(Context context, CalendarHelper calendarHelper) {
super(context);
mCalendarHelper = calendarHelper;
mDateList = calendarHelper.getDateList();
mRectFList = new ArrayList<>();

for (int i = 0; i < mDateList.size(); i++) {
mRectFList.add(new RectF());
}

}


Expand All @@ -59,31 +69,70 @@ private void drawBg(Canvas canvas, CalendarPainter calendarPainter) {
//绘制日期
private void drawDate(Canvas canvas, CalendarPainter calendarPainter) {

List<RectF> rectFList = mCalendarHelper.getRectFList();
List<LocalDate> dateList = mCalendarHelper.getDateList();

for (int i = 0; i < rectFList.size(); i++) {

LocalDate localDate = dateList.get(i);
RectF rectF = rectFList.get(i);
int lineNum = mCalendarHelper.getLineNum();


for (int i = 0; i < lineNum; i++) {
for (int j = 0; j < 7; j++) {
int index = i * 7 + j;
RectF rectF = mRectFList.get(index);
//矩形确定位置
float width = getMeasuredWidth();
float height = getMeasuredHeight();
//为每个矩形确定位置
if (lineNum == 5 || lineNum == 1) {
//5行的月份,5行矩形平分view的高度 mLineNum==1是周的情况
float rectHeight = height / lineNum;
rectF.set(j * width / 7, i * rectHeight, j * width / 7 + width / 7, i * rectHeight + rectHeight);
} else {
//6行的月份,要第一行和最后一行矩形的中心分别和和5行月份第一行和最后一行矩形的中心对齐
//5行一个矩形高度 mHeight/5, 画图可知,4个5行矩形的高度等于5个6行矩形的高度 故:6行的每一个矩形高度是 (mHeight/5)*4/5
float rectHeight5 = height / 5;
float rectHeight6 = (height / 5) * 4 / 5;
rectF.set(j * width / 7, i * rectHeight6 + (rectHeight5 - rectHeight6) / 2, j * width / 7 + width / 7, i * rectHeight6 + rectHeight6 + (rectHeight5 - rectHeight6) / 2);
}

if (mCalendarHelper.isAvailableDate(localDate)) { //可用的日期
if (mCalendarHelper.isCurrentMonthOrWeek(localDate)) { //当月日期
if (CalendarUtil.isToday(localDate)) { //当天
calendarPainter.onDrawToday(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
} else { //非当天的当月日期
calendarPainter.onDrawCurrentMonthOrWeek(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
//
LocalDate localDate = mDateList.get(index);
if (mCalendarHelper.isAvailableDate(localDate)) { //可用的日期
if (mCalendarHelper.isCurrentMonthOrWeek(localDate)) { //当月日期
if (CalendarUtil.isToday(localDate)) { //当天
calendarPainter.onDrawToday(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
} else { //非当天的当月日期
calendarPainter.onDrawCurrentMonthOrWeek(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
}
} else { //上下月日期
calendarPainter.onDrawLastOrNextMonth(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
}
} else { //上下月日期
calendarPainter.onDrawLastOrNextMonth(canvas, rectF, localDate, mCalendarHelper.getAllSelectListDate());
} else { //不可用日期
calendarPainter.onDrawDisableDate(canvas, rectF, localDate);
}
} else { //不可用日期
calendarPainter.onDrawDisableDate(canvas, rectF, localDate);

}
}

}

private GestureDetector mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
for (int i = 0; i < mRectFList.size(); i++) {
RectF rectF = mRectFList.get(i);
if (rectF.contains((int) e.getX(), (int) e.getY())) {
LocalDate clickDate = mDateList.get(i);
mCalendarHelper.dealClickDate(clickDate);
break;
}
}
return true;
}
});


@Override
public LocalDate getInitialDate() {
Expand All @@ -98,7 +147,7 @@ public LocalDate getMiddleLocalDate() {

@Override
public boolean onTouchEvent(MotionEvent event) {
return mCalendarHelper.getGestureDetector().onTouchEvent(event);
return mGestureDetector.onTouchEvent(event);
}

@Override
Expand All @@ -125,6 +174,8 @@ public List<LocalDate> getCurrentSelectDateList() {
@Override
public void updateSlideDistance(int currentDistance) {
this.mCurrentDistance = currentDistance;

Log.e("updateSlideDistance", "updateSlideDistance::33333:;");
invalidate();
}

Expand All @@ -136,6 +187,8 @@ public List<LocalDate> getCurrentDateList() {
@Override
public void notifyCalendarView() {
invalidate();

Log.e("invalidate", "invalidate:::;");
}

//周或者月的第一天
Expand Down
Loading

0 comments on commit 19c9738

Please sign in to comment.