CompactCalendarView 是一個簡單的日曆視圖,提供月份之間的捲動。它基於 Java 的 Date 和 Calendar 類別。它提供了一個簡單的 API 來查詢特定事件的日期和偵聽器。例如,當日曆捲動到新的月份或選擇了某一天。仍在積極開發中。
請提出需求問題,以便在編寫任何程式碼之前進行討論,即使您打算提出拉取請求。請參閱測試設定。
CompactCalendarView 使用 snapshot-tests-for-android (https://github.com/facebook/screenshot-tests-for-android)。這是用於 UI 測試。由於 Screenshot-tests-for-android 會截取螢幕截圖,因此我們需要一種方法來確保可以一致地再現圖像。為此,需要使用特定的模擬器來執行測試。不幸的是,目前使用的是較舊的模擬器。如果可能的話,改變功能的新拉取請求應該旨在建立新的螢幕截圖測試或單元測試。若要在本機執行此命令,請執行以下命令:
先決條件(另請參閱.travis.yml):
使用 Android 19 模擬器是因為它在 travis-ci 上似乎足夠快,而且 travis-ci 不支援 x86 模擬器。更新的 Android 版本是可能的,但建置時間會增加。
安裝 abi 並接受:
$ $ANDROID_HOME /tools/bin/sdkmanager ' system-images;android-22;default;armeabi-v7a '
建立模擬器:
$ echo no | $ANDROID_HOME /tools/bin/avdmanager create avd --force -n testCompactCalendarEmulator -k " system-images;android-22;default;armeabi-v7a "
建立 SD 卡(在目前目錄中建立):SD 卡的任何問題最好透過刪除並重試來解決
$ mksdcard -l sdcard 100M sdcard
運行模擬器(沒有音訊和視窗):
$ $ANDROID_HOME /emulator/emulator -avd testCompactCalendarEmulator -no-audio -no-window -sdcard sdcard &
運行模擬器並觀看(帶有音訊和視窗):
$ $ANDROID_HOME /emulator/emulator -avd testCompactCalendarEmulator -sdcard sdcard
執行測試以驗證目前測試是否通過並檢查哪些測試未產生相同的螢幕截圖:
$ ./gradlew verifyMode screenshotTests
如果新增了新測試,則產生新的螢幕截圖:
$ ./gradlew recordMode screenshotTests
運行單元測試如下:
$ ./gradlew test
可以使用 android studio 模擬器進行測試。但是,它必須是 android 19 和 480x800 螢幕解析度。 Nexus S 模擬器就是一個例子。只需啟動模擬器並執行 gradle 命令即可運行測試。模擬器應該會自動找到。
該庫支援帶或不帶動畫的開啟/關閉。
可以透過一些屬性來變更視圖的外觀。這包括當天的背景顏色、文字顏色、文字大小顏色和當月第一天的顏色。
< com .github.sundeepk.compactcalendarview.CompactCalendarView
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : id = " @+id/compactcalendar_view "
android : layout_width = " fill_parent "
android : paddingRight = " 10dp "
android : paddingLeft = " 10dp "
android : layout_height = " 250dp "
app : compactCalendarTargetHeight = " 250dp "
app : compactCalendarTextSize = " 12sp "
app : compactCalendarBackgroundColor = " #ffe95451 "
app : compactCalendarTextColor = " #fff "
app : compactCalendarCurrentSelectedDayBackgroundColor = " #E57373 "
app : compactCalendarCurrentDayBackgroundColor = " #B71C1C "
app : compactCalendarMultiEventIndicatorColor = " #fff "
/>
請參閱範例應用程式以取得完整範例。
// ... code omitted for brevity
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
final CompactCalendarView compactCalendarView = ( CompactCalendarView ) findViewById ( R . id . compactcalendar_view );
// Set first day of week to Monday, defaults to Monday so calling setFirstDayOfWeek is not necessary
// Use constants provided by Java Calendar class
compactCalendarView . setFirstDayOfWeek ( Calendar . MONDAY );
// Add event 1 on Sun, 07 Jun 2015 18:20:51 GMT
Event ev1 = new Event ( Color . GREEN , 1433701251000L , "Some extra data that I want to store." );
compactCalendar . addEvent ( ev1 );
// Added event 2 GMT: Sun, 07 Jun 2015 19:10:51 GMT
Event ev2 = new Event ( Color . GREEN , 1433704251000L );
compactCalendar . addEvent ( ev2 );
// Query for events on Sun, 07 Jun 2015 GMT.
// Time is not relevant when querying for events, since events are returned by day.
// So you can pass in any arbitary DateTime and you will receive all events for that day.
List < Event > events = compactCalendar . getEvents ( 1433701251000L ); // can also take a Date object
// events has size 2 with the 2 events inserted previously
Log . d ( TAG , "Events: " + events );
// define a listener to receive callbacks when certain events happen.
compactCalendarView . setListener ( new CompactCalendarView . CompactCalendarViewListener () {
@ Override
public void onDayClick ( Date dateClicked ) {
List < Event > events = compactCalendarView . getEvents ( dateClicked );
Log . d ( TAG , "Day was clicked: " + dateClicked + " with events " + events );
}
@ Override
public void onMonthScroll ( Date firstDayOfNewMonth ) {
Log . d ( TAG , "Month was scrolled to: " + firstDayOfNewMonth );
}
});
}
您可以使用預設樣式修改指標,以下是一個範例,但也可以使用其他幾種組合:
請注意,日曆不會嘗試刪除相同日期時間的重複事件。如果這對您的用例很重要,那麼您必須自行處理。
可以設定區域設置,以便日曆自動設定工作日列名稱。
CompactCalendarView compactCalendarView = ( CompactCalendarView ) findViewById ( R . id . compactcalendar_view );
compactCalendarView . setLocale ( Locale . CHINESE );
compactCalendarView . setUseThreeLetterAbbreviation ( true );
dependencies {
compile ' com.github.sundeepk:compact-calendar-view:3.0.0 '
}
The MIT License (MIT)
Copyright (c) [2018] [Sundeepk]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.