仿小米日历
SuperCalendar
項(xiàng)目地址:MagicMashRoom/SuperCalendar?
簡介:仿小米日歷
SuperCalendar
ChangeLog
- 發(fā)布1.6 穩(wěn)定版
- 發(fā)布1.5 修改了一些BUG
- 發(fā)布1.4穩(wěn)定版
- 清除了dev污染
- 添加手動(dòng)切換上一月下一月功能
- 解決神奇的日歷尺寸問題,確保日歷尺寸正確
簡介
- 可能是第十好的Android開源日歷
- 博主現(xiàn)在工作在一家教育公司,最近公司的產(chǎn)品狗扔過來一個(gè)需求,說要做一個(gè)可以周月切換的課表,可以展示用戶在某一天的上課安排。接到這個(gè)任務(wù)之后我研究了很多的日歷控件,并且抽出了一個(gè)calenderlib。先看一下最后的項(xiàng)目中的效果:
?
- 看到本篇文章的同學(xué)估計(jì)也是實(shí)驗(yàn)課或者項(xiàng)目需求中需要一個(gè)日歷表,當(dāng)我接到這個(gè)需求的時(shí)候,當(dāng)時(shí)腦子壓根連想都沒想,這么通用的控件,GitHub上一搜一大堆不是嘛。可是等到真正做起來的時(shí)候,扎心了老鐵,GitHub上的大神居然異常的不給力,都是實(shí)現(xiàn)了基本功能,能夠滑動(dòng)切換月份,找實(shí)現(xiàn)了周月切換功能的開源庫很難。終于我費(fèi)盡千辛萬苦找到一個(gè)能夠完美切換的項(xiàng)目時(shí),你周月切換之后的數(shù)據(jù)亂的一塌糊涂啊!!!
- 算了,自己擼一個(gè)!!!
項(xiàng)目鏈接?SuperCalendar
- 如果你感覺到對你有幫助,歡迎star
- 如果你感覺對代碼有疑惑,或者需要修改的地方,歡迎issue
主要特性
- 日歷樣式完全自定義,拓展性強(qiáng)
- 左右滑動(dòng)切換上下周月,上下滑動(dòng)切換周月模式
- 抽屜式周月切換效果
- 標(biāo)記指定日期(marker)
- 跳轉(zhuǎn)到指定日期
思路
使用方法
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 兩個(gè)方法
- 新建CustomDayView實(shí)例,并作為參數(shù)構(gòu)建CalendarViewAdapter
初始化View
- 目前來看 相比于Dialog選擇日歷 我的控件更適合于Activity/Fragment在Activity的onCreate?或者Fragment的onCreateView你需要實(shí)現(xiàn)這兩個(gè)方法來啟動(dòng)日歷并裝填進(jìn)數(shù)據(jù)
使用此方法回調(diào)日歷點(diǎn)擊事件
private void initListener() {onSelectDateListener = new OnSelectDateListener() {@Overridepublic void onSelectDate(CalendarDate date) {//your code}@Overridepublic void onSelectOtherMonth(int offset) {//偏移量 -1表示上一個(gè)月 , 1表示下一個(gè)月monthPager.selectOtherMonth(offset);}};}使用此方法初始化日歷標(biāo)記數(shù)據(jù)
private void initMarkData() {HashMap markData = new HashMap<>();//1表示紅點(diǎn),0表示灰點(diǎn)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添加上相關(guān)監(jiān)聽
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.總結(jié)
- 上一篇: ManoMotion⭐二、Unity手势
- 下一篇: HCNE110个知识点