Java - Android开发之日历篇(2)

上次说到日历开发,但是并没有对接下来的实现做出完整的代码,而是给你们推荐的是15年写的代码,实属抱歉,现在分享一下最新的代码,结合的是上一篇中的kotlin语言编写的相关方法开发的,先看一下截图:

日历截图

进入正题
组件界面编写,我们先分析界面布局,从图像上看,最适用的当然是GridView控件,因此我们有如下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#efefef"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f0f0f0" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:gravity="center"
        android:padding="10dp"
        android:textSize="16sp"
        android:textStyle="bold"
        tools:text="2017-05    Today is 2017-05-16" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f3f3f3"
        android:divider="@drawable/shape_line_lightgray_solid"
        android:dividerHeight="2dp"
        android:showDividers="middle"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="日" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="一" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="二" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="三" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="四" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="五" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="六" />
    </LinearLayout>

    <com.mrper.framework.component.widget.nowrapview.NoWrapGridView
        android:id="@+id/gvCalendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f0f0"
        android:horizontalSpacing="1dp"
        android:numColumns="7"
        android:verticalSpacing="1dp"
        tools:layout_height="180dp"
        tools:listitem="@layout/listitem_calendar_view" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f0f0f0" />

</LinearLayout>
UI预览效果

PS: 上面涉及到一个NoWrapGridView的实现,它的作用是为了保证在ScrollView中不产生滑动排斥,实现如下:

/**
 * Created by mrper on 17-3-17.
 */
class NoWrapGridView : GridView {

    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Int.MAX_VALUE.shr(2), MeasureSpec.AT_MOST))
    }

}

当然,少不了我们的ItemView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtDate"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_circle_red_solid"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="14sp"
        tools:text="30" />

</FrameLayout>

基本界面已经搭建完成,接下来就是编写我们的Adapter组件,当然编写之前我们需要对上一篇中提到的获取日期在日历中的数据进行调整,如下:

    /**
     * 计算日历中的日期数据(结果总数据长度恒定为42)
     * @param year 年份
     * @param month 月份
     * @return 结果返回日历日期的相关信息,MutableMap<Int, Int>  --- First: 日期,  Second: -1 - 上个月的,0 - 这个月的,1 - 下个月的
     */
    @JvmStatic
    fun computeDatesInCalendar(year: Int, month: Int): MutableList<MutableMap<String, Any>> {
        val dates: MutableList<MutableMap<String, Any>> = mutableListOf()
        val monthDayCount = computeMonthDayCount(year, month) //计算出当前这个月有多少天
        val preMonthDayNum = computeWeekNum(year, month, 1) //先获得该日期下是周几?然后计算出上个月有几天要在日期中显示
        val preMonthDayCount = computeMonthDayCount(if (month == 1) year - 1 else year, if (month == 1) 12 else month - 1)
        val nextMonthDayNum = 42 - preMonthDayNum - monthDayCount
        IntRange(0, preMonthDayNum - 1).forEach {
            val item: MutableMap<String, Any> = mutableMapOf()
            item.put("date", preMonthDayCount - (preMonthDayNum - 1 - it))
            item.put("month", month)
            item.put("year", year)
            item.put("property", -1)
            dates.add(item)
        }
        IntRange(0, monthDayCount - 1).forEach {
            val item: MutableMap<String, Any> = mutableMapOf()
            item.put("date", it + 1)
            item.put("month", month)
            item.put("year", year)
            item.put("property", 0)
            dates.add(item)
        }
        IntRange(0, nextMonthDayNum - 1).forEach {
            val item: MutableMap<String, Any> = mutableMapOf()
            item.put("date", it + 1)
            item.put("month", month)
            item.put("year", year)
            item.put("property", 1)
            dates.add(item)
        }
        return dates
    }

接下来再看如何实现Adapter, BaseLIstAdapter的实现可以参考 Kotlin - 贡献两个Android的Adapter类,结合DataBinding和常规的Adapter

/**
 * Created by Jbtm on 2017/5/16.
 * 日历数据适配器
 */
public class CalendarAdapter extends BaseListAdapter<Map<String, Object>> {

    public CalendarAdapter(@NotNull Context context, @Nullable List<Map<String, Object>> dataSource) {
        super(context, R.layout.listitem_calendar_view, dataSource);
    }

    @Override
    public void bindValues(@NotNull ViewHolder holder, int position, @NotNull Map<String, Object> itemData) {
        String dateStr = itemData.get("date").toString();
        holder.setText(R.id.txtDate, dateStr);
        TextView txtDate = holder.getViewById(R.id.txtDate);
        final boolean isToday = DateUtil.isToday(Integer.valueOf(itemData.get("year").toString()), Integer.valueOf(itemData.get("month").toString()), Integer.valueOf(dateStr));
        final int row = position / 7, column = position - row * 7;
        final boolean isWeekday = column == 0 || column == 6;
        switch (Integer.valueOf(itemData.get("property").toString())) {
            case 0: //当月的
                if (isToday) {
                    txtDate.setTextColor(Color.WHITE);
                    txtDate.setBackgroundResource(R.drawable.shape_circle_red_solid);
                } else if (isWeekday) {
                    txtDate.setTextColor(Color.RED);
                    txtDate.setBackgroundResource(0);
                } else {
                    txtDate.setTextColor(Color.GRAY);
                    txtDate.setBackgroundResource(0);
                }
                break;
            case -1: //上一个月的
            case 1: //下一个月的
                txtDate.setTextColor(isWeekday ? 0xffa00000 : Color.LTGRAY);
                txtDate.setBackgroundResource(0);
                break;
        }
    }
}

现在我们需要使用界面,开始组合控件,并实现,如下:

/**
 * Created by Jbtm on 2017/5/16.
 * 日历视图控件
 */
public class CalendarView extends FrameLayout {

    private Context context;
    private TextView txtTitle;
    private NoWrapGridView gvCalendar;

    public CalendarView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        View contentView = View.inflate(context, R.layout.widget_calendar_view, null);
        txtTitle = (TextView) contentView.findViewById(R.id.txtTitle);
        gvCalendar = (NoWrapGridView) contentView.findViewById(R.id.gvCalendar);
        setCalendarAdapter(DateTime.now().getYear(), DateTime.now().getMonthOfYear());
        addView(contentView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        if (attrs != null) {

        }
    }

    /**
     * 设置日历数据适配器
     *
     * @param year  年份
     * @param month 月份
     */
    private void setCalendarAdapter(int year, int month) {
        txtTitle.setText(String.format(Locale.CHINESE, "%d年%d月  今天是%d月%d日", year, month, DateTime.now().getMonthOfYear(), DateTime.now().getDayOfMonth()));
        List<Map<String, Object>> daysOfCalendar = CalendarUtil.computeDatesInCalendar(year, month);
        gvCalendar.setAdapter(new CalendarAdapter(context, daysOfCalendar));
    }

}

到此位置,日历UI就开发完整了,当然你可以根据你的需要进行更个性化的修改和优化。希望有需要的朋友也能帮你解决一些开发中遇到的问题,谢谢观赏。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,043评论 25 707
  • Google在今年的IO大会上宣布,将Android开发的官方语言更换为Kotlin,作为跟着Google玩儿An...
    蓝灰_q阅读 76,710评论 31 490
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,943评论 4 60
  • 全程抖,顺其自然。今早起来有股莫名怒火,发到了孩子身上,她磨蹭还有昨晚提醒她准备的东西今早又没有准备好,火蹭的就上...
    安心的心灵花园阅读 187评论 0 0
  • 今天周末,好不容易,抽了点时间,读了一下《巨婴国》,有点小感受,与大家分享。 苏格拉底说“认识你自己”但很多时候,...
    周周有料阅读 243评论 0 0