如何知道CalendarView中当前显示的月份和年份?

问题描述 投票:0回答:1

我正在开发基于MaterialCalendarView(prolificinteractive)的应用程序,正在用可绘制对象等装饰日期。我想知道用户当前可以看到哪个月份和年份,因此我只能将装饰者应用于当前可见的月份而不是所有月份,因为它会毫无理由地降低应用程序运行速度。注意:我不是在谈论当前选择的日期。

java android android-calendar calendarview
1个回答
0
投票

已经有一个OnMonthChangedListener,但它甚至没有月份参数!如果您不介意反思,可以这样做:

calview.setOnMonthChangedListener((widget, date) -> {
    try {
        Field currentMonthField = MaterialCalendarView.class.getDeclaredField("currentMonth");
        currentMonthField.setAccessible(true);
        int currentMonth = ((CalendarDay) currentMonthField.get(widget)).getMonth();
        // Do something, currentMonth is between 1 and 12.

    } catch (NoSuchFieldException | IllegalAccessException e) {
        // Failed to get field value, maybe library was changed.
    }
});

或者更好的是,打开并发出或发出拉取请求,以使该字段在调用侦听器时传递其值时公开。

© www.soinside.com 2019 - 2024. All rights reserved.