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.