How do I change the background color of calendar dates in MaterialCalendarView

后端 未结 1 1051
猫巷女王i
猫巷女王i 2021-01-14 08:58

I\'m trying to change the background color of dates using a JSON response I get. But I\'m having some difficulty.

Here is my code:



        
相关标签:
1条回答
  • 2021-01-14 09:31

    The first step is to create a DayViewDecorator that will take as parameters a Date and a color:

    public class EventDecorator implements DayViewDecorator {
    
        private final Drawable drawable;
        private final CalendarDay day;
        private final int color;
    
        public EventDecorator(MaterialCalendarView view, Date date, int color) {
            this.day = CalendarDay.from(date);
            this.color = color;
            this.drawable = createTintedDrawable(view.getContext(), color);
        }
    
        @Override
        public boolean shouldDecorate(CalendarDay day) {
            if (this.day.equals(day)) {
                return true;
            }
            return false;
        }
    
        @Override
        public void decorate(DayViewFacade view) {
            view.setSelectionDrawable(drawable);
        }
    
        private static Drawable createTintedDrawable(Context context, int color) {
            return applyTint(createBaseDrawable(context), color);
        }
    
        private static Drawable applyTint(Drawable drawable, int color) {
            Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(wrappedDrawable, color);
            return wrappedDrawable;
        }
    
        private static Drawable createBaseDrawable(Context context) {
            return ContextCompat.getDrawable(context, R.drawable.day);
        }
    }
    

    (N.B. I used the code in this answer to apply the tint. Also, since you didn't specify, I have assumed that the drawable is some kind of image that needs tinting in this way.)

    The next step is to create an Event class for storing the events you parse from the API call:

    public class Event {
    
        private Date date;
        private int color;
    
        public Event(Date date, int color) {
            this.date = date;
            this.color = color;
        }
    
        public Date getDate() {
            return date;
        }
    
        public int getColor() {
            return color;
        }
    }
    

    Now we need to add logic to your onResponse() method to parse the JSON and add decorators for each event. It is hard to know what exactly to write because you haven't given a sample of the JSON. Your previous questions show that you already know how to parse a Date so I think this will be enough. Since you haven't specified, I'll leave it out for now. Also, I'm just appending to your code - I won't try and refactor it much.

    @Override
    public void onResponse(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        JSONArray jsonarray = null;
        try {
            jsonarray = object.getJSONArray("Table");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        SimpleDateFormatter formatter = new SimpleDateFormatter(); //TODO: update this line with the correct formatter
        List<Event> events = new ArrayList<>();
        for (int i = 0; i < jsonarray.length(); i++) {
            try {
                JSONObject obj = jsonarray.getJSONObject(i);
                String str2 = obj.optString("eventdate");
                String str1 = obj.optString("eventcolor"); 
                Date date = formatter.parse(str2);
                int color = Integer.parseInt(str1); //TODO: update this line with the correct code to parse your color
                Event event = new Event(date, color);
                events.add(event);
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
    
        for (Event event : events) {
            EventDecorator eventDecorator = new EventDecorator(calendarView, event.getDate(), event.getColor());
            calendarView.addDecorator(eventDecorator);
        }
    }
    
    0 讨论(0)
提交回复
热议问题