android 的日历布局,GitHub - flyfire/HoVerticalCalendarView: Android 垂直滑动的日历视图

HoVerticalCalendarView

Author : hosle

Created in 14th June 2018

Feature

支持连续月份的上下自由滚动

支持每天日期下添加任务计数标示

每天日期支持设置点击事件,返回点击的Calendar类实例

滚动时,月份标签顶部冻结

figure_demo.gif

Usage

1.1. 添加日历视图到布局文件

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.hosle.vertical_calendar.demo.MainActivity">

android:id="@+id/calendar_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

1.2. 设置日历参数

在Activity 的fun onCreate()方法中,设置日历参数。

(1)配置日历的月份范围,需要传入一个指代年份和月份值的二维数组

(2)定义并传入日期点击事件的回调

(3)定义并传入获取每天任务事件数的方法参数

calendar_view.setCalendarParams(createMonth(4)/** set the month arange **/,

object : MonthView.OnDayClickListener {

override fun onDayClick(view: MonthView, day: Calendar) {

// callback on click to the specific day

}

},

{ calendar ->

run {

// interface to set the task count to each day

}

})

Key Point

2.1. 绘制月份View

整体框架

实现垂直的连续滚动,可以使用RecyclerView。每个ItemView为一个月份的View。

以月份为子View

月份的View若使用如LinearLayout来布局,最小单元为天,那么一个月的ItemView至少将会包含28个子View。这样的方案需要重复多次View的“measure-layout-draw”的流程,效率较低。

参考android.widget.SimpleMonthView的设计思想,自定义view。

提供任务标识数据读取接口

从日历的Map中获取任务数,绘制标识到对应的日期底部

2.2. 月份标签顶部冻结及动效

手势事件

(1) 假设冻结月份栏的layout位置左、上、右、下分别为:lleft, ltop, lright, lbottom ,初始位置则为 lleft0, ltop0, lright0, lbottom0, 月份栏的高度为h, 滚动回调onScrollListener返回的位移量分别为dx, dy。

(2) 利用LinearLayoutManager的findFirstVisibleItemPosition()方法,判断RecyclerView内,上/下一个月份ItemView的切换

向下滑动时,冻结栏从 (lleft0, ltop0-h, lright0, lbottom0-h) 开始向下移动

向上滑动时,冻结栏从(lleft0, ltop0, lright0, lbottom)开始向上移动

(3) 以第二个可见月份子View的顶部,作为分界线line。到达冻结月份栏高度h的时候,触发冻结栏位置的动效变化。设定最小值以保证月份栏动效不滑出边界。

* line的位置top小于h时,冻结栏的位置为(lleft0, min(ltop-dy,ltop0), lright0, min(lbottom-dy, lbottom0))。

* line的位置top大于等于h时,冻结栏的位置为默认位置(lleft0, ltop0, lright0, lbottom0)。

Extension

A horizontal calendar view which is based ViewPager

License

Copyright (C) 2018. Henry Tam (hosle)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值