我正在使用 kizitonwose 的 CalendarView 库,我在 github 上使用指南做了一些东西。但是现在,我找不到一种方法可以将标题添加到我的日历中。我的 xml;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Calendar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<include
android:id="@+id/monthHeaderContainer"
layout="@layout/calendar_month_header_layout"/>
<include
android:id="@+id/titlesContainer"
layout="@layout/calendar_day_titles_container" />
<com.kizitonwose.calendar.view.CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cv_dayViewResource="@layout/calendar_day_layout"
app:cv_monthHeaderResource="@layout/calendar_day_titles_container" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如您所见,我尝试添加“calendar_month_header_layout”,但我无法将其放入 app:cv_monthHeaderResource,因为我在其中添加了一些不同的内容。这是我的片段文件;
package com.ahmetkaan.kediy
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.children
import androidx.core.view.forEachIndexed
import com.ahmetkaan.kediy.databinding.CalendarDayLayoutBinding
import com.ahmetkaan.kediy.databinding.FragmentCalendarBinding
import com.kizitonwose.calendar.core.CalendarDay
import com.kizitonwose.calendar.core.CalendarMonth
import com.kizitonwose.calendar.core.DayPosition
import com.kizitonwose.calendar.view.MonthDayBinder
import com.kizitonwose.calendar.view.MonthHeaderFooterBinder
import com.kizitonwose.calendar.view.ViewContainer
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.YearMonth
class Calendar : Fragment() {
private lateinit var binding: FragmentCalendarBinding
private var selectedDate: LocalDate? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentCalendarBinding.inflate(layoutInflater, container, false)
val currentMonth = YearMonth.now()
val startMonth = currentMonth.minusMonths(100)
val endMonth = currentMonth.plusMonths(100)
val daysOfWeek = DayOfWeek.values()
binding.calendarView.setup(startMonth, endMonth, daysOfWeek[0])
binding.calendarView.scrollToMonth(currentMonth)
class DayViewContainer(view: View) : ViewContainer(view) {
val textView = CalendarDayLayoutBinding.bind(view).calendarDayText
lateinit var day: CalendarDay
init {
view.setOnClickListener {
if (day.position == DayPosition.MonthDate) {
val currentSelection = selectedDate
if (currentSelection == day.date) {
selectedDate = null
binding.calendarView.notifyDateChanged(currentSelection)
} else {
selectedDate = day.date
binding.calendarView.notifyDateChanged(day.date)
if (currentSelection != null) {
binding.calendarView.notifyDateChanged(currentSelection)
}
}
}
}
}
}
binding.calendarView.dayBinder = object : MonthDayBinder<DayViewContainer> {
override fun create(view: View) = DayViewContainer(view)
override fun bind(container: DayViewContainer, data: CalendarDay) {
container.day = data
val day = data
val textView = container.textView
textView.text = day.date.dayOfMonth.toString()
if (day.position == DayPosition.MonthDate) {
// Show the month dates. Remember that views are reused!
textView.visibility = View.VISIBLE
if (day.date == selectedDate) {
//textView.setTextColor(Color.WHITE)
textView.setBackgroundResource(R.drawable.selection_background)
} else {
//textView.setTextColor(Color.BLACK)
textView.background = null
}
} else {
// Hide in and out dates
textView.visibility = View.INVISIBLE
}
}
}
class MonthViewContainer(view: View) : ViewContainer(view) {
var titlesContianer = view as ViewGroup
}
binding.calendarView.monthHeaderBinder = object : MonthHeaderFooterBinder<MonthViewContainer> {
override fun create(view: View) = MonthViewContainer(view)
override fun bind(container: MonthViewContainer, data: CalendarMonth) {
if (container.titlesContianer.tag == null) {
container.titlesContianer.tag = data.yearMonth
container.titlesContianer.children.map {it as TextView}.forEachIndexed { index, textView ->
val title = when (index) {
0 -> "Pzt"
1 -> "Sal"
2 -> "Çar"
3 -> "Per"
4 -> "Cum"
5 -> "Cmt"
6 -> "Paz"
else -> ""
}
textView.text = title
}
}
}
}
return binding.root
}
}
如您所见,我正在使用它来设置与我的语言兼容的工作日名称。我如何在我的日历视图中添加月份标题(例如 2023 年 4 月或使用我的语言 Nisan 2023)?