RecycleView实现Android自定义日历

 移动端有自带的控件  其实并不需要自己写 既然写了 就留个纪念吧  主要是在iptv上使用

RecycleView实现Android自定义日历_第1张图片

分析:第一行是LinearLayout包含一个左箭头 两个TextView显示年和月 一个右箭头 其中左右箭头可以点击

第二行可以用RecycleView 其中GridLayoutManager的spanCount设置为7  把星期填上

第三行开始是一个RecycleView 与星期对齐

首先 我默认的是显示当前月份的日历 判断本月的周一是星期几

/**
     * 功能:返回星期 1:星期一,2:星期二 ... 6:星期六 0:星期日
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = c.get(Calendar.DAY_OF_WEEK) - 1;
        return week;
    }

判断一个月有多少天 主要是判断二月份

    public static int judgeDay(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, 2, 1);// year年的3月1日
        c.add(Calendar.DAY_OF_MONTH, -1);//将3月1日往左偏移一天结果是2月的天数
        return c.get(Calendar.DAY_OF_MONTH);
    }
        int dayNum = 31;
        List longList = Arrays.asList(1, 3, 5, 7, 8, 10, 12);
        List shortList = Arrays.asList(4, 6, 9, 11);
        if (longList.contains(monthNew)) {
            dayNum = 31;
        } else if (shortList.contains(monthNew)) {
            dayNum = 30;
        } else {
            dayNum = DateUtils.judgeDay(yearNew) + 1;
        }

数据写入 因为第一行有空格 空格数我默认用0补齐 然后设置成INVISIBLE

因为设置的是左右显示的 所以在加载新的月份的时候 把上个月的list清空

        mDay.clear();
        for (int j = 0; j < week; j++) {
            mDay.add(0);
        }
        for (int i = 1; i <= dayNum; i++) {
            mDay.add(i);
        }
        mCalendarAdapter.notifyDataSetChanged();

因为后面要用 所以我再写一下完整的方法

public void onChangeData(int choiceYear, int choiceMonth) {
        Date date = DateUtils.parseDate(choiceYear + "-" + choiceMonth + "-01");
        int week = DateUtils.getWeek(date);
        int dayNum = 31;
        List longList = Arrays.asList(1, 3, 5, 7, 8, 10, 12);
        List shortList = Arrays.asList(4, 6, 9, 11);
        if (longList.contains(monthNew)) {
            dayNum = 31;
        } else if (shortList.contains(monthNew)) {
            dayNum = 30;
        } else {
            dayNum = DateUtils.judgeDay(yearNew) + 1;
        }
        mDay.clear();
        for (int j = 0; j < week; j++) {
            mDay.add(0);
        }
        for (int i = 1; i <= dayNum; i++) {
            mDay.add(i);
        }
        mCalendarAdapter.notifyDataSetChanged();
    }

左右按钮的点击事件:

            case R.id.iv_left_icon:
                int choiceYearL = Integer.parseInt(tvChoiceYear.getText().toString());
                int choiceMonthL = Integer.parseInt(tvChoiceMonth.getText().toString());
                choiceMonthL--;
                if (choiceYearL == yearNew) {
                    if (choiceMonthL < monthNew) {
                        showToast("选择日期不能早于今天");
                    } else {
                        tvChoiceMonth.setText(choiceMonthL + "");
                        onChangeData(choiceYearL, choiceMonthL);
                    }
                } else {
                    if (choiceMonthL == 0) {
                        choiceMonthL = 12;
                        choiceYearL--;
                    }
                    tvChoiceMonth.setText(choiceMonthL + "");
                    onChangeData(choiceYearL, choiceMonthL);
                }
                break;
            case R.id.iv_right_icon:
                int choiceYear = Integer.parseInt(tvChoiceYear.getText().toString());
                int choiceMonth = Integer.parseInt(tvChoiceMonth.getText().toString());
                choiceMonth++;
                if (choiceMonth > 12) {
                    choiceMonth = 1;
                    choiceYear = choiceYear + 1;
                }
                tvChoiceMonth.setText(choiceMonth + "");
                tvChoiceYear.setText(choiceYear + "");
                onChangeData(choiceYear, choiceMonth);
                break;

 

你可能感兴趣的:(Android开发)