Is there any way to detect month change in android calendar view(i.e. when user changes calendar to another month)

偶尔善良 提交于 2019-12-03 12:04:50

问题


I want to get month shown in calendar view and i cant figure it out. i tried

calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {
            int d = dayOfMonth;
            int m = month;
            Log.d("month", "" + m);
            String curDate = String.valueOf(d);

        }
});

but it only triggers when user click to the days of month. What i want is when user changes the month i want the changed month(i.e. the month shown in calendar view.) Any help would be appreciated. Thanks in advance.


回答1:


I think from Android's default CalendarView you can't achieve what you want. For this you need to implement your custom calendar or any other library which provides this implementation or provides listeners or gestures for swiping between months. Refer this link if it helps you: Material-CalendeView

And this is the sample code:

 MaterialCalendarView calendarView = new MaterialCalendarView(getActivity());
 calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
     @Override
     public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
            //Do something like this
            Date date1 = date.getDate();
     }
 });

Hope it helps you.



来源:https://stackoverflow.com/questions/45752408/is-there-any-way-to-detect-month-change-in-android-calendar-viewi-e-when-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!