Closed harikiran-01 closed 3 years ago
@Ro I want to disable scroll only in one direction. Like a max date and min date and preventing scrolling beyond those dates
@harikiran-01 , try using this when condition is true
compactCalendarView.shouldScrollMonth(false)
@harikiran-01 what you can do in that case is when you know you don't want user to scroll any further,
compactCalendarView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY -> //Your code }
compactCalendarView.scrollLeft()
or compactCalendarView.scrollRight()
This will give a better user experience and hopefully solves your problem.
@harikiran-01 what you can do in that case is when you know you don't want user to scroll any further,
- Let user scroll
- get event in listener and check condition
compactCalendarView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY -> //Your code }
- if doesn't match scroll back along with a toast or snackbar message scrollBack :
compactCalendarView.scrollLeft()
orcompactCalendarView.scrollRight()
This will give a better user experience and hopefully solves your problem.
Yeah this workaround is legit, but wondered if there is any implementation to prevent loading the max or min month altogether. Like restricting the onScroll event even if the swipe is detected which would make the ui more friendly, imo
Can you share your code please? I was be able to add a min and max with left and right button but my problem is apply it with the scroll, I don't know how to do it. I would like to apply it when user exceed the maximum or minimum date.
You need to Override the onMonthScroll function available in CompactCalendarView.CompactCalendarViewListener interface to detect the scroll and perform required operations. Here is the snippet of code I'm using to prevent scroll after a min date and max date:
public class CalendarActivity extends AppCompatActivity implements
CompactCalendarView.CompactCalendarViewListener
{
private CompactCalendarView compactCalendarView;
Date lastselecteddate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compactCalendarView = findViewById(R.id.compactcalendar_view);//replace with your view id
compactCalendarView.setListener(this);
}
@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
if(firstDayOfNewMonth.after(maxDate) || firstDayOfNewMonth.before(minDate)){
compactCalendarView.setCurrentDate(lastselecteddate);
//this resets calendar to the previous selected month
}
else{
lastselecteddate = firstDayOfNewMonth;
}
}
}
@rensocs
workaround found
@harikiran-01 , try using this when condition is true
compactCalendarView.shouldScrollMonth(false)