Skip to content

Commit

Permalink
修复日历日期选择器月份可能缺失问题 See gzu-liyujiang/AndroidPicker#262
Browse files Browse the repository at this point in the history
  • Loading branch information
pandruszkowp committed Oct 26, 2023
1 parent d397c12 commit 7e78274
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
Expand All @@ -39,6 +40,7 @@
@SuppressWarnings("unused")
public class CalendarPicker extends ConfirmPicker implements OnCalendarSelectListener {
private CalendarView calendarView;
private FrameLayout bottomView;
private CalendarAdapter calendarAdapter;
private boolean rangePick = true;
private Date minDate, maxDate;
Expand All @@ -61,6 +63,7 @@ public CalendarPicker(@NonNull Activity activity, @StyleRes int themeResId) {
protected View createBodyView(@NonNull Activity activity) {
View view = View.inflate(activity, R.layout.calendar_picker, null);
calendarView = view.findViewById(R.id.calendar_picker_body);
bottomView = view.findViewById(R.id.calendar_picker_bottom);
return view;
}

Expand Down Expand Up @@ -108,6 +111,9 @@ public void onDoubleSelect(@NonNull Date before, @NonNull Date after) {
endDate = after;
}

/**
* 设置日期范围选择回调
*/
public void setOnRangeDatePickListener(OnRangeDatePickListener onRangeDatePickListener) {
this.rangePick = true;
this.onRangeDatePickListener = onRangeDatePickListener;
Expand All @@ -116,6 +122,9 @@ public void setOnRangeDatePickListener(OnRangeDatePickListener onRangeDatePickLi
}
}

/**
* 设置单个日期选择回调
*/
public void setOnSingleDatePickListener(OnSingleDatePickListener onSingleDatePickListener) {
this.rangePick = false;
this.onSingleDatePickListener = onSingleDatePickListener;
Expand All @@ -124,6 +133,9 @@ public void setOnSingleDatePickListener(OnSingleDatePickListener onSingleDatePic
}
}

/**
* 设置日期范围
*/
public void setRangeDate(Date minDate, Date maxDate) {
this.minDate = DateUtils.min(minDate, maxDate);
this.maxDate = DateUtils.max(minDate, maxDate);
Expand All @@ -132,10 +144,16 @@ public void setRangeDate(Date minDate, Date maxDate) {
}
}

/**
* 设置日期范围为当前年月之后的几个月
*/
public void setRangeDateOnFuture(int offsetMonth) {
if (offsetMonth < 0) {
offsetMonth = 0;
}
minDate = new Date(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(minDate);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MONTH, offsetMonth);
calendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(calendar.getTime()));
maxDate = calendar.getTime();
Expand All @@ -144,21 +162,45 @@ public void setRangeDateOnFuture(int offsetMonth) {
}
}

/**
* 设置默认选择的日期时间戳(单个日期选择模式)
*/
public void setSelectedDate(long timeInMillis) {
this.selectDate = new Date(timeInMillis);
setSelectedDate(new Date(timeInMillis));
}

/**
* 设置默认选择的日期(单个日期选择模式)
*/
public void setSelectedDate(Date date) {
this.selectDate = date;
if (initialized) {
refreshData();
}
}

/**
* 设置默认选择的日期时间戳(日期范围选择模式)
*/
public void setSelectedDate(long timeInMillisStart, long timeInMillisEnd) {
this.startDate = new Date(Math.min(timeInMillisStart, timeInMillisEnd));
this.endDate = new Date(Math.max(timeInMillisStart, timeInMillisEnd));
setSelectedDate(new Date(Math.min(timeInMillisStart, timeInMillisEnd)),
new Date(Math.max(timeInMillisStart, timeInMillisEnd)));
}

/**
* 设置默认选择的日期(日期范围选择模式)
*/
public void setSelectedDate(Date startDate, Date endDate) {
this.startDate = startDate;
this.endDate = endDate;
if (initialized) {
refreshData();
}
}

/**
* 设置选择区间提示语
*/
public void setIntervalNotes(String noteFrom, String noteTo) {
this.noteFrom = noteFrom;
this.noteTo = noteTo;
Expand All @@ -180,4 +222,12 @@ private void refreshData() {
calendarAdapter.select(startDate, endDate);
}

public final CalendarView getCalendarView() {
return calendarView;
}

public final FrameLayout getBottomView() {
return bottomView;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.github.gzuliyujiang.calendarpicker.calendar.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.ViewGroup;

Expand All @@ -27,11 +28,9 @@
import com.github.gzuliyujiang.calendarpicker.calendar.utils.TimeUtils;
import com.github.gzuliyujiang.calendarpicker.calendar.view.MonthView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* 日历适配器
Expand All @@ -41,10 +40,9 @@
public class CalendarAdapter extends RecyclerView.Adapter<CalendarViewHolder> implements OnMonthClickListener {
private final String TAG = CalendarAdapter.class.getSimpleName();
private final List<Date> dates = new ArrayList<>();
private Interval<Date> valid = new Interval<>();
private Interval<Date> select = new Interval<>();
private Interval<String> selectNote = new Interval<>();
//单选标记
private final Interval<Date> valid = new Interval<>();
private final Interval<Date> select = new Interval<>();
private final Interval<String> selectNote = new Interval<>();
private boolean singleFlag = false;

public CalendarAdapter() {
Expand All @@ -53,26 +51,15 @@ public CalendarAdapter() {
/**
* 设置选择日期范围
*
* @param sDate 开始时间
* @param eDate 结束时间
* @param startDate 开始时间
* @param endDate 结束时间
*/
public void setRange(Date sDate, Date eDate, boolean clean, boolean notify) {
List<Date> dates = DateUtils.fillMonths(sDate, eDate);
public void setRange(Date startDate, Date endDate, boolean clean, boolean notify) {
List<Date> dates = DateUtils.fillMonths(startDate, endDate);
setRange(dates, clean, notify);
}

public void setRange(String sTime, String eTime, String format, boolean clean, boolean notify) {
Date[] dates = new Date[2];
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.SIMPLIFIED_CHINESE);
dates[0] = sdf.parse(sTime);
dates[1] = sdf.parse(eTime);
} catch (Exception e) {
e.printStackTrace();
}
setRange(dates[0], dates[1], clean, notify);
}

@SuppressLint("NotifyDataSetChanged")
public void setRange(List<Date> list, boolean clean, boolean notify) {
if (clean) {
dates.clear();
Expand All @@ -90,6 +77,7 @@ public void single(boolean value) {
singleFlag = value;
}

@SuppressLint("NotifyDataSetChanged")
public void valid(String fromDay, String toDay) {
try {
Date from = TimeUtils.date(fromDay, TimeUtils.YY_MD);
Expand All @@ -106,6 +94,7 @@ public void valid(String fromDay, String toDay) {
notifyDataSetChanged();
}

@SuppressLint("NotifyDataSetChanged")
public void valid(Date from, Date to) {
valid.left(from);
valid.right(to);
Expand All @@ -129,6 +118,7 @@ public void intervalNotes(String noteFrom, String noteTo) {
* @param fromDay 开始时间
* @param toDay 结束时间
*/
@SuppressLint("NotifyDataSetChanged")
public void select(String fromDay, String toDay) {
try {
Date from = TimeUtils.date(fromDay, TimeUtils.YY_MD);
Expand All @@ -147,6 +137,7 @@ public void select(String fromDay, String toDay) {
* @param from 开始日期
* @param to 结束日期
*/
@SuppressLint("NotifyDataSetChanged")
public void select(Date from, Date to) {
select.left(from);
select.right(to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
/**
* Created by peng on 2017/8/2.
*/

public class DateUtils {

private static Calendar calendar(Date date) {
public static Calendar calendar(Date date) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(date);
return calendar;
Expand Down Expand Up @@ -121,13 +120,11 @@ public static List<Date> fillMonths(Date sDate, Date eDate) {
if (null == sDate || null == eDate) {
dates.add(new Date());
} else {
int months = months(sDate, eDate);
Calendar calendar = calendar(min(sDate, eDate));
int months = months(sDate, eDate);
for (int i = 0; i <= months; i++) {
dates.add(calendar.getTime());
int month = calendar.get(Calendar.MONTH);
month += 1;
calendar.set(Calendar.MONTH, month);
calendar.add(Calendar.MONTH, 1);
}
}
return dates;
Expand Down
11 changes: 0 additions & 11 deletions CalendarPicker/src/main/res/layout/calendar_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@
android:background="@android:color/white"
android:orientation="vertical">

<TextView
android:id="@+id/calendar_picker_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="选择日期"
android:textColor="#666666"
android:textSize="16sp" />

<com.github.gzuliyujiang.calendarpicker.calendar.view.CalendarView
android:id="@+id/calendar_picker_body"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
* 日历日期选择器
Expand All @@ -49,12 +48,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
CalendarAdapter calendarAdapter = calendarView.getAdapter();
calendarAdapter.single(false);
Date minDate = new Date(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(minDate);
Calendar calendar = DateUtils.calendar(minDate);
calendar.add(Calendar.MONTH, 3);
calendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(calendar.getTime()));
Date maxDate = calendar.getTime();
calendarAdapter.setRange(minDate, maxDate, true, false);
calendarAdapter.valid(minDate, maxDate);
}

public void onCalendarDateRange(View view) {
Expand Down

0 comments on commit 7e78274

Please sign in to comment.