简体   繁体   中英

How to get position of item in RecyclerView Adapter

In my application I want create custom calendar with RecyclerView and for this I write below codes.
I want set black color for today and next days and set gray color for previous days.
I can know which day is today, but I don't know how can I get today's position in adapter for set gray color!
My Adapter color:

class DaysAdapter constructor(val context: Context, private val items: MutableList<DayEntity>) :
    RecyclerView.Adapter<DaysAdapter.ViewHolder>() {

    private val TYPE_HEADER = 0
    private val TYPE_DAY = 1
    private var firstDayDayOfWeek = 0
    private var totalDays = 0

    init {
        firstDayDayOfWeek = items[0].dayOfWeek
        totalDays = items.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return ViewHolder(inflater.inflate(R.layout.item_day_new, parent, false))
    }

    override fun getItemCount() = 7 * 7

    override fun getItemViewType(position: Int): Int {
        return if (isPositionHeader(position)) {
            TYPE_HEADER
        } else {
            TYPE_DAY
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(position)

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val dayTxt = itemView.findViewById(R.id.dayTxt) as TextView

        //Bind data
        fun bind(pos: Int) {
            var position = pos
            position += 6 - (position % 7) * 2

            if (totalDays < position - 6 - firstDayDayOfWeek) {
                return
            } else {
                //Day
                if (!isPositionHeader(position)) {
                    if (position - 7 - firstDayDayOfWeek >= 0) {
                        val day = items[position - 7 - firstDayDayOfWeek]
                        dayTxt.isVisible = true
                        dayTxt.text = day.num
                        items[position].dayOfWeek
                        //Today
                        if (position < day.isToday) {
                            dayTxt.setTextColor(ContextCompat.getColor(context, R.color.gray))
                        } else {
                            dayTxt.setTextColor(ContextCompat.getColor(context, R.color.black))
                        }
                    } else {
                        dayTxt.isVisible = false
                    }
                } else {
                    //Header
                    dayTxt.text = Constants.FIRST_CHAR_OF_DAYS_OF_WEEK_NAME[position]
                }
            }
        }
    }

    private fun isPositionHeader(position: Int) = position < 7
}

day.isToday is boolean and but I don't how can I completed this code if (position < day.isToday) for set gray color.
day.isToday is Boolean and I can't used it in if (position < day.isToday) .

day.isToday is Boolean and I can't used it in if (position < day.isToday) .
How can I it?

Update You can use the Calendar class to get the current date and then compare it with the date of the days that you want to color. Based on the comparison you can set the color of the days.

val calendar = Calendar.getInstance()
val currentDay = calendar.get(Calendar.DAY_OF_MONTH)

if (dayOfMonth < currentDay) {
// Previous days
textView.setTextColor(Color.GRAY)
} else {
// Today and next days
textView.setTextColor(Color.BLACK)
 }

OLD you can try this where you are comparing position with day.IsToday in bind() method just simply try the below code

 inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val dayTxt = itemView.findViewById(R.id.dayTxt) as TextView

    //Bind data
    fun bind(pos: Int) {
        var position = pos
        position += 6 - (position % 7) * 2

        if (totalDays < position - 6 - firstDayDayOfWeek) {
            return
        } else {
            //Day
            if (!isPositionHeader(position)) {
                if (position - 7 - firstDayDayOfWeek >= 0) {
                    val day = items[position - 7 - firstDayDayOfWeek]
                    dayTxt.isVisible = true
                    dayTxt.text = day.num
                    items[position].dayOfWeek
                    //Today
                    if (items[position].isToday) {
                        dayTxt.setTextColor(ContextCompat.getColor(context, R.color.gray))
                    } else {
                        dayTxt.setTextColor(ContextCompat.getColor(context, R.color.black))
                    }
                } else {
                    dayTxt.isVisible = false
                }
            } else {
                //Header
                dayTxt.text = 
        Constants.FIRST_CHAR_OF_DAYS_OF_WEEK_NAME[position]
            }
        }
    }
}

I am assuming

items[position].isToday

this function will identify your date is today or not.

Now simply get today position and store it some where globally like

Int todayPosition=-1 // note take it globally, where it will not over-right every time.  

if(items[position].isToday){
   todayPosition=position 
} 

Now you have to just compare position

if(position==todayPosition){
//Today colour
}else if(position <todayPosition){
//Past colour
}else{
//Future colour
}

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM