我想实现一个 RecyclerView,显示一周的日期和月份,包括当前日期。并且它需要可滚动,当前日期应位于中心。我明白我需要
我想实现一个 RecyclerView,显示一周的日期和月份,包括当前日期。并且它需要可滚动,当前日期应位于中心。我知道我需要 RecyclerView 的 Adapter 和 ViewHolder,但我不知道如何实现日期和月份。请帮帮我。
我需要实现这些
列表项日期.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data></data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/ttnorms_medium"
android:textColor="@color/white"
android:textSize="60sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="12/31" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
适配器.kt
class Adapter(private val data: ArrayList<Date>) : RecyclerView.Adapter<ViewHolder>() {
private lateinit var binding: ListitemDateBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding =
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.listitem_date,
parent,
false
)
return ViewHolder(binding)
}
override fun getItemCount(): Int =
data.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
}
ViewHolder.kt
class ViewHolder (
private val binding: ListitemDateBinding
) : RecyclerView.ViewHolder(binding.root){
fun bind(date: Date){
val currentDateTime = LocalDateTime.now()
val format = SimpleDateFormat("M/d", Locale.US)
val cal = Calendar.getInstance(Locale.US)
val currentDate = Calendar.getInstance(Locale.US)
val currentDay = currentDate[Calendar.DAY_OF_MONTH]
val currentMonth = currentDate[Calendar.MONTH]
binding.textDate.text = currentDateTime.toString()
}
}