Попытка создать селектор времени, используя RecyclerView - PullRequest
0 голосов
/ 28 января 2020

Добрый день, я сейчас учусь, хочу создать селектор времени, и у меня возникла проблема с держателем адаптера.

Как мне изменить цвет фона выбранного startTime на endTime

    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 и 9:00 утра

Текущий выход My Code прямо сейчас
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>

1 Ответ

0 голосов
/ 28 января 2020

я публикую здесь отредактированный 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>

Когда вы выбираете время начала, установите желаемый цвет фона для вида 'Между_вартирой!

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

                }

Попробуйте, если он не работает, обновите меня как можно скорее;)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...