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

Added: OnWeekChangedListener #873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -208,7 +208,12 @@ public void onPageSelected(int position) {
currentMonth = adapter.getItem(position);
updateUi();

dispatchOnMonthChanged(currentMonth);
if (adapter instanceof MonthPagerAdapter) {
dispatchOnMonthChanged(currentMonth);
}
if (adapter instanceof WeekPagerAdapter) {
dispatchOnWeekChanged(currentMonth);
}
}

@Override
Expand All @@ -226,6 +231,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
private OnDateSelectedListener listener;
private OnDateLongClickListener longClickListener;
private OnMonthChangedListener monthListener;
private OnWeekChangedListener weekListener;
private OnRangeSelectedListener rangeListener;

CharSequence calendarContentDescription;
Expand Down Expand Up @@ -1328,6 +1334,15 @@ public void setOnMonthChangedListener(OnMonthChangedListener listener) {
this.monthListener = listener;
}

/**
* Sets the listener to be notified upon week changes.
*
* @param listener thing to be notified
*/
public void setOnWeekChangedListener(OnWeekChangedListener listener) {
this.weekListener = listener;
}

/**
* Sets the listener to be notified upon a range has been selected.
*
Expand Down Expand Up @@ -1380,6 +1395,17 @@ protected void dispatchOnMonthChanged(final CalendarDay day) {
}
}

/**
* Dispatch date change events to a listener, if set
*
* @param day first day of the new week
*/
protected void dispatchOnWeekChanged(final CalendarDay day) {
if (weekListener != null) {
weekListener.onWeekChanged(MaterialCalendarView.this, day);
}
}

/**
* Call by {@link CalendarPagerView} to indicate that a day was clicked and we should handle it.
* This method will always process the click to the selected date.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.prolificinteractive.materialcalendarview;

/**
* The callback used to indicate the user changes the displayed week
*/
public interface OnWeekChangedListener {

/**
* Called upon change of the selected day
*
* @param widget the view associated with this listener
* @param date the week picked, as the first day of the week
*/
void onWeekChanged(MaterialCalendarView widget, CalendarDay date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.prolificinteractive.materialcalendarview.OnWeekChangedListener;
import com.prolificinteractive.materialcalendarview.sample.decorators.HighlightWeekendsDecorator;
import com.prolificinteractive.materialcalendarview.sample.decorators.MySelectorDecorator;
import com.prolificinteractive.materialcalendarview.sample.decorators.OneDayDecorator;
import org.threeten.bp.LocalDate;
import org.threeten.bp.Month;
import org.threeten.bp.format.DateTimeFormatter;

/**
* Shows off the most basic usage
*/
public class SwappableBasicActivityDecorated extends AppCompatActivity
implements OnDateSelectedListener {
implements OnDateSelectedListener, OnMonthChangedListener, OnWeekChangedListener {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");

private final OneDayDecorator oneDayDecorator = new OneDayDecorator();

Expand All @@ -31,6 +37,8 @@ public class SwappableBasicActivityDecorated extends AppCompatActivity
setContentView(R.layout.activity_basic_modes);
ButterKnife.bind(this);

widget.setOnMonthChangedListener(this);
widget.setOnWeekChangedListener(this);
widget.setOnDateChangedListener(this);
widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);

Expand Down Expand Up @@ -72,4 +80,17 @@ public void onSetMonthMode() {
.setCalendarDisplayMode(CalendarMode.MONTHS)
.commit();
}

@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Toast.makeText(this, "First day of Month: " + FORMATTER.format(date.getDate()),
Toast.LENGTH_SHORT).show();
}

@Override
public void onWeekChanged(MaterialCalendarView widget, CalendarDay date) {
Toast.makeText(this, "First day of Week: " + FORMATTER.format(date.getDate()),
Toast.LENGTH_SHORT).show();
}

}