Using Multiple DotSpan in MaterialCalendarView

Is there a way to connect more than 1 DotSpan to a date with Android MaterialCalendarView? Although I have 2 DotSpan added to my CalendarView, it still only displays 1 DotSpan ... I use the following code:

@Override
public boolean shouldDecorate(CalendarDay day) {
    day.copyTo(calendar);
    for (int i = 0; i < weekDayStrTmp.size(); i++) {
        if (weekDayStrTmp.contains(day)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

@Override
public void decorate(DayViewFacade view) {
    view.addSpan(new DotSpan(8, myContext.getResources().getColor(R.color.myColor3)));
    view.addSpan(new DotSpan(8, myContext.getResources().getColor(R.color.myColor3)));
}
+4
source share
2 answers

You override the first DotSpan with the second. This DotSpan class allows you to create a centered color dot under the text, so if you put one on top of the other, the first one will not be visible.

I managed to create several DotSpans in the same DayViewFacade view, I'm not sure if this is the exact solution you were looking for, but I'm sure it will be useful:

, Decorator, DayViewDecorator, OrangeDecorator.

, LineBackgroundSpan, MyCustomOrangeSpan.

, DotSpan EventDecorator, , .

"decorate" ( OrangeDecorator) LineBackgroundSpan :

@Override
public void decorate(DayViewFacade view) {
    view.addSpan(new MyCustomOrangeSpan(6, ContextCompat.getColor(mContext, R.color.AppOrange)));
}

drawBackground ( MyCustomOrangeSpan) , :

@Override
    public void drawBackground(Canvas canvas, Paint paint, int left, int right, int top, int baseline,
                               int bottom, CharSequence text, int start, int end, int lnum) {

        int oldColor = paint.getColor();
        if (color != 0) {
            paint.setColor(color);
        }

        canvas.drawCircle((left + right) / 2 - 20, bottom + radius, radius, paint);
        paint.setColor(oldColor);

    }

, DayViewDecorators LineBackgroundSpan ( ):

BlueDecorator blueDecorator = new BlueDecorator(getActivity(),eventsDays,eventsMap);
OrangeDecorator orangeDecorator = new OrangeDecorator(getActivity(),eventsDays,eventsMap);
GreenDecorator greenDecorator = new GreenDecorator(getActivity(),eventsDays,eventsMap);
materialCalendarView.addDecorator(blueDecorator);
materialCalendarView.addDecorator(orangeDecorator);
materialCalendarView.addDecorator(greenDecorator);
+8

, :

-, DotSpan, :

public class CustmMultipleDotSpan implements LineBackgroundSpan {


    private final float radius;
    private int[] color = new int[0];


    public CustmMultipleDotSpan() {
        this.radius = DEFAULT_RADIUS;
        this.color[0] = 0;
    }


    public CustmMultipleDotSpan(int color) {
        this.radius = DEFAULT_RADIUS;
        this.color[0] = 0;
    }


    public CustmMultipleDotSpan(float radius) {
        this.radius = radius;
        this.color[0] = 0;
    }


    public CustmMultipleDotSpan(float radius, int[] color) {
        this.radius = radius;
        this.color = color;
    }

    @Override
    public void drawBackground(
            Canvas canvas, Paint paint,
            int left, int right, int top, int baseline, int bottom,
            CharSequence charSequence,
            int start, int end, int lineNum
    ) {

        int total = color.length > 5 ? 5 : color.length;
        int leftMost = (total - 1) * -10;

        for (int i = 0; i < total; i++) {
            int oldColor = paint.getColor();
            if (color[i] != 0) {
                paint.setColor(color[i]);
            }
            canvas.drawCircle((left + right) / 2 - leftMost, bottom + radius, radius, paint);
            paint.setColor(oldColor);
            leftMost = leftMost + 20;
        }
    }
}

EventDecorator:

public class EventDecorator implements DayViewDecorator {

    private final int[] colors;
    private final HashSet<CalendarDay> dates;


    public EventDecorator(Collection<CalendarDay> dates, int[] colors) {
        //this.color = color;
        this.dates = new HashSet<>(dates);

        this.colors = colors;

    }


    public EventDecorator(List<MainActivity.Filter> filteredEvents) {
        //this.color = color;

        this.dates = new HashSet<>(filteredEvents.get(0).calDayArr);
        int[] colors = new int[1];
        colors[0] = filteredEvents.get(0).color;
        this.colors = colors;

    }

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

    @Override
    public void decorate(DayViewFacade view) {

        view.addSpan((new CustmMultipleDotSpan(5,colors)));

    }


}

.

. 5 , -

calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));

EventDays - CalendarDay threeColors - int

int[] threeColors = {
Color.rgb(0, 0, 255),
Color.rgb(0, 255, 0),
Color.rgb(255, 0, 0)};

, , , , . , , , - , . 2 - -10, 3 - -20 . .

5 , , , , .

0

Source: https://habr.com/ru/post/1613258/


All Articles