SundeepK / CompactCalendarView

An android library which provides a compact calendar view much like the one used in google calenders.
MIT License
1.52k stars 428 forks source link

any way to disable scrolling for month when certain date conditions are met #334

Closed harikiran-01 closed 3 years ago

Root-vb commented 5 years ago

@harikiran-01 , try using this when condition is true compactCalendarView.shouldScrollMonth(false)

harikiran-01 commented 5 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)

Root-vb commented 5 years ago

@harikiran-01 what you can do in that case is when you know you don't want user to scroll any further,

  1. Let user scroll
  2. get event in listener and check condition compactCalendarView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY -> //Your code }
  3. if doesn't match scroll back along with a toast or snackbar message scrollBack : compactCalendarView.scrollLeft() or compactCalendarView.scrollRight()

This will give a better user experience and hopefully solves your problem.

harikiran-01 commented 4 years ago

@harikiran-01 what you can do in that case is when you know you don't want user to scroll any further,

  1. Let user scroll
  2. get event in listener and check condition compactCalendarView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY -> //Your code }
  3. if doesn't match scroll back along with a toast or snackbar message scrollBack : compactCalendarView.scrollLeft() or compactCalendarView.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

rensocontreras commented 4 years ago

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.

harikiran-01 commented 4 years ago

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

harikiran-01 commented 3 years ago

workaround found