2017-08-28 75 views
1

我有材料日曆照片喜歡這張如何設置顏色的材料日曆視圖中選擇日期

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天= 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

我看了你的代碼,我很確信問題出在EventDecorator裏的decorate方法。

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

在Material Dates上無效(或者至少不會按您希望的方式更改顏色)。

你可以使用

view.addSpan(new ForegroundColorSpan(color)); 

view.addSpan(new BackgroundColorSpan(color)); 

代替。

請確保public void decorate(DayViewFacade view)被調用的正確日期。如果你能確保那麼設置顏色範圍應該工作。希望這有助於:)

+0

我在最後一段感到困惑。你的意思是在EventDecorator中傳遞的CalendarDays應該有相應的月份和年份的正確日期 –

+0

偉大的兄弟你的最後一段解決了我的問題。上面的代碼也可以工作,但不是我想要的。問題是getYear()方法並沒有退休。多謝兄弟 –

相關問題