尝试使用RecyclerView创建时间选择器

问题描述 投票:0回答:1

早安,所以我现在正在学习,我想创建一个时间选择器,但在适配器支架上遇到了问题

如何将所选开始时间的背景颜色更改为结束时间

    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)
    }
} 

[输入,我单击了8:00 am和9:00 am

我的代码当前输出enter image description here

我想要的输出enter image description here

layout_item_time_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>
android kotlin android-recyclerview recycler-adapter
1个回答
0
投票
我在这里发布了经过编辑的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>

选择开始时间时,根据需要设置'between_quarters'视图的背景颜色!

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 } } }

如果无法使用,请尝试此,然后尽快更新我;)
© www.soinside.com 2019 - 2024. All rights reserved.