自定义考勤统计日历(二)

在说这篇文章之前我们先了解下自定义考勤统计一 的内容,以及他的参考文献,这样就更容易了解这篇文章了。

看下效果图:

分析下效果图:

1.功能是:筛选日期,检索考勤统计列表,点击列表条目进入考勤统计日历,但是有一点要注意:统计列表筛选的开始月份与统计日历要一样,就是说统计列表月份与统计日历月份要联动,一致。

2.点击统计日历进行月份翻页。

3.还有一点:不管是统计列表还是统计日历都是根据筛选的开始日历而定的,当开始日期被选择 点击确定才有效,取消无效;结束日期被选择无效。

这篇文章对pickerview 日历日期选择不做介绍,在后面的文章会单独介绍;只针对翻页日历进行介绍。

看下代码:以统计中 “我的” 为例:

StatisticsAttendanceActivity.java

/**
 * 统计界面
 */
public class StatisticsAttendanceActivity extends BaseActivity {

    private FactoryWaitDialog waitDialog;
    private ImageView iv_back;
    private ImageView iv_date_left;
    private ImageView iv_date_right;
    private TextView tv_title;
    private TextView tv_date_top;
    private CalendarView calendar_view;
    private TextView tv_late;
    private TextView tv_leave_early;
    private TextView tv_lack;
    private TextView tv_off_work;
    private TextView tv_absent;
    private TextView tv_out_work;
    private RecyclerView recycleview;
    private String yearMonth;
    private String yearMonthDay;
    private String queryId;
    private Calendar calendar;
    private String start_timeYm;
    private String start_timeYmd;
    private boolean ischoose_start_calander;
    private Date mchoose_date;


    @Override
    public int getLayoutResId() {
        return R.layout.activity_statistics_attendance;
    }

    @Override
    protected void initView() {
        waitDialog = new FactoryWaitDialog(activity, false, null);
        calendar = Calendar.getInstance();
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_date_left = (ImageView) findViewById(R.id.iv_date_left);
        iv_date_right = (ImageView) findViewById(R.id.iv_date_right);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_date_top = (TextView) findViewById(R.id.tv_date_top);
        calendar_view = (CalendarView) findViewById(R.id.calendar_view);
        tv_late = (TextView) findViewById(R.id.tv_late);
        tv_leave_early = (TextView) findViewById(R.id.tv_leave_early);
        tv_lack = (TextView) findViewById(R.id.tv_lack);
        tv_off_work = (TextView) findViewById(R.id.tv_off_work);
        tv_absent = (TextView) findViewById(R.id.tv_absent);
        tv_out_work = (TextView) findViewById(R.id.tv_out_work);
        recycleview = (RecyclerView) findViewById(R.id.recycleview);
        recycleview.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        iv_date_left.setOnClickListener(this);
        iv_date_right.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("统计");

        Intent intent = getIntent();
        queryId = intent.getStringExtra("id");
        start_timeYm = intent.getStringExtra("start_timeYm");//查询的开始年月
        start_timeYmd = intent.getStringExtra("start_timeYmd");//查询的开始年月日
        ischoose_start_calander = intent.getBooleanExtra("choose_start_calander", false);
        mchoose_date = new Date(intent.getLongExtra("mchoose_date", 0));//查询的Date数据

        if (ischoose_start_calander) {
            yearMonth = start_timeYm;
            yearMonthDay = start_timeYmd;
        } else {
            yearMonth = DateUtils.currentYM();//当前年月
            yearMonthDay = DateUtils.currentYMD();//当前年月日
        }

        tv_date_top.setText(yearMonthDay);

        getMonthAttendance();
    }

    private void getMonthAttendance() {
        waitDialog.show();
        ServiceApi.getMonthAttendance(SpUtils.getUserId(activity), yearMonth, queryId, new MyString2Callback() {
            @Override
            public void onError(Call call, Exception e) {
                waitDialog.dismiss();
                ToastUtils.showServiceDataErrorToast();
            }

            @Override
            public void onResponse(Call call, String s) {
                waitDialog.dismiss();
                CalendarDataBean calendarDataBean = new Gson().fromJson(s, CalendarDataBean.class);
                if (calendarDataBean.code == 0) {
                    List<CalendarDataBean.ListCodeBean> datas = calendarDataBean.listCode;
                    calendar_view.setData(datas, new CalendarAdapter.OnItemChooseListener() {
                        @Override
                        public void onItemChoose(String chooseDay) {

                            waitDialog.show();
                            tv_date_top.setText(chooseDay);
                            getDayAttendanceList(chooseDay);
                        }
                    }, yearMonthDay);

                    tv_late.setText("迟到  :  " + calendarDataBean.countCode.lateAttenCount);
                    tv_leave_early.setText("早退  :  " + calendarDataBean.countCode.earlyAtenCount);
                    tv_lack.setText("缺卡  :  " + calendarDataBean.countCode.singleAttenCount);
                    tv_off_work.setText("请假  :  " + calendarDataBean.countCode.approAttenCount);
                    tv_absent.setText("旷工  :  " + calendarDataBean.countCode.workAttenCount);
                    tv_out_work.setText("外勤  :  " + calendarDataBean.countCode.outAttenCount);

                    getDayAttendanceList(yearMonthDay);
                } else {
                    ToastUtils.showToast(calendarDataBean.msg);
                }
            }
        });
    }

    /**
     * 获取某天的出勤
     *
     * @param ymd
     */
    private void getDayAttendanceList(String ymd) {
        ServiceApi.getDayAttendanceList(SpUtils.getUserId(activity), ymd, queryId, new MyString2Callback() {
            @Override
            public void onError(Call call, Exception e) {
                waitDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, String s) {

                AttendanceDayListBean attendanceDayListBean = new Gson().fromJson(s, AttendanceDayListBean.class);
                if (attendanceDayListBean.code == 0) {
                    AttendanceDayListAdapter attendanceDayListAdapter = new AttendanceDayListAdapter(activity, attendanceDayListBean.approvingList, attendanceDayListBean.attendanceList);
                    recycleview.setAdapter(attendanceDayListAdapter);
                } else {
                    ToastUtils.showToast(attendanceDayListBean.msg);
                }
                waitDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_date_right:
                if (ischoose_start_calander) { //是否查询 ,在查询月份的基础上左右翻页
//                   yearMonth = start_timeYm;  //查询的开始年月
//                   yearMonthDay = start_timeYmd; //查询的开始年月日
//                    mchoose_date                 //查询的Date数据

                    //将Date对象设置给Calendar对象的方法
                    calendar.setTime(mchoose_date);
                    calendar.add(Calendar.MONTH, +1);
                    //getTime()将当前Calendar对象转换为Date对象
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    mchoose_date = DateUtils.Str2DateYM(yearMonth);

                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();

                } else {//在当前月份的基础上左右翻页
                    calendar.add(Calendar.MONTH, +1);
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();
                }

                break;
            case R.id.iv_date_left:
                if (ischoose_start_calander) { //是否查询 ,在查询月份的基础上左右翻页
                    //将Date对象设置给Calendar对象的方法
                    calendar.setTime(mchoose_date);
                    calendar.add(Calendar.MONTH, -1);
                    //getTime()将当前Calendar对象转换为Date对象
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    mchoose_date = DateUtils.Str2DateYM(yearMonth);

                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();

                } else {//在当前月份的基础上左右翻页
                    calendar.add(Calendar.MONTH, -1);
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());

                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();
                }

                break;
        }
    }

    /**
     * 判断不同格式的日期 是否是同一天
     *
     * @param year_month1
     * @param year_month2
     * @return
     */
    private boolean checkDateEquals(String year_month1, String year_month2) {
        String[] ym1 = year_month1.split("-");
        String[] ym2 = year_month2.split("-");
        String year1 = ym1[0];
        String month1 = ym1[1];
        String year2 = ym2[0];
        String month2 = ym2[1];

        if (year1.equals(year2) && month1.equals(month2)) {
            return true;
        } else {
            return false;
        }
    }
}

这里我只介绍效果图的第一点介绍:统计列表筛选的开始月份与统计日历要一样,就是说统计列表月份与统计日历月份要联动,一致。也就是点击列表进入日历月份一致,月份翻页无错误。

那么主要的就是点击拉列表条目模块和左右翻页模块了。

点击列表表条目进入翻页日历:

            case R.id.ll1:
            case R.id.ll2:
                if(ischoose_start_calander){
                    Intent intent = new Intent(getActivity(), StatisticsAttendanceActivity.class);
                    intent.putExtra("id",SpUtils.getUserId(getActivity()));
                    intent.putExtra("start_timeYm",start_timeYm);//年月
                    intent.putExtra("start_timeYmd",start_day);//年月日
                    intent.putExtra("choose_start_calander",ischoose_start_calander);//是否选择日期
                    intent.putExtra("mchoose_date",mchoose_date.getTime());//选择日期时的开始Date
                    getActivity().startActivity(intent);
                }else {
                    Intent intent = new Intent(getActivity(), StatisticsAttendanceActivity.class);
                    intent.putExtra("id",SpUtils.getUserId(getActivity()));
                    intent.putExtra("choose_start_calander",ischoose_start_calander);
                    getActivity().startActivity(intent);
                }

ischoose_start_calander:是否选择日期(筛选查询的日期,开始日期)

start_timeYm:日期年月

start_day:年月日

mchoose_data.getTime() :选择日期时的开始Data.

左右翻页模块:

           case R.id.iv_date_right:
                if (ischoose_start_calander) { //是否查询 ,在查询月份的基础上左右翻页
//                   yearMonth = start_timeYm;  //查询的开始年月
//                   yearMonthDay = start_timeYmd; //查询的开始年月日
//                    mchoose_date                 //查询的Date数据

                    //将Date对象设置给Calendar对象的方法
                    calendar.setTime(mchoose_date);
                    calendar.add(Calendar.MONTH, +1);
                    //getTime()将当前Calendar对象转换为Date对象
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    mchoose_date = DateUtils.Str2DateYM(yearMonth);

                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();

                } else {//在当前月份的基础上左右翻页
                    calendar.add(Calendar.MONTH, +1);
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();
                }

                break;
            case R.id.iv_date_left:
                if (ischoose_start_calander) { //是否查询 ,在查询月份的基础上左右翻页
                    //将Date对象设置给Calendar对象的方法
                    calendar.setTime(mchoose_date);
                    calendar.add(Calendar.MONTH, -1);
                    //getTime()将当前Calendar对象转换为Date对象
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    mchoose_date = DateUtils.Str2DateYM(yearMonth);

                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();

                } else {//在当前月份的基础上左右翻页
                    calendar.add(Calendar.MONTH, -1);
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());

                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();
                }

                break;
        }

如果我们不要求月份的一致,没有查月份的筛选,只有当月的日历,然后进行翻页,我们只需要:

              //在当前月份的基础上左右翻页
                    calendar.add(Calendar.MONTH, +1);
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();
                }

如果要求有列表查询,并且月份一致,主要注意这部分:

              if (ischoose_start_calander) { //是否查询 ,在查询月份的基础上左右翻页
//                   yearMonth = start_timeYm;  //查询的开始年月
//                   yearMonthDay = start_timeYmd; //查询的开始年月日
//                    mchoose_date                 //查询的Date数据

                    //将Date对象设置给Calendar对象的方法
                    calendar.setTime(mchoose_date);
                    calendar.add(Calendar.MONTH, +1);
                    //getTime()将当前Calendar对象转换为Date对象
                    yearMonth = DateUtils.getTimeYM(calendar.getTime());
                    mchoose_date = DateUtils.Str2DateYM(yearMonth);

                    //判断不同格式的日期 是否是同一天
                    if (checkDateEquals(yearMonth, DateUtils.currentYM())) {
                        yearMonthDay = DateUtils.currentYMD();
                        tv_date_top.setText(DateUtils.currentYMD());
                    } else {
                        yearMonthDay = yearMonth + "-01";
                        tv_date_top.setText(yearMonth + "-01");
                    }
                    getMonthAttendance();

                }

看下布局:activity_statistics_attendance.xml

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

    <include layout="@layout/titlebar_blue" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="36dp">

        <ImageView
            android:id="@+id/iv_date_left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/tv_date_top"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@drawable/arrow_blue_left" />

        <TextView
            android:id="@+id/tv_date_top"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_centerInParent="true"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:gravity="center"
            android:text=""
            android:textColor="@color/titlebar_bg_blue"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/iv_date_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/tv_date_top"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@drawable/arrow_blue_right" />
    </RelativeLayout>

    <包名.oa.CalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </包名.oa.CalendarView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_late"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_late_big"
            android:drawablePadding="6dp"
            android:text="迟到:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_leave_early"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_earlyleave_big"
            android:drawablePadding="6dp"
            android:text="早退:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_lack"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_lack_big"
            android:drawablePadding="6dp"
            android:text="缺卡:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_off_work"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_offday_big"
            android:drawablePadding="6dp"
            android:text="请假:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_absent"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_absent_big"
            android:drawablePadding="6dp"
            android:text="旷工:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_out_work"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/p_outwork_big"
            android:drawablePadding="6dp"
            android:text="外勤:  0"
            android:textColor="@color/tv_33"
            android:textSize="13sp" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bg_gray"
        android:paddingBottom="10dp">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

CalendarView.java

/**
 */
public class CalendarView extends LinearLayout {


    private RecyclerView recyclerview;

    public CalendarView(Context context) {
        this(context,null);
    }

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_calendar,null);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        addView(view);
        recyclerview.setLayoutManager(new GridLayoutManager(context,7));
    }

    public void setData(List<CalendarDataBean.ListCodeBean> data,CalendarAdapter.OnItemChooseListener onItemChooseListener,String currentYMD){
        CalendarAdapter calendarAdapter = new CalendarAdapter(getContext(), data,currentYMD);
        calendarAdapter.setOnItemChooseListener(onItemChooseListener);
        recyclerview.setAdapter(calendarAdapter);
        invalidate();
    }

}
view_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/bg_gray"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_height="30dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="日"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="一"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="二"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="三"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="四"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="五"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="六"
            android:gravity="center"
            android:textColor="@color/tv_44"
            android:textSize="14sp"

            />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/bg_gray"/>
    <包名.view.AutoFitRecycleView
        android:id="@+id/recyclerview"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </包名.view.AutoFitRecycleView>
</LinearLayout>

CalendarAdapter.java

public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.Holder> {
    private Context activity;
    private List<CalendarDataBean.ListCodeBean> datas;
    private String currentYMD;
    private String chooseDay;
    private final int firstDayOfWeek;

    public CalendarAdapter(Context activity, List<CalendarDataBean.ListCodeBean> datas,String currentYMD) {
        this.activity = activity;
        this.datas = datas;
        this.currentYMD = currentYMD;
        chooseDay = currentYMD;

        String[] date = currentYMD.split("-");
        int year = Integer.parseInt(date[0]);
        int month = Integer.parseInt(date[1]);
        //先得出 第"1"天是周几
        firstDayOfWeek = DateUtils.getFirstDayWeek(year, month, 1);


    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_calendar, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        if (position >= firstDayOfWeek) {
            if ("1".equals(datas.get(position - firstDayOfWeek).timeStatus)) {
                //当天
                if ((DateUtils.currentYMD()).equals(datas.get(position - firstDayOfWeek).day)) {
                    holder.iv_bg_day.setVisibility(View.VISIBLE);
                } else {
                    holder.iv_bg_day.setVisibility(View.INVISIBLE);
                }

                //是否选中
                if (chooseDay.equals(datas.get(position - firstDayOfWeek).day)){
                    holder.iv_bg.setImageResource(R.drawable.bg_choose);
                    holder.iv_bg.setVisibility(View.VISIBLE);
                }else {
                    //是否签到
                    if ("1".equals(datas.get(position - firstDayOfWeek).status)) {
                        holder.iv_bg.setImageResource(R.drawable.bg_sign);
                        holder.iv_bg.setVisibility(View.VISIBLE);
                    } else {
                        holder.iv_bg.setVisibility(View.INVISIBLE);
                    }

                }

                //是否早退
                if ("1".equals(datas.get(position - firstDayOfWeek).earlyStatus)) {
                    holder.fv_flags.setLeaveEarly(true);
                } else {
                    holder.fv_flags.setLeaveEarly(false);
                }
                //是否缺卡
                if ("1".equals(datas.get(position - firstDayOfWeek).singleStatus)) {
                    holder.fv_flags.setLack(true);
                } else {
                    holder.fv_flags.setLack(false);
                }
                //是否旷工
                if ("1".equals(datas.get(position - firstDayOfWeek).workStatus)) {
                    holder.fv_flags.setAbsent(true);
                } else {
                    holder.fv_flags.setAbsent(false);
                }

                //是否请假
                if ("1".equals(datas.get(position - firstDayOfWeek).approvingStatus)) {
                    holder.fv_flags.setDayOff(true);
                } else {
                    holder.fv_flags.setDayOff(false);
                }
                //是否迟到
                if ("1".equals(datas.get(position - firstDayOfWeek).lateStatus)) {
                    holder.fv_flags.setLate(true);
                } else {
                    holder.fv_flags.setLate(false);
                }
                //是否外勤
                if ("1".equals(datas.get(position - firstDayOfWeek).outStatus)) {
                    holder.fv_flags.setOutWork(true);
                } else {
                    holder.fv_flags.setOutWork(false);
                }

                holder.rl_item.setClickable(true);
                holder.rl_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chooseDay = datas.get(position-firstDayOfWeek).day;
                        notifyDataSetChanged();
                        if (onItemChooseListener!=null) {
                            onItemChooseListener.onItemChoose(chooseDay);
                        }
                    }
                });
            } else {
                holder.rl_item.setClickable(false);
            }
            holder.tv_num.setText((position - firstDayOfWeek + 1) + "");
        }else {
            holder.tv_num.setText("");
            holder.fv_flags.clear();
            holder.iv_bg.setVisibility(View.INVISIBLE);
            holder.iv_bg_day.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() + firstDayOfWeek;
    }

    public class Holder extends RecyclerView.ViewHolder {

        private final RelativeLayout rl_item;
        private final ImageView iv_bg;
        private final ImageView iv_bg_day;
        private final TextView tv_num;
        private final FlagsView fv_flags;

        public Holder(View itemView) {
            super(itemView);
            rl_item = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            iv_bg = (ImageView) itemView.findViewById(R.id.iv_bg);
            iv_bg_day = (ImageView) itemView.findViewById(R.id.iv_bg_day);
            tv_num = (TextView) itemView.findViewById(R.id.tv_num);
            fv_flags = (FlagsView) itemView.findViewById(R.id.fv_flags);
        }
    }

    public interface OnItemChooseListener{
        void onItemChoose(String chooseDay);
    }

    private OnItemChooseListener onItemChooseListener;

    public void setOnItemChooseListener(OnItemChooseListener onItemChooseListener) {
        this.onItemChooseListener = onItemChooseListener;
    }
}

item_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:id="@+id/rl_item"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/iv_bg"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/bg_sign"
            android:scaleType="center"
            android:visibility="invisible"
            />
        <ImageView
            android:id="@+id/iv_bg_day"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/circle_day"
            android:visibility="invisible"
            android:scaleType="center"
            />
        <TextView
            android:id="@+id/tv_num"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:gravity="center"
            android:textColor="#000000"
            android:textSize="12sp"
            android:layout_centerHorizontal="true"
            android:text="12"
            />
        <包名.oa.FlagsView
            android:id="@+id/fv_flags"
            android:layout_width="wrap_content"
            android:layout_height="10dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/iv_bg"
            android:layout_marginTop="2dp"
            />
    </RelativeLayout>


</RelativeLayout>

DateUtils.java
public class DateUtils {
    /**
     * 通过年份和月份 得到当月的日子个数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
//        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }

    /**
     * 返回当前月份几号位于周几
     *
     * @param year  年份
     * @param month 月份,传入系统获取的,不需要正常的
     * @return 日:0	一:1		二:2		三:3		四:4		五:5		六:6
     */
    public static int getFirstDayWeek(int year, int month,int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month-1, day);

        return calendar.get(Calendar.DAY_OF_WEEK)-1;
    }

    /**
     * 根据列明获取周
     *
     * @param
     * @return
     */
    public static String getWeekName(int year, int month,int day) {
        int column = getFirstDayWeek(year, month, day);
        switch (column) {
            case 0:
                return "周日";
            case 1:
                return "周一";
            case 2:
                return "周二";
            case 3:
                return "周三";
            case 4:
                return "周四";
            case 5:
                return "周五";
            case 6:
                return "周六";
            default:
                return "";
        }
    }

    /**
     * 得到日期字符串
     * @param date
     * @return  年月日时分秒
     */
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    /**
     * 得到日期字符串
     * @param date
     * @return  年月日
     */
    public static String getTimeYMD(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 得到日期字符串
     * @param date 年月
     * @return
     */
    public static String getTimeYM(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    /**
     * 得到日期字符串
     * @param date 年
     * @return
     */
    public static String getTimeY(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(date);
    }

    /**
     * 获取当前年月日字符串
     * @return
     */
    public static String currentYMD(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date());
    }

    /**
     * 获取当前年月日字符串
     * @return
     */
    public static String currentYMD2(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d");
        return format.format(new Date());
    }

    /**
     * 获取当前年月字符串
     *
     * @return
     */
    public static String currentYM(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(new Date());
    }

    /**
     * 获取当前年月字符串
     *
     * @return
     */
    public static String currentYM2(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M");
        return format.format(new Date());
    }

    /**
     * 通过日期字符串获取日期对象
     * @param dateStr
     * @return
     */
    public static Date Str2DateYMD(String dateStr)  {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date ;
        try{
           date = format.parse(dateStr);
        }catch (Exception e){
            Log.d("DateUtils.Str2DateYMD()","Str2DateYMD() returned: " + e.toString() );
            date = new Date();
        }

        return date;
    }

    public static Date Str2DateYM(String dateStr){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date ;
        try{
            date = format.parse(dateStr);
        }catch (Exception e){
            Log.d("DateUtils.Str2DateYMD()","Str2DateYMD() returned: " + e.toString() );
            date = new Date();
        }

        return date;
    }


    /**
     * 获取当前所在月份
     * @return
     */
    public static int getToday_MONTH(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH);
    }

    /**
     * 获取当前所在天
     * @return
     */
    public static int getToday_DAY(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值