日历

1、依赖:
//日历

implementation 'com.prolificinteractive:material-calendarview:1.4.0'

2、布局控件:


3、findViewById

//日历
materialCalendarView = (MaterialCalendarView) findViewById(R.id.materialCalendarView);

4、各种设置:

Calendar calendar = Calendar.getInstance();
materialCalendarView.state().edit()
        .setFirstDayOfWeek(Calendar.MONDAY)
        .setMinimumDate(CalendarDay.from(2013,4,3))
        .setMaximumDate(CalendarDay.from(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE)))
        .setCalendarDisplayMode(CalendarMode.MONTHS)
        .commit();

5、监听:

materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        //格式化日期
        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyyyMMdd");
        String dateS = yyMMdd.format(date.getDate()).toString();
        Log.d(TAG, "onDateSelected: "+dateS);
        Intent intent = new Intent();
        intent.setAction("com.geek.calender");
        intent.putExtra("date",dateS);
        //本地广播发送
        LocalBroadcastManager.getInstance(CalenderActivity.this).sendBroadcast(intent);
        finish();
    }
});

6、传值之后处理数据

//初始化广播,注册广播
    private void initBroadCaseManager() {
        IntentFilter intentFilter = new IntentFilter("com.geek.calender");
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver,intentFilter);
    }
@Override
    public void onDestroyView() {
        super.onDestroyView();
        //注销广播
        unRegisterBroadcast();
    }

    private void unRegisterBroadcast() {
        if(broadcastReceiver != null){
            LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcastReceiver);
        }
    }
//广播接收器
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String date = intent.getStringExtra("date");
            //判断是否是当天
            String yyyymmdd = DateUtil.getYYYYMMDD();
            if(date.equals(yyyymmdd)){
                isBefore = false;
                adapter.setBefore(isBefore,"今日新闻");
                presenter.getPaperData();
            }else{
                isBefore = true;
                adapter.setBefore(isBefore,date);
                presenter.getBeforePaperData(date);
            }
            Log.d(TAG, "日期为:"+date+"----"+isBefore);

        }
    };

你可能感兴趣的:(日历)