在recycleView中获取具有当前日期的商品(没有OnClick)

时间:2019-10-18 09:12:43

标签: android kotlin

我将日历排成一行。我正在使用recyclerview。如何跟踪当前日期的位置以将其显示在屏幕中央? 现在显示每月的第一天

  

这就是现在显示的内容

Calendar stating from 1st date

  

下面是所需的

Need to start from current date range

import sqlite3  
import click
from flask import current_app, g
from flask.cli import with_appcontext

def init_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

def init_db():
    db = get_db()

    with current_app.open_resource('schema.sql') as d:
        db.executescript(d.read().decode('utf8'))

@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')


def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db

def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()  

#MyViewHolder#

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

初始化单行日历

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

}

用于获取月份日期列表的功能

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

增加日

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
}

0 个答案:

没有答案