Android CalendarView Drops Weeks at Random

2019-08-08 17:53发布

问题:

I'm using the standard CalendarView in one of my projects. When I display it in a dialog, it seems to work normally, but when I display it in another activity in the app, it seems to leave out a few weeks at random. Scrolling up and down through the calendar causes them to appear again, but others to disappear. Often the missing week is the one including the date that I set it to using setDate() so no selected date is even shown.

Has anyone else seen this behavior with CalendarView? I'm using SDK v17.

    <CalendarView
        android:id="@+id/cv_planner"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="@dimen/item_list_padding"
        android:selectedDateVerticalBar="@color/teal"
        android:selectedWeekBackgroundColor="@color/teal"
        android:showWeekNumber="true" />

回答1:

As a temporary fix, force a refresh by scrolling down/up a month or two, and then scroll back. You should see that the refresh causes the missing week to appear.

Provided this is truly a bug with no workaround, the code involved to compensate for this wouldn't be trivial. I'd imagine it would involve posting a runnable method on your UI thread from the containing activity that would analyze the view after its rendered to check for a missing week, and then forcing a refresh of the view somehow (This would be "easiest" way before getting into sub-classing the CalendarView itself and trying to control its rendering at a finer granularity, say through onMeasure or onLayout).

Update: Found a workaround - CalendarView Issues when Used Directly (Outside of a DatePicker)