如何在物料日历视图的选定日期中设置颜色

2022-08-10 00:00:00 colors decorator android java calendarview

我有这样的材料日历视图

private RecyclerView recycler_month_event_lists;
private FirebaseRecyclerAdapter adapter;

我是这样分配的

recycler_month_event_lists = (RecyclerView) view.findViewById(R.id.event_recycler_view);
calendar_view = (MaterialCalendarView) view.findViewById(R.id.calendar_view);

我已经通过Firebase RecillerAdapter列出了各自月份的事件,如下所示

recycler_month_event_lists.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(Event.class, R.layout.row_event_list,
            EventViewHolder.class, mRef.child("events").child("1").child(month)) {

        @Override
        protected void populateViewHolder(EventViewHolder viewHolder, Event model, int position) {
            List<CalendarDay> list = new ArrayList<CalendarDay>();
            ArrayList<Integer> day = model.getDate();
            ArrayList<Date> markedDates = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();

            //generating ArrayList<Date> 
            for (int i = 0; i < day.size(); ++i) {
                Calendar cal = Calendar.getInstance();
                Integer year = cal.get(Calendar.YEAR);
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.DAY_OF_MONTH, day.get(i));
                cal.set(Calendar.MONTH, Integer.valueOf(month));
                Date newdate = cal.getTime();
                markedDates.add(newdate);
            }
            //adding those generated ArrayList<Dates> in List<CalendarDays>
            for (Date date : markedDates) {
                // might be a more elegant way to do this part, but this is very explicit
                int year = date.getYear();
                int month = date.getMonth() -1; // months are 0-based in Calendar
                int newday = date.getDay();

                calendar.set(year, month, newday);
                CalendarDay calendarDay = CalendarDay.from(calendar);
                list.add(calendarDay);
            }

            //adding list of CalendarDays
            calendarDays = list;

            //code to decorate selected dates
            calendar_view.addDecorators(new EventDecorator(Color.parseColor("#00ff00"), calendarDays));



            if (!day.isEmpty()) {

                for(int i = 0; i < day.size();i++){
                    int z = i+1;
                    if(i == 0){
                        from = "From"+ " " +day.get(i);
                    }

                    if(z == day.size()){
                        to = "to"+ " " +day.get(i);
                    }
                }


            }

            viewHolder.ev_title.setText(model.getTitle());
            viewHolder.ev_description.setText(model.getDescription());
            viewHolder.ev_date.setText(from + " " + to);
            Glide.with(getActivity()).load(model.getIcon()).into(viewHolder.ev_image);


        }
    };
    recycler_month_event_lists.setAdapter(adapter);

}

此处ArrayList<Integer> day = model.getDate();返回需要上色的日期。一天的日志是这样的

D/arra: [1, 2, 3]

我不能给我的约会对象涂上颜色。我不知道我的代码出了什么问题。

我使用了

calendar_view.addDecorators(new EventDecorator(Color.parseColor("#00ff00"), calendarDays));

装饰选定的日子 我的EventDecorator是这样的

public class EventDecorator implements DayViewDecorator {

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

public EventDecorator(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));
}
}

我的日历天数是这样的

private Collection<CalendarDay> calendarDays = new Collection<CalendarDay>() {
    @Override
    public boolean add(CalendarDay object) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends CalendarDay> collection) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public boolean contains(Object object) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> collection) {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @NonNull
    @Override
    public Iterator<CalendarDay> iterator() {
        return null;
    }

    @Override
    public boolean remove(Object object) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        return false;
    }

    @Override
    public int size() {
        return 0;
    }

    @NonNull
    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @NonNull
    @Override
    public <T> T[] toArray(T[] array) {
        return null;
    }
};

解决方案

我看过您的代码,我确信问题出在EventDecorator中的decorate方法中。

view.addSpan(new DotSpan(5, color));

在材料日期上不起作用(或者至少不会按您希望的方式更改颜色)。

您可以使用

view.addSpan(new ForegroundColorSpan(color));

view.addSpan(new BackgroundColorSpan(color));

改为。

请确保为正确的日期调用public void decorate(DayViewFacade view)。如果您可以确保这一点,则设置颜色范围应该有效。

相关文章