Skip to content
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 @@ -130,7 +130,7 @@ public int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}
int index = indexOf((V) object);
if (index < 0) {
if ( index < 0) {
return POSITION_NONE;
}
return index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.threetenabp.AndroidThreeTen;
import com.prolificinteractive.materialcalendarview.format.ArrayWeekDayFormatter;
import com.prolificinteractive.materialcalendarview.format.DayFormatter;
import com.prolificinteractive.materialcalendarview.format.MonthArrayTitleFormatter;
Expand Down Expand Up @@ -249,6 +251,8 @@ public MaterialCalendarView(Context context) {
public MaterialCalendarView(Context context, AttributeSet attrs) {
super(context, attrs);

if (isInEditMode()) AndroidThreeTen.init(context);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//If we're on good Android versions, turn off clipping for cool effects
setClipToPadding(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,140 +10,142 @@
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.format.TitleFormatter;

class TitleChanger {

public static final int DEFAULT_ANIMATION_DELAY = 400;
public static final int DEFAULT_Y_TRANSLATION_DP = 20;

private final TextView title;
@NonNull private TitleFormatter titleFormatter = TitleFormatter.DEFAULT;

private final int animDelay;
private final int animDuration;
private final int translate;
private final Interpolator interpolator = new DecelerateInterpolator(2f);
public static final int DEFAULT_ANIMATION_DELAY = 400;
public static final int DEFAULT_Y_TRANSLATION_DP = 20;

private int orientation = MaterialCalendarView.VERTICAL;
private final TextView title;
@NonNull
private TitleFormatter titleFormatter = TitleFormatter.DEFAULT;

private long lastAnimTime = 0;
private CalendarDay previousMonth = null;
private final int animDelay;
private final int animDuration;
private final int translate;
private final Interpolator interpolator = new DecelerateInterpolator(2f);

public TitleChanger(TextView title) {
this.title = title;
private int orientation = MaterialCalendarView.VERTICAL;

Resources res = title.getResources();
private long lastAnimTime = 0;
private CalendarDay previousMonth = null;

animDelay = DEFAULT_ANIMATION_DELAY;
public TitleChanger(TextView title) {
this.title = title;

animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
Resources res = title.getResources();

translate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}
animDelay = DEFAULT_ANIMATION_DELAY;

public void change(final CalendarDay currentMonth) {
long currentTime = System.currentTimeMillis();
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;

if (currentMonth == null) {
return;
translate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}

if (TextUtils.isEmpty(title.getText()) || (currentTime - lastAnimTime) < animDelay) {
doChange(currentTime, currentMonth, false);
}
public void change(final CalendarDay currentMonth) {
long currentTime = System.currentTimeMillis();

if (currentMonth.equals(previousMonth) ||
(currentMonth.getMonth() == previousMonth.getMonth()
&& currentMonth.getYear() == previousMonth.getYear())) {
return;
}
if (currentMonth == null) {
return;
}

doChange(currentTime, currentMonth, true);
}
if (TextUtils.isEmpty(title.getText()) || (currentTime - lastAnimTime) < animDelay) {
doChange(currentTime, currentMonth, false);
}

private void doChange(final long now, final CalendarDay currentMonth, boolean animate) {
if (previousMonth == null || currentMonth.equals(previousMonth) ||
(currentMonth.getMonth() == previousMonth.getMonth()
&& currentMonth.getYear() == previousMonth.getYear())) {
return;
}

title.animate().cancel();
doTranslation(title, 0);
doChange(currentTime, currentMonth, true);
}

title.setAlpha(1);
lastAnimTime = now;
private void doChange(final long now, final CalendarDay currentMonth, boolean animate) {

final CharSequence newTitle = titleFormatter.format(currentMonth);
title.animate().cancel();
doTranslation(title, 0);

if (!animate) {
title.setText(newTitle);
} else {
final int translation = translate * (previousMonth.isBefore(currentMonth) ? 1 : -1);
final ViewPropertyAnimator viewPropertyAnimator = title.animate();
title.setAlpha(1);
lastAnimTime = now;

if (orientation == MaterialCalendarView.HORIZONTAL) {
viewPropertyAnimator.translationX(translation * -1);
} else {
viewPropertyAnimator.translationY(translation * -1);
}
final CharSequence newTitle = titleFormatter.format(currentMonth);

viewPropertyAnimator
.alpha(0)
.setDuration(animDuration)
.setInterpolator(interpolator)
.setListener(new AnimatorListener() {
if (!animate) {
title.setText(newTitle);
} else {
final int translation = translate * (previousMonth.isBefore(currentMonth) ? 1 : -1);
final ViewPropertyAnimator viewPropertyAnimator = title.animate();

@Override
public void onAnimationCancel(Animator animator) {
doTranslation(title, 0);
title.setAlpha(1);
if (orientation == MaterialCalendarView.HORIZONTAL) {
viewPropertyAnimator.translationX(translation * -1);
} else {
viewPropertyAnimator.translationY(translation * -1);
}

@Override
public void onAnimationEnd(Animator animator) {
title.setText(newTitle);
doTranslation(title, translation);

final ViewPropertyAnimator viewPropertyAnimator = title.animate();
if (orientation == MaterialCalendarView.HORIZONTAL) {
viewPropertyAnimator.translationX(0);
} else {
viewPropertyAnimator.translationY(0);
}

viewPropertyAnimator
.alpha(1)
.setDuration(animDuration)
.setInterpolator(interpolator)
.setListener(new AnimatorListener())
.start();
}
}).start();
viewPropertyAnimator
.alpha(0)
.setDuration(animDuration)
.setInterpolator(interpolator)
.setListener(new AnimatorListener() {

@Override
public void onAnimationCancel(Animator animator) {
doTranslation(title, 0);
title.setAlpha(1);
}

@Override
public void onAnimationEnd(Animator animator) {
title.setText(newTitle);
doTranslation(title, translation);

final ViewPropertyAnimator viewPropertyAnimator = title.animate();
if (orientation == MaterialCalendarView.HORIZONTAL) {
viewPropertyAnimator.translationX(0);
} else {
viewPropertyAnimator.translationY(0);
}

viewPropertyAnimator
.alpha(1)
.setDuration(animDuration)
.setInterpolator(interpolator)
.setListener(new AnimatorListener())
.start();
}
}).start();
}

previousMonth = currentMonth;
}

previousMonth = currentMonth;
}

private void doTranslation(final TextView title, final int translate) {
if (orientation == MaterialCalendarView.HORIZONTAL) {
title.setTranslationX(translate);
} else {
title.setTranslationY(translate);
private void doTranslation(final TextView title, final int translate) {
if (orientation == MaterialCalendarView.HORIZONTAL) {
title.setTranslationX(translate);
} else {
title.setTranslationY(translate);
}
}
}

public void setTitleFormatter(@Nullable final TitleFormatter titleFormatter) {
this.titleFormatter = titleFormatter == null ? TitleFormatter.DEFAULT : titleFormatter;
}
public void setTitleFormatter(@Nullable final TitleFormatter titleFormatter) {
this.titleFormatter = titleFormatter == null ? TitleFormatter.DEFAULT : titleFormatter;
}

public void setOrientation(int orientation) {
this.orientation = orientation;
}
public void setOrientation(int orientation) {
this.orientation = orientation;
}

public int getOrientation() {
return orientation;
}
public int getOrientation() {
return orientation;
}

public void setPreviousMonth(CalendarDay previousMonth) {
this.previousMonth = previousMonth;
}
public void setPreviousMonth(CalendarDay previousMonth) {
this.previousMonth = previousMonth;
}
}