0

I make a calendar in one line. I am using recyclerview. How can I track the position of the current date to show it in the center of the screen? Right now it shows first day of month

This is what it is showing now

Calendar stating from 1st date

And below is the needed one

Need to start from current date range

class DateAdapter(private val dateList: List<Calendar>, val currentDate: String):
                RecyclerView.Adapter<MyViewHolder>(){

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                val layoutInflater = LayoutInflater.from(parent?.context)
                val cellForRow = layoutInflater.inflate(R.layout.date_layout, parent,  false)

                val holder = MyViewHolder(parent)


                return holder
            }

            override fun getItemCount(): Int {
                return dateList.count()
            }

            override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

                holder?.setData(currentDate, dateList, position)
                Log.d(TAG,holder.layoutPosition.toString())
            }
        }

# MyViewHolder #

        class MyViewHolder(private val view: View): RecyclerView.ViewHolder(view){

    fun setData(currentDate: String, dateList: List<Calendar>, position: Int){
        if(currentDate == dateList[position].get(Calendar.DATE).toString()){
            view?.str_day_element?.setTextColor(Color.WHITE)
            view?.num_day_element?.setTextColor(Color.WHITE)

            view?.str_day_element?.setBackgroundColor(ContextCompat.getColor(view.context,R.color.blue800))
            view?.num_day_element?.setBackgroundColor(ContextCompat.getColor(view.context,R.color.blue800))

        }
        else{
            view?.str_day_element?.setTextColor(ContextCompat.getColor(view.context,R.color.gray700))
            view?.num_day_element?.setTextColor(ContextCompat.getColor(view.context,R.color.gray700))

            view?.str_day_element?.setBackgroundColor(Color.WHITE)
            view?.num_day_element?.setBackgroundColor(Color.WHITE)
        }
        val dayOfWeekInMonth: Int = dateList[position].get(Calendar.DAY_OF_WEEK)
        var dayOfWeekInMonthStr = ""
        when(dayOfWeekInMonth-1) {
            1 -> dayOfWeekInMonthStr = "Пн"
            2 -> dayOfWeekInMonthStr = "Вт"
            3 -> dayOfWeekInMonthStr = "Ср"
            4 -> dayOfWeekInMonthStr = "Чт"
            5 -> dayOfWeekInMonthStr = "Пт"
            6 -> dayOfWeekInMonthStr = "Сб"
            0 -> dayOfWeekInMonthStr = "Вс"
        }
        view?.str_day_element?.text = dayOfWeekInMonthStr
        view?.num_day_element?.text = dateList[position].get(Calendar.DATE).toString()
    }

}

init single row calendar

fun InitLittleCalendar(){
        var date = Calendar.getInstance()
        val numDay = date.get(Calendar.DATE).toString()
        singleRowCalendar.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        singeRowCalendar.adapter = DateAdapter(GetDaysList(), numDay)
        singeRowCalendar.addOnScrollListener(CustomScrollListener())
    }

function for getting a list of dates of the month

fun GetDaysList(): List<Calendar> {

    val readOnlyView =  mutableListOf<Calendar>()
    val calendar = Calendar.getInstance()
    val days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
    var index = 0
    while(index < days){
        readOnlyView += getDaysPlus(index)
        index++
    }
    return readOnlyView
}

increment day

fun getDaysPlus(daysAgo: Int): Calendar {
    val calendar = Calendar.getInstance()
    calendar.set(Calendar.DAY_OF_MONTH, 1)
    calendar.add(Calendar.DAY_OF_MONTH, +daysAgo)

    return calendar
}

0 Answers0