在CalendarView中删除月份和年份标题

时间:2019-04-10 14:40:52

标签: java android android-recyclerview calendarview

我有一个父亲RecyclerView,有一个孩子RecyclerView,有多个CalendarView,显示了指定年份的月份,如下所示:

enter image description here

  1. 我不想像每个日历视图条目的标题一样显示 2015年1月
  2. 我不想显示所有两位数字的日期。
  3. 我想禁用所有特定日期的点击,并从日期中删除蓝色圆圈。

我该怎么做?

这是我给父母RecyclerView的代码:
视图:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_margin="@dimen/size_10">

    <TextView
        android:id="@+id/big_calendar_year_text_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/size_10"
        android:textStyle="bold"
        android:textSize="@dimen/font_size_18sp"
        android:text="@string/insertImage"
        android:textColor="@color/black" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/big_calendar_months_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        app:reverseLayout="true"/>

</androidx.appcompat.widget.LinearLayoutCompat>

适配器:

public class AdapterBigCalendar extends RecyclerView.Adapter<AdapterBigCalendar.AdapterBigCalendarViewHolder> {
    private static final int periodToShow = 21;
    private final int[] yearsArray = new int[periodToShow];
    private final RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();

    public AdapterBigCalendar(long dateInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(dateInMillis));
        int currentYear = calendar.get(Calendar.YEAR);

        yearsArray[periodToShow/2] = currentYear;
        int tempYear = currentYear;
        for (int i = (yearsArray.length/2) - 1; i >= 0 ; i--) {
            yearsArray[i] = --tempYear;
        }
        for (int i = (yearsArray.length/2) + 1; i < yearsArray.length; i++) {
            yearsArray[i] = ++tempYear;
        }
    }

    @NonNull
    @Override
    public AdapterBigCalendarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdapterBigCalendarViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.big_calendar_view_holder, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterBigCalendarViewHolder holder, int position) {
        int currentYear = yearsArray[position];
        holder.bind(currentYear);
    }

    @Override
    public int getItemCount() {
        return yearsArray.length;
    }

    class AdapterBigCalendarViewHolder extends RecyclerView.ViewHolder {
        private static final int columnSpanCount = 3;

        @BindView(R.id.big_calendar_year_text_field) TextView currentYearTextView;
        @BindView(R.id.big_calendar_months_recycler_view) RecyclerView recyclerViewCurrentYear;

        AdapterBigCalendarViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void bind(int year) {
            AdapterMonthsInGivenYear monthsInYearAdapter =
                    new AdapterMonthsInGivenYear(recyclerViewCurrentYear.getContext(), year);
            GridLayoutManager layoutManager = new GridLayoutManager(recyclerViewCurrentYear.getContext(),
                            columnSpanCount,
                            RecyclerView.VERTICAL,
                            false);
            layoutManager.setInitialPrefetchItemCount(12);

            recyclerViewCurrentYear.setLayoutManager(layoutManager);
            recyclerViewCurrentYear.setAdapter(monthsInYearAdapter);
            recyclerViewCurrentYear.setRecycledViewPool(recycledViewPool);
            recyclerViewCurrentYear.setNestedScrollingEnabled(false);

            currentYearTextView.setText(String.valueOf(year));
        }

    }
}  

片段内部的用法:

AdapterBigCalendar adapterBigCalendar = new AdapterBigCalendar(System.currentTimeMillis());

        bigCalendarRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false));
        bigCalendarRecyclerView.setAdapter(adapterBigCalendar);
        bigCalendarRecyclerView.setNestedScrollingEnabled(false);
        bigCalendarRecyclerView.scrollToPosition(10);  

这是我的孩子RecyclerView的代码:
视图:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/big_calendar_name_of_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="@dimen/font_size_15sp"
        android:text="@string/insertImage"
        android:textColor="@color/black" />

    <CalendarView
        android:id="@+id/calendar_month_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:clickable="false"
        android:focusableInTouchMode="false"
        android:focusedMonthDateColor="@color/transparent"
        android:scrollbars="none"
        android:unfocusedMonthDateColor="@android:color/transparent" />

</androidx.appcompat.widget.LinearLayoutCompat>  

适配器:

public class AdapterMonthsInGivenYear extends RecyclerView.Adapter<AdapterMonthsInGivenYear.AdapterMonthsInGivenYearViewHolder>{

    private final ArrayList<String> monthNumberToNameMapping = new ArrayList<>(12);
    private final int year;

    AdapterMonthsInGivenYear(Context context, int year) {
        this.year = year;
        Resources resources = context.getResources();
        monthNumberToNameMapping.add(resources.getString(R.string.January));
        monthNumberToNameMapping.add(resources.getString(R.string.February));
        monthNumberToNameMapping.add(resources.getString(R.string.March));
        monthNumberToNameMapping.add(resources.getString(R.string.April));
        monthNumberToNameMapping.add(resources.getString(R.string.May));
        monthNumberToNameMapping.add(resources.getString(R.string.June));
        monthNumberToNameMapping.add(resources.getString(R.string.July));
        monthNumberToNameMapping.add(resources.getString(R.string.August));
        monthNumberToNameMapping.add(resources.getString(R.string.August));
        monthNumberToNameMapping.add(resources.getString(R.string.October));
        monthNumberToNameMapping.add(resources.getString(R.string.November));
        monthNumberToNameMapping.add(resources.getString(R.string.December));
    }

    @NonNull
    @Override
    public AdapterMonthsInGivenYearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdapterMonthsInGivenYearViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.big_calendar_single_month_view_holder, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterMonthsInGivenYearViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return monthNumberToNameMapping.size();
    }

    class AdapterMonthsInGivenYearViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.big_calendar_name_of_month) TextView nameOfMonth;
        @BindView(R.id.calendar_month_entry) CalendarView calendarMonthEntry;

        AdapterMonthsInGivenYearViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void bind(final int monthIndex) {
            int actualMonthIndex = monthIndex + 1;
            DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
            DateTime dateTimeMin = new DateTime(year, actualMonthIndex,1,0,0, dateTimeZone);
            DateTime dateTimeMax = new DateTime(year, actualMonthIndex, getLastDayOfMonth(actualMonthIndex, year),0,0, dateTimeZone);
            long dateTimeMillisMin = dateTimeMin.getMillis();
            long dateTimeMillisMax = dateTimeMax.getMillis();

            nameOfMonth.setText(monthNumberToNameMapping.get(monthIndex));

            calendarMonthEntry.setMinDate(dateTimeMillisMin);
            calendarMonthEntry.setMaxDate(dateTimeMillisMax);
            calendarMonthEntry.clearFocus();
            calendarMonthEntry.setClickable(false);
            calendarMonthEntry.setLongClickable(false);
            calendarMonthEntry.setFocusable(false);

        }

        private int getLastDayOfMonth(final int month, final int year) {
            int lastDay = 0;

            if ((month >= 1) && (month <= 12)) {
                LocalDate aDate = new LocalDate(year, month, 1);

                lastDay = aDate.dayOfMonth().getMaximumValue();
            }

            return lastDay;
        }
    }
}  

它在父ViewHolder的{​​{1}}内部使用

0 个答案:

没有答案