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

1.毫秒设置成1000,实际上获取出来的时间是下个月的1号0点,改为999,防止出现其他月份;2. 滑动日历时,添加对月份变化的监听 #365

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.github.gzuliyujiang.calendarpicker.core.FestivalProvider;
import com.github.gzuliyujiang.calendarpicker.core.ItemViewProvider;
import com.github.gzuliyujiang.calendarpicker.core.OnDateSelectedListener;
import com.github.gzuliyujiang.calendarpicker.listener.OnPageChangeCallback;
import com.github.gzuliyujiang.calendarpicker.listener.ScrollEventAdapter;
import com.github.gzuliyujiang.dialog.DialogConfig;
import com.github.gzuliyujiang.dialog.DialogStyle;
import com.github.gzuliyujiang.dialog.ModalDialog;
Expand All @@ -47,6 +49,7 @@
public class CalendarPicker extends ModalDialog implements OnDateSelectedListener {
private CalendarView calendarView;
private CalendarAdapter calendarAdapter;
private ScrollEventAdapter mScrollEventAdapter;
private ColorScheme colorScheme;
private boolean singleMode = false;
private FestivalProvider festivalProvider;
Expand Down Expand Up @@ -104,8 +107,26 @@ protected void initData() {
maxCalendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(maxCalendar.getTime()));
maxDate = maxCalendar.getTime();
}
if (mScrollEventAdapter == null) {
mScrollEventAdapter = new ScrollEventAdapter(calendarView.getBodyView());
calendarView.getBodyView().addOnScrollListener(mScrollEventAdapter);
}
calendarAdapter = calendarView.getAdapter();
calendarAdapter.setOnCalendarSelectedListener(this);
mScrollEventAdapter.setOnPageChangeCallback(new OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (calendarAdapter != null) {
Date cDate = calendarAdapter.getDateValue(position);
if (onSingleDatePickListener != null) {
onSingleDatePickListener.onMonthChanged(cDate);
}
if (onRangeDatePickListener != null) {
onRangeDatePickListener.onMonthChanged(cDate);
}
}
}
});
refreshData();
}

Expand Down Expand Up @@ -331,4 +352,14 @@ public final CalendarView getCalendarView() {
return calendarView;
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mScrollEventAdapter != null && calendarView != null) {
mScrollEventAdapter.setOnPageChangeCallback(null);
calendarView.getBodyView().removeOnScrollListener(mScrollEventAdapter);
mScrollEventAdapter = null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface OnRangeDatePickListener {

void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate);

void onMonthChanged(Date date);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface OnSingleDatePickListener {

void onSingleDatePicked(@NonNull Date date);

void onMonthChanged(Date date);

}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public final int getDatePosition(Date date) {
maxDate.set(Calendar.HOUR_OF_DAY, 23);
maxDate.set(Calendar.MINUTE, 59);
maxDate.set(Calendar.SECOND, 59);
maxDate.set(Calendar.MILLISECOND, 1000);
maxDate.set(Calendar.MILLISECOND, 999);
if (time >= minDate.getTime().getTime() && time <= maxDate.getTime().getTime()) {
return i;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.gzuliyujiang.calendarpicker.listener;

import androidx.annotation.Px;

public abstract class OnPageChangeCallback {
/**
* This method will be invoked when the current page is scrolled, either as part
* of a programmatically initiated smooth scroll or a user initiated touch scroll.
*
* @param position Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* @param positionOffset Value from [0, 1) indicating the offset from the page at position.
* @param positionOffsetPixels Value in pixels indicating the offset from position.
*/
public void onPageScrolled(int position, float positionOffset,
@Px int positionOffsetPixels) {
}

/**
* This method will be invoked when a new page becomes selected. Animation is not
* necessarily complete.
*
* @param position Position index of the new selected page.
*/
public void onPageSelected(int position) {
}

/**
* Called when the scroll state changes. Useful for discovering when the user begins
* dragging, when a fake drag is started, when the pager is automatically settling to the
* current page, or when it is fully stopped/idle. {@code state} can be one of {@link
*/
public void onPageScrollStateChanged(int state) {
}
}
Loading