仿小米日历
SuperCalendar
項目地址:MagicMashRoom/SuperCalendar?
簡介:仿小米日歷
SuperCalendar
ChangeLog
- 發布1.6 穩定版
- 發布1.5 修改了一些BUG
- 發布1.4穩定版
- 清除了dev污染
- 添加手動切換上一月下一月功能
- 解決神奇的日歷尺寸問題,確保日歷尺寸正確
簡介
- 可能是第十好的Android開源日歷
- 博主現在工作在一家教育公司,最近公司的產品狗扔過來一個需求,說要做一個可以周月切換的課表,可以展示用戶在某一天的上課安排。接到這個任務之后我研究了很多的日歷控件,并且抽出了一個calenderlib。先看一下最后的項目中的效果:
?
- 看到本篇文章的同學估計也是實驗課或者項目需求中需要一個日歷表,當我接到這個需求的時候,當時腦子壓根連想都沒想,這么通用的控件,GitHub上一搜一大堆不是嘛。可是等到真正做起來的時候,扎心了老鐵,GitHub上的大神居然異常的不給力,都是實現了基本功能,能夠滑動切換月份,找實現了周月切換功能的開源庫很難。終于我費盡千辛萬苦找到一個能夠完美切換的項目時,你周月切換之后的數據亂的一塌糊涂啊!!!
- 算了,自己擼一個!!!
項目鏈接?SuperCalendar
- 如果你感覺到對你有幫助,歡迎star
- 如果你感覺對代碼有疑惑,或者需要修改的地方,歡迎issue
主要特性
- 日歷樣式完全自定義,拓展性強
- 左右滑動切換上下周月,上下滑動切換周月模式
- 抽屜式周月切換效果
- 標記指定日期(marker)
- 跳轉到指定日期
思路
使用方法
XML布局
- 新建XML布局
RecyclerView的layout_behavior為com.ldf.calendar.behavior.RecyclerViewBehavior
<android.support.design.widget.CoordinatorLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"><com.ldf.calendar.view.MonthPagerandroid:id="@+id/calendar_view"android:layout_width="match_parent"android:layout_height="300dp"android:background="#fff"></com.ldf.calendar.view.MonthPager><android.support.v7.widget.RecyclerViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_behavior="com.ldf.calendar.behavior.RecyclerViewBehavior"android:background="#c2c2c2"android:layout_gravity="bottom"/></android.support.design.widget.CoordinatorLayout>自定義日歷樣式
- 新建CustomDayView繼承自DayView并重寫refreshContent 和 copy 兩個方法
- 新建CustomDayView實例,并作為參數構建CalendarViewAdapter
初始化View
- 目前來看 相比于Dialog選擇日歷 我的控件更適合于Activity/Fragment在Activity的onCreate?或者Fragment的onCreateView你需要實現這兩個方法來啟動日歷并裝填進數據
使用此方法回調日歷點擊事件
private void initListener() {onSelectDateListener = new OnSelectDateListener() {@Overridepublic void onSelectDate(CalendarDate date) {//your code}@Overridepublic void onSelectOtherMonth(int offset) {//偏移量 -1表示上一個月 , 1表示下一個月monthPager.selectOtherMonth(offset);}};}使用此方法初始化日歷標記數據
private void initMarkData() {HashMap markData = new HashMap<>();//1表示紅點,0表示灰點markData.put("2017-8-9" , "1");markData.put("2017-7-9" , "0");markData.put("2017-6-9" , "1");markData.put("2017-6-10" , "0");calendarAdapter.setMarkData(markData);}使用此方法給MonthPager添加上相關監聽
monthPager.addOnPageChangeListener(new MonthPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {mCurrentPage = position;currentCalendars = calendarAdapter.getAllItems();if(currentCalendars.get(position % currentCalendars.size()) instanceof Calendar){//you code}}@Overridepublic void onPageScrollStateChanged(int state) {}});重寫onWindowFocusChanged方法,使用此方法得知calendar和day的尺寸
@Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);if(hasFocus && !initiated) {CalendarDate today = new CalendarDate();calendarAdapter.notifyDataChanged(today);initiated = true;}}- 大功告成,如果還不清晰,請下載DEMO
Download
Gradle: Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {repositories {...maven { url 'https://www.jitpack.io' }} }Step 2. Add the dependency
dependencies {compile 'com.github.MagicMashRoom:SuperCalendar:1.6'}Licence
Copyright 2017 MagicMashRoom, Inc.總結
- 上一篇: ManoMotion⭐二、Unity手势
- 下一篇: HCNE110个知识点