MaterialCalendarView Dotspan not appearing

时间:2018-06-04 16:45:07

标签: android

I'm using this library: https://github.com/prolificinteractive/material-calendarview in my project. Here are segments of my code. I used the EventDecorator class that was provided in the documentation for the Dot Span and renamed it. The calendarView is a MaterialCalendarView in the EventFragment. I wanted to add a Dot Span whenever I click on a specific date in the calendarView, but it doesn't seem to be working/showing up. markedDates is an array list containing all the CalendarDay that were clicked on, so every time I click on a specific date in the calendarView, I add the CalendarDay to the array list. Does anyone know how to fix this/make it work?

public class CurrentDayDecorator implements DayViewDecorator {

    private final int color;
    private final HashSet<CalendarDay> dates;

    public CurrentDayDecorator(int color, Collection<CalendarDay> dates) {
        this.color = color;
        this.dates = new HashSet<>(dates);
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return dates.contains(day);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new DotSpan(5, color));
    }
}

In EventFragment class

calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                markedDates.add(date);

                currentDayDecorator = new CurrentDayDecorator(Color.WHITE, markedDates);
                widget.addDecorator(currentDayDecorator);
                widget.invalidateDecorators();
            }
        });

1 个答案:

答案 0 :(得分:0)

我知道已经为时已晚,但是我自己的解决方案可能会带来更多好处,因为我自己找不到添加点的良好实现。我将展示一个简单的实现方式,即在用户要点击的所有日期上放置一个红色的点,我相信您可以相应地自定义内容。所以,我们开始。

首先,创建一个新的Java类“ EventDecorator”,并将以下所有代码复制到其中-

public class EventDecorator extends AppCompatActivity implements DayViewDecorator {

 private final int color = Color.parseColor("#FF0000");
 private final CalendarDay dates;

 public EventDecorator(CalendarDay dates) {

    this.dates = dates;
 }

 @Override
 public boolean shouldDecorate(CalendarDay day) {
     return dates==day;
 }

 @Override
 public void decorate(DayViewFacade view) {
     view.addSpan(new DotSpan(5, color));
 }
}

现在进入片段,我们将在其中设置onDateChangegListener,因此,将所有这些代码复制到createView-

  CalendarView calendarView = view.findViewById(R.id.calendarView);     //remove view. if it's an activity
  calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
        @Override
        public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
           
            EventDecorator  eventDecorator= new EventDecorator(date);
            widget.addDecorator(eventDecorator);
            widget.invalidateDecorators();

        }
    });

这就是事情的运行方式,如果您只想在带有某些事件的那些日期上使用这些点,则可以对这些日期进行数组处理,存储它们,然后将它们传递给EventDecorator类。 希望这对某人有帮助。随便问问。