Skip to content

Commit 972fb7a

Browse files
committed
fix addEventClickListener
1 parent a77ba8f commit 972fb7a

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,13 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
312312
}
313313
}
314314

315+
float xOffset = getXStartPixel();
316+
317+
float x = e.getX() - xOffset;
318+
float y = e.getY() - mCurrentOrigin.y;
315319
// If the tap was on add new Event space, then trigger the callback
316-
if (mAddEventClickListener != null && mNewEventRect != null && mNewEventRect.rectF != null && e.getX() > mNewEventRect.rectF.left && e.getX() < mNewEventRect.rectF.right && e.getY() > mNewEventRect.rectF.top && e.getY() < mNewEventRect.rectF.bottom) {
320+
if (mAddEventClickListener != null && mNewEventRect != null && mNewEventRect.rectF != null &&
321+
mNewEventRect.rectF.contains(x,y)){
317322
mAddEventClickListener.onAddEventClicked(mNewEventRect.event.getStartTime(), mNewEventRect.event.getEndTime());
318323
return super.onSingleTapConfirmed(e);
319324
}
@@ -358,23 +363,21 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
358363

359364
WeekViewEvent newEvent = new WeekViewEvent(mNewEventIdentifier, "", null, selectedTime, endTime);
360365

361-
int marginTop = mHourHeight * mMinTime;
362-
float top = selectedTime.get(Calendar.HOUR_OF_DAY) * 60;
363-
top = mHourHeight * top / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical - marginTop;
364-
float bottom = endTime.get(Calendar.HOUR_OF_DAY) * 60;
365-
bottom = mHourHeight * bottom / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 - mEventMarginVertical - marginTop;
366+
float top = mHourHeight * getPassedMinutesInDay(selectedTime) / 60 + getEventsTop();
367+
float bottom = mHourHeight * getPassedMinutesInDay(endTime) / 60 + getEventsTop();
366368

367369
// Calculate left and right.
368-
float left = 0;
370+
float left = mWidthPerDay * WeekViewUtil.daysBetween(getFirstVisibleDay(), selectedTime);
369371
float right = left + mWidthPerDay;
372+
370373
// Add the new event if its bounds are valid
371374
if (left < right &&
372375
left < getWidth() &&
373376
top < getHeight() &&
374377
right > mHeaderColumnWidth &&
375378
bottom > 0
376379
) {
377-
RectF dayRectF = new RectF(left, top, right, bottom);
380+
RectF dayRectF = new RectF(left, top , right, bottom - mCurrentOrigin.y);
378381
newEvent.setColor(mNewEventColor);
379382
mNewEventRect = new EventRect(newEvent, newEvent, dayRectF);
380383
tempEvents.add(newEvent);
@@ -815,10 +818,9 @@ else if (mNewHourHeight > mMaxHourHeight)
815818
mCurrentOrigin.y = 0;
816819
}
817820

821+
int leftDaysWithGaps = getLeftDaysWithGaps();
818822
// Consider scroll offset.
819-
int leftDaysWithGaps = (int) -(Math.ceil(mCurrentOrigin.x / (mWidthPerDay + mColumnGap)));
820-
float startFromPixel = mCurrentOrigin.x + (mWidthPerDay + mColumnGap) * leftDaysWithGaps +
821-
mHeaderColumnWidth;
823+
float startFromPixel = getXStartPixel();
822824
float startPixel = startFromPixel;
823825

824826
// Prepare to iterate for each day.
@@ -985,9 +987,8 @@ else if (mNewHourHeight > mMaxHourHeight)
985987
* @return The time and date at the clicked position.
986988
*/
987989
private Calendar getTimeFromPoint(float x, float y) {
988-
int leftDaysWithGaps = (int) -(Math.ceil(mCurrentOrigin.x / (mWidthPerDay + mColumnGap)));
989-
float startPixel = mCurrentOrigin.x + (mWidthPerDay + mColumnGap) * leftDaysWithGaps +
990-
mHeaderColumnWidth;
990+
int leftDaysWithGaps = getLeftDaysWithGaps();
991+
float startPixel = getXStartPixel();
991992
for (int dayNumber = leftDaysWithGaps + 1;
992993
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays() + 1;
993994
dayNumber++) {
@@ -1040,6 +1041,25 @@ private void limitEventTime(List<Calendar> dates) {
10401041
}
10411042
}
10421043

1044+
private int getMinHourOffset(){
1045+
return mHourHeight * mMinTime;
1046+
}
1047+
1048+
private float getEventsTop(){
1049+
// Calculate top.
1050+
return mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical - getMinHourOffset();
1051+
1052+
}
1053+
1054+
private int getLeftDaysWithGaps(){
1055+
return (int) -(Math.ceil(mCurrentOrigin.x / (mWidthPerDay + mColumnGap)));
1056+
}
1057+
1058+
private float getXStartPixel(){
1059+
return mCurrentOrigin.x + (mWidthPerDay + mColumnGap) * getLeftDaysWithGaps() +
1060+
mHeaderColumnWidth;
1061+
}
1062+
10431063
/**
10441064
* Draw all the events of a particular day.
10451065
*
@@ -1051,14 +1071,8 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {
10511071
if (mEventRects != null && mEventRects.size() > 0) {
10521072
for (int i = 0; i < mEventRects.size(); i++) {
10531073
if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && !mEventRects.get(i).event.isAllDay()) {
1054-
1055-
int marginTop = mHourHeight * mMinTime;
1056-
// Calculate top.
1057-
float top = mHourHeight * mEventRects.get(i).top / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical - marginTop;
1058-
1059-
// Calculate bottom.
1060-
float bottom = mEventRects.get(i).bottom;
1061-
bottom = mHourHeight * bottom / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 - mEventMarginVertical - marginTop;
1074+
float top = mHourHeight * mEventRects.get(i).top / 60 + getEventsTop();
1075+
float bottom = mHourHeight * mEventRects.get(i).bottom / 60 + getEventsTop();
10621076

10631077
// Calculate left and right.
10641078
float left = startFromPixel + mEventRects.get(i).left * mWidthPerDay;
@@ -1081,7 +1095,7 @@ top < getHeight() &&
10811095
canvas.drawRoundRect(mEventRects.get(i).rectF, mEventCornerRadius, mEventCornerRadius, mEventBackgroundPaint);
10821096
float topToUse = top;
10831097
if (mEventRects.get(i).event.getStartTime().get(Calendar.HOUR_OF_DAY) < mMinTime)
1084-
topToUse = mHourHeight * getPassedMinutesInDay(mMinTime, 0) / 60 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2 + mEventMarginVertical - marginTop;
1098+
topToUse = mHourHeight * getPassedMinutesInDay(mMinTime, 0) / 60 + getEventsTop();
10851099

10861100
if (!mNewEventIdentifier.equals(mEventRects.get(i).event.getIdentifier()))
10871101
drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, topToUse, left);
@@ -1334,6 +1348,7 @@ private void cacheEvent(WeekViewEvent event) {
13341348
for (WeekViewEvent splitedEvent : splitedEvents) {
13351349
mEventRects.add(new EventRect(splitedEvent, event, null));
13361350
}
1351+
13371352
mEvents.add(event);
13381353
}
13391354

0 commit comments

Comments
 (0)