riku
2020-09-01 52cab40bb0568b1b1da2035d15158154928c6464
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (C) 2016 huanghaibin_dev <huanghaibin_dev@163.com>
 * WebSite https://github.com/MiracleTimes-Dev
 * 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
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */
package com.haibin.oldcalendarview;
 
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
 
 
import java.util.List;
 
@SuppressWarnings("all")
public class MonthRecyclerView extends RecyclerView {
    private MonthAdapter mAdapter;
    private OnMonthSelectedListener mListener;
 
    public MonthRecyclerView(Context context) {
        this(context, null);
    }
 
    public MonthRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mAdapter = new MonthAdapter(context);
        setLayoutManager(new GridLayoutManager(context, 3));
        setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, long itemId) {
                if (mListener != null) {
                    Month month = mAdapter.getItem(position);
                    mListener.onMonthSelected(month.getYear(), month.getMonth());
                }
            }
        });
    }
 
    void init(int year) {
        java.util.Calendar date = java.util.Calendar.getInstance();
        for (int i = 1; i <= 12; i++) {
            date.set(year, i - 1, 1);
            int firstDayOfWeek = date.get(java.util.Calendar.DAY_OF_WEEK) - 1;//月第一天为星期几,星期天 == 0
            int mDaysCount = Util.getMonthDaysCount(year, i);
            Month month = new Month();
            month.setDiff(firstDayOfWeek);
            month.setCount(mDaysCount);
            month.setMonth(i);
            month.setYear(year);
            mAdapter.addItem(month);
        }
    }
 
    void setSchemes(List<Calendar> mSchemes) {
        mAdapter.setSchemes(mSchemes);
    }
 
    void setSchemeColor(int schemeColor) {
        mAdapter.setSchemeColor(schemeColor);
    }
 
    void setOnMonthSelectedListener(OnMonthSelectedListener listener) {
        this.mListener = listener;
    }
 
    public interface OnMonthSelectedListener {
        void onMonthSelected(int year, int month);
    }
}