如何在材质日历视图

时间:2017-08-28 08:34:18

标签: java android colors decorator 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);

我已经通过FirebaseRecyclerAdapater列出了我个月的事件,就像这样

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 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));
}
}

我的CalendarDays就像这样

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;
    }
};

任何帮助将不胜感激。提前致谢。

1 个答案:

答案 0 :(得分:1)

我查看了您的代码,我非常确信问题出在decorate内的EventDecorator方法中。

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

不适用于材质日期(或者至少它不会按照您希望的方式更改颜色)。

您可以使用

view.addSpan(new ForegroundColorSpan(color));

view.addSpan(new BackgroundColorSpan(color));

代替。

请确保调用public void decorate(DayViewFacade view)以获取正确的日期。如果你能确保那么设置颜色范围应该有效。希望这会有所帮助:)