gpt4 book ai didi

java - Material Calendar View如何设置选中日期的颜色

转载 作者:行者123 更新时间:2023-11-30 00:23:05 25 4
gpt4 key购买 nike

我有这样的 Material 日历 View

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

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

最佳答案

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

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

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

你可以使用

view.addSpan(new ForegroundColorSpan(color));

view.addSpan(new BackgroundColorSpan(color));

相反。

请确保为正确的日期调用 public void decorate(DayViewFacade view)。如果您可以确保设置颜色跨度应该有效。

关于java - Material Calendar View如何设置选中日期的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45914857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com