Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix onDateSelected not called #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fobidlim
Copy link

Fix HorizontalCalendarListener#onDateSelected not called when first date selected.
Fix #90

@davcpas1234
Copy link

davcpas1234 commented Jan 11, 2019

Fix #95

This will only work if the first selected date is the oldest

In order to fix this if the first selected date is the newest:

if ((snapDistance[0] != -1) && (snapDistance[0] != -2) && (snapDistance[0] != 1) && (snapDistance[0] != 2)){
    return snapView;
}

@fobidlim
Copy link
Author

fobidlim commented Feb 4, 2019

Thank you for reviewing.

@oddEvenAndroid
Copy link

What is the snapDistance ?

@jjimenez0611
Copy link

Yes.. What is the snapDistance ?

where put this code:
if ((snapDistance[0] != -1) && (snapDistance[0] != -2) && (snapDistance[0] != 1) && (snapDistance[0] != 2)){
return snapView;
}

@jjimenez0611
Copy link

with this fix, the method calls two times when I made click on item and not when I scroll

if ((snapDistance[0] != -1) && (snapDistance[0] != -2) && (snapDistance[0] != 1) && (snapDistance[0] != 2)){
return snapView;
}

@jjimenez0611
Copy link

This solution only works when the user scroll on the calendar, but if the user click on item the method notifyCalendarListener call two times because the snapDistance[0] is equal to 1 always...

@jjimenez0611
Copy link

Hi this is my solution, I check if the calendar is on the first position or in the last position, with the method isItemDisabled.. The others solution work in some cases, but for some reason the behavior of the calendar is different when the user scroll or the user click on some item or if the user scroll fast the values of snapDistance always get different values, for example, if the user click on item the values are -1,0 or 1,0 if the user scroll fast the values are 0,0, if the user scroll slow the values are for example -31,0 and then 0,0.... So to solve the problem is call the method when the values are different of 0,0 and are not in the begin or end of the scroll

@OverRide
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
View snapView = super.findSnapView(layoutManager);

    if (calendarView.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING) {
        int selectedItemPosition;
        if (snapView == null) {
            // no snapping required
            selectedItemPosition = horizontalCalendar.getSelectedDatePosition();
        } else {
            selectedItemPosition = layoutManager.getPosition(snapView);
            int[] snapDistance = calculateDistanceToFinalSnap(layoutManager, snapView);
            boolean beforeItemIsDisable = horizontalCalendar.isItemDisabled(selectedItemPosition-1);
            boolean nextItemIsDisable = horizontalCalendar.isItemDisabled(selectedItemPosition+1);
            if (((snapDistance[0] != 0) || (snapDistance[1] != 0)) && !beforeItemIsDisable && !nextItemIsDisable) {
             return snapView;
            }
        }

        notifyCalendarListener(selectedItemPosition);
    }

    return snapView;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

onDateSelected not called first date
4 participants