UI

Android自定义签到日历

2021-07-27  本文已影响0人  itfitness

目录

效果展示

使用的三方库

https://github.com/myjoybar/android-calendar-view

关键修改

原代码做的非常的简洁,非常适合咱们通过自定义来获取自己的效果,比如我们想做一个展示签到信息的日历,只需要在原加载日历的代码后加入我们的签到信息即可
CalendarViewFragment:

/**
     * 加载签到数据
     * @param mListDataCalendar
     */
    private void initSigninData(List<CalendarDate> mListDataCalendar) {
        
        //在加载日历数据之前加载签到数据(或者是先加载日历数据然后再加载签到数据,加载完签到数据之后刷新数据即可)
        ArrayList<String> signinDatas = new ArrayList<>();
        Random random = new Random();
        //生成签到数据(这里是根据接口获取的)
        for(int i = 0 ; i < 10 ; i ++){
            signinDatas.add(mYear +"-" + mMonth + "-" + random.nextInt(28));
        }

        //将签到数据加到日历中
        for (CalendarDate calendarDate : mListDataCalendar) {
            if(calendarDate != null && calendarDate.getSolar() != null){
                for (String signinData : signinDatas) {
                    if(!TextUtils.isEmpty(signinData)
                            && signinData.equals(calendarDate.getSolar().solarYear + "-" + calendarDate.getSolar().solarMonth + "-" + calendarDate.getSolar().solarDay)){
                        calendarDate.setSignin(true);
                        break;
                    }
                }
            }
        }



        mGridView.setAdapter(new CalendarGridViewAdapter(mListDataCalendar));
        final List<CalendarDate> finalMListDataCalendar = mListDataCalendar;
        if (isChoiceModelSingle) {
            mGridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
        } else {
            mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
        }
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CalendarDate calendarDate = ((CalendarGridViewAdapter) mGridView.getAdapter()).getListData().get(position);
                if (isChoiceModelSingle) {
                    //单选
                    if (finalMListDataCalendar.get(position).isInThisMonth()) {
                        onDateClickListener.onDateClick(calendarDate);
                    } else {
//                        clearStatus();

//                        mGridView.setItemChecked(position, false);
                    }
                    clearStatus();
                    setItemSelect(position,true);
                } else {
                    //多选
                    if (finalMListDataCalendar.get(position).isInThisMonth()) {
                        // mGridView.getCheckedItemIds()
//                        if(!mGridView.isItemChecked(position)){
//                            onDateCancelListener.onDateCancel(calendarDate);
//                        } else {
//                            onDateClickListener.onDateClick(calendarDate);
//                        }
                        if(calendarDate.isSelect()){
                            onDateCancelListener.onDateCancel(calendarDate);
                        } else {
                            onDateClickListener.onDateClick(calendarDate);
                        }
                        setItemSelect(position,!calendarDate.isSelect());
                    } else {
                        setItemSelect(position,!calendarDate.isSelect());
//                        mGridView.setItemChecked(position, false);
                    }

                }
            }
        });
        mGridView.post(new Runnable() {
            @Override
            public void run() {
                //需要默认选中当天
                List<CalendarDate> mListData = ((CalendarGridViewAdapter) mGridView.getAdapter()).getListData();
                int count = mListData.size();
                for (int i = 0; i < count; i++) {
                    if (mListData.get(i).getSolar().solarDay == DateUtils.getDay()
                            && mListData.get(i).getSolar().solarMonth == DateUtils.getMonth()
                            && mListData.get(i).getSolar().solarYear == DateUtils.getYear()) {
                        if (null != mGridView.getChildAt(i) && mListData.get(i).isInThisMonth()) {
                            // mListData.get(i).setIsSelect(true);
                            onDateClickListener.onDateClick(mListData.get(i));
//                            mGridView.setItemChecked(i, true);
                            setItemSelect(i,true);
                        }
                    }
                }

            }
        });
    }

案例源码

https://gitee.com/itfitness/signin-calendar

上一篇 下一篇

猜你喜欢

热点阅读