早安,所以我现在正在学习,我想创建一个时间选择器,但在适配器支架上遇到了问题
如何将所选开始时间的背景颜色更改为结束时间class TimeAdapter constructor(
listTime: MutableList,
mainActivity: MainActivity
) : RecyclerView.Adapter() {
private val TAG = "TimeAdapter"
private val timeList : MutableList = listTime
private val main = mainActivity
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.layout_item_time_select, parent, false)
return ItemViewHolder(itemView)
}
override fun getItemCount(): Int {
return timeList.size
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
if (timeList[position].timeTitle == null){
holder.mTitle.visibility = View.INVISIBLE
}
holder.mTitle.text = timeList[position].timeTitle
holder.timeQuarter.setOnClickListener {
main.updateArrayIfSelected(position)
timeList.forEach {
Log.d(TAG,"Position $it" + " " + it.isThisSelected.toString())
}
timeList.forEach {
if(it.isThisSelected == true){
holder.timeQuarter.setBackgroundColor(main.resources.getColor(R.color.time_select_color))
}
}
}
}
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var mTitle: TextView = itemView.findViewById(R.id.time_title)
var timeQuarter: View = itemView.findViewById(R.id.time_quarter)
// var firstQuarter: View = itemView.findViewById(R.id.firstQuarter)
// var secondQuarter: View = itemView.findViewById(R.id.secondQuarter)
}
}[输入,我单击了8:00 am和9:00 am
我的代码当前输出
我想要的输出
layout_item_time_select.xml<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@android:color/white">
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
android:layout_width=".8dp"
android:layout_height="100dp"
android:layout_marginStart=".5dp"
android:layout_marginEnd=".5dp"
android:background="@color/gray_tint" />
android:layout_width="75dp"
android:layout_height="100dp"
android:orientation="vertical">
android:id="@+id/time_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:singleLine="true"
android:text="8:00 AM"
android:textColor="@android:color/black"
android:textSize="15sp" />
android:id="@+id/time_quarter"
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
android:clickable="true"
android:focusable="true" />