首页 > 解决方案 > 如何使用android中的回收器视图将事件放入日历中

问题描述

我正在使用 JAVA 创建一个 android 应用程序,其中将显示带有一些事件的日历。我在回收站视图的帮助下创建日历,GridLayoutManager并在java.time.LocalDate

我需要帮助如何使用回收站视图在网格布局单元格中显示事件。我不知道该怎么做以及如何将事件放在回收站视图中。 我已经为适配器类、视图持有者类、日历工具类和主要活动类编写了代码。

适配器类:-


 private final ArrayList<LocalDate> daysOfMonth;
    private final OnItemListner onItemListner;

    public CalAdapter(ArrayList<LocalDate> daysOfMonth, OnItemListner onItemListner) {
        this.daysOfMonth = daysOfMonth;
        this.onItemListner = onItemListner;
    }

public CalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.calendar_cell_hp, parent, false);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = (int) (parent.getHeight() * 0.166666666); //This will give the calendar cell hieght

        return new CalViewHolder(view, onItemListner);
    }

 public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {

        //holder.dayOfMonth.setText(daysOfMonth.get(position));

        final LocalDate date = daysOfMonth.get(position);
        if (date == null)
            holder.dayOfMonth.setText("");
        else {
            holder.dayOfMonth.setText(String.valueOf(date.getDayOfMonth()));
            if (date.equals(CalendarUtils.selectedDate))          //this will display current date
               holder.parentView.setBackgroundColor(Color.BLUE); //this will display current date
            
        }


    }


public int getItemCount() {
        return daysOfMonth.size();
    }

//this will show the date on click
    public interface OnItemListner{
        void onItemClick(int position, String dayText);
    }

ViewHolder 类


class CalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public final View parentView;
    public final TextView dayOfMonth;
    private final CalAdapter.OnItemListner onItemListner;

    public CalViewHolder(@NonNull View itemView, CalAdapter.OnItemListner onItemListner) {

        super(itemView);
        parentView = itemView.findViewById(R.id.parentView);
        dayOfMonth = itemView.findViewById(R.id.cellDayTextHP);
        this.onItemListner = onItemListner;
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onItemListner.onItemClick(getAdapterPosition(), (String) dayOfMonth.getText());
    }
}

日历实用程序类

public static LocalDate selectedDate;
 public static String monthYearFromDate(LocalDate date)
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
        return date.format(formatter);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static ArrayList<LocalDate> daysInMonthArray(LocalDate date)
    {
        ArrayList<LocalDate> daysInMonthArray = new ArrayList<>();
        YearMonth yearMonth = YearMonth.from(date);

        int daysInMonth = yearMonth.lengthOfMonth();

        LocalDate firstOfMonth = CalendarUtils.selectedDate.withDayOfMonth(1);
        int dayOfWeek = firstOfMonth.getDayOfWeek().getValue();

        for(int i = 1; i <= 42; i++)
        {
            if(i <= dayOfWeek || i > daysInMonth + dayOfWeek)
                daysInMonthArray.add(null);
            else
                daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek));
        }
        return  daysInMonthArray;
    }

在这里,我有使用方法来设置月份视图MainActivity.java


private void setMonthView() {

        monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
        ArrayList<LocalDate> daysInMonth = daysInMonthArray(CalendarUtils.selectedDate);

        CalAdapter calAdapter = new CalAdapter(daysInMonth, this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
        calendarRecyclerView.setLayoutManager(layoutManager);
        calendarRecyclerView.setAdapter(calAdapter);

    }

我附上了两张图片:一张是我的输出,另一张是示例输出,即我想要实现的目标。

我的输出

样本输出

我已经厌倦了将事件设置为日期,即 2021 年 8 月 15 日onBindViewHolder:-

String date1 = "14/07/2021";
        holder.event.setText("90% good"+date.format(DateTimeFormatter.ofPattern(date1)));

但出现错误: Attempt to invoke virtual method 'java.lang.String java.time.LocalDate.format(java.time.format.DateTimeFormatter)' on a null object reference

我真的需要帮助请...

标签: javaandroidandroid-recyclerviewlogic

解决方案


您可以自定义视图持有者布局以显示事件。如果那天有任何事件,则显示它,否则,设置为事件视图可见性。

为此,您可以将包含日期和事件的自定义类的 ArrayList 传递给适配器,而不是将 LocalDate 的 ArrayList 传递给适配器,例如 MyDate 的 ArrayList :

   class MyDate {
        LocalDate date;
        MyEvent event;
    }

    class MyEvent {
        String title;
        ...
    }

MyDate 类的 MyEvent 应该从您的数据库或任何其他方式填充。

并在您的 onbindview 持有人中使用它来显示事件:

    ArrayList<MyDate> myDates; // instead of  ArrayList<LocalDate> daysOfMonth;

    public void onBindViewHolder(@NonNull CalViewHolder holder, int position) {
        final MyDate myDate = myDates.get(position);
        if (myDate == null) {
            holder.dayOfMonth.setText("");
            holder.eventTextView.setText("");
        } else {

            // set date
            holder.dayOfMonth.setText(String.valueOf(myDate.date.getDayOfMonth()));

            //set background color
            if (myDate.date.equals(CalendarUtils.selectedDate)) {
                holder.parentView.setBackgroundColor(Color.BLUE);
            }

            //set event
            if (myDate.event != null) {
                holder.eventTextView.setText(myDate.event.title);
                holder.eventTextView.setVisibility(View.VISIBLE);
            } else {
                holder.eventTextView.setVisibility(View.GONE);
            }
        }
    }

推荐阅读