Android 尝试使用RecyclerView创建时间选择器
你好,所以我现在正在学习,我想创建一个时间选择器,我在适配器支架上遇到了一个问题 如何将所选开始时间的背景色更改为结束时间Android 尝试使用RecyclerView创建时间选择器,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,你好,所以我现在正在学习,我想创建一个时间选择器,我在适配器支架上遇到了一个问题 如何将所选开始时间的背景色更改为结束时间 class TimeAdapter constructor( listTime: MutableList<TimeModel>, mainActivity: MainActivity ) : RecyclerView.Adapter<TimeAdapter.ItemViewHolder>() { private val
class TimeAdapter constructor(
listTime: MutableList<TimeModel>,
mainActivity: MainActivity
) : RecyclerView.Adapter<TimeAdapter.ItemViewHolder>() {
private val TAG = "TimeAdapter"
private val timeList : MutableList<TimeModel> = 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)
}
}
类时间适配器构造函数(
listTime:MutableList,
主要活动:主要活动
):RecyclerView.Adapter(){
private val TAG=“TimeAdapter”
private val timeList:MutableList=listTime
private val main=main活动
重写CreateViewHolder(父级:ViewGroup,viewType:Int):ItemViewHolder{
val itemView=LayoutInflater.from(parent.context)
.充气(R.layout.layout\u项目\u时间\u选择,父项,false)
返回ItemViewHolder(itemView)
}
重写getItemCount():Int{
返回timeList.size
}
覆盖BindViewHolder(holder:ItemViewHolder,位置:Int){
if(timeList[position].timeTitle==null){
holder.mTitle.visibility=View.INVISIBLE
}
holder.mTitle.text=时间列表[位置].timeTitle
holder.timeQuarter.setOnClickListener{
main.updateArrayIfSelected(位置)
时间主义者{
Log.d(标记“Position$it”+“”+it.isThisSelected.toString())
}
时间主义者{
if(it.isThisSelected==true){
holder.timeQuarter.setBackgroundColor(main.resources.getColor(R.color.time\u select\u color))
}
}
}
}
类ItemViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
var mTitle:TextView=itemView.findViewById(R.id.time\u标题)
var timeQuarter:View=itemView.findViewById(R.id.time\u quarter)
//var firstQuarter:View=itemView.findViewById(R.id.firstQuarter)
//var secondQuarter:View=itemView.findViewById(R.id.secondQuarter)
}
}
我点击了8:00am和9:00am的输入
我的代码当前输出我想要的输出
layout\u item\u time\u select.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@android:color/white">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
<View
android:layout_width=".8dp"
android:layout_height="100dp"
android:layout_marginStart=".5dp"
android:layout_marginEnd=".5dp"
android:background="@color/gray_tint" />
<LinearLayout
android:layout_width="75dp"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
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" />
<View
android:id="@+id/time_quarter"
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我在这里发布了编辑过的xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="@android:color/white">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
<View
android:layout_width=".8dp"
android:layout_height="100dp"
android:layout_marginStart=".5dp"
android:layout_marginEnd=".5dp"
android:background="#85DDDDDD" />
<LinearLayout
android:layout_width="75dp"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
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" />
<View
android:id="@+id/time_quarter"
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
<!--apply change below-->
<View
android:id="@+id/between_quarters"
android:layout_width="75dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
试试这个,如果它不起作用,那么尽快更新我;) 这就是问题所在same@jeiboi您想要相同的开始时间和结束时间背景色还是不同的颜色?我的意思是,我想要的输出是当我单击上午8:00和上午9:00时,绿色将高亮显示从上午8:00到上午9:00的范围。不仅仅是8:00am酒吧和9:00am酒吧你能告诉我什么是添加时间列表吗?你在哪里设置isThisSelected为true?main.updateArrayIfSelected(position)这是什么?
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))
holder.between_quarters.setBackgroundColor(main.resources.getColor(R.color.time_select_color)) //apply change here
}
}
}