N G
N G

Reputation: 55

Replace Recyclerview's textview item to imageview item

I have RecyclerViewwith TextView items and want to replace the last item to ImageView. The numbers must be text, that's why I can't make all them ImageView It mmust be like this: enter image description here

I add TextView and ImageView to recycler row item and make the visibility of ImageView gone. Here is my ViewHolder

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

        holder.testName?.text = listOfTests?.get(position)?.testName
        holder.txtNumberIcon?.text = listOfTests?.get(position)?.txtNumberIcon

        if (listOfTests?.get(listOfTests!!.size.minus(1))?.txtNumberIcon == dont know what condition must be here) {


            holder.txtNumberIcon?.visibility = View.GONE
            holder.imageStarIcon?.visibility = View.VISIBLE

            Glide
                .with(context?.applicationContext!!)
                .load(listOfTests?.get(listOfTests!!.size.minus(1))?.testNumberIcon)
                .into(holder.imageStarIcon!!)

        }

What condition I must write to replace last item from textview to imageview?

Upvotes: 0

Views: 49

Answers (1)

Miruna Radu
Miruna Radu

Reputation: 452

Let me see if I understand it right. If the listOfTests has 10 elements you want 9 cells from the RecyclerView to be TextView's and 1 ImageView.

If so:

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

    holder.testName?.text = listOfTests?.get(position)?.testName
    holder.txtNumberIcon?.text = listOfTests?.get(position)?.txtNumberIcon

    if (position == listOfTests.size - 1) { /* position starts from 0 */
        // I would suggest to load the image only for the element that will be displayed
        Glide.with(context?.applicationContext!!)
              .load(listOfTests?.get(position)?.testNumberIcon)
              .into(holder.imageStarIcon!!)
        holder.txtNumberIcon?.visibility = View.GONE
        holder.imageStarIcon?.visibility = View.VISIBLE
    }
    else {
        holder.txtNumberIcon?.visibility = View.VISIBLE
        holder.imageStarIcon?.visibility = View.GONE
    }
}

Upvotes: 2

Related Questions