Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Image gets replaced in recylerview while scrolling, android?

Posted on November 11, 2022 By

Solution 1 :

So the default image you specified in your XML layout is the ic_share, this means that when onBindViewHolder is called, the image gets substituted by:

.load(country.filePath!!.trim()).into(binding.ivFlag)

However, you never specified that at position 0, the icon must be ic_share, so because of RecyclerView’s nature, when you scroll downwards and upwards and the first itemHolder is created (again) it uses a recycled view from further down, and as you’re not setting ic_share to iv_flag at position 0 it just uses the recycled view image.

If you just add a line of code like @ADM suggested in your bind method like this:

if(adapterPosition==0){
            binding.ivFlag.setImageResource(R.drawable.ic_share)
        }

With the ic_share, I think that should make it work

Solution 2 :

That’s normal due to the recycling mechanism of views in RV/LV. To avoid that set it to null

 if (country.filePath != null)
     Glide.with(binding.root.context)
       .load(country.filePath!!.trim()).into(binding.ivFlag)
else
     binding.ivFlag.setImageDrawable(null)

assuming ivFlag is an ImageView, or a default/placeholder if you have it

Solution 3 :

This is happening because you never set ic_share during bind View.

inner class ViewHolder(val binding: ElementCountryBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(country: Data) {
        binding.data = country
        if(adapterPosition==0){
            binding.ivFlag.setImageResource(R.drawable.binding.ivFlag)
        }else {
            if (country.filePath != null)
                Glide.with(binding.root.context)
                    .load(country.filePath!!.trim()).into(binding.ivFlag)
        }
    }
}

Solution 4 :

Having the function getItemViewType also solves the problem

override fun getItemViewType(position: Int): Int {
    return position
}

Problem :

I am displaying a list of countries along with their flags in recylerview
The 1st element does not have a image and uses a default image which is visible on launch of page
But when I scroll and come back to it the image gets changed to some random from the list which should not happen

This is my adapter

class CountryAdapter(private val list: MutableList<Data?>?) :
RecyclerView.Adapter<CountryAdapter.ViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val binding = ElementCountryBinding.inflate(inflater)
    return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val country: Data? = list?.get(position)
    if (country != null) {
        holder.bind(country)
    }
    holder.itemView.setOnClickListener {

    }
}

override fun getItemCount(): Int = list!!.size

inner class ViewHolder(val binding: ElementCountryBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(country: Data) {
        binding.data = country
        if (country.filePath != null)
            Glide.with(binding.root.context)
            .load(country.filePath!!.trim()).into(binding.ivFlag)
    }
}
}

This is the xml layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto">

<data>


    <variable
        name="data"
        type="com.mountmeru.model.Data" />
</data>

<RelativeLayout
    android_layout_width="match_parent"
    android_layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android_id="@+id/main_cardview"
        android_layout_width="match_parent"
        android_layout_height="80dp"
        android_layout_marginBottom="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android_layout_width="match_parent"
            android_layout_height="match_parent">

            <androidx.appcompat.widget.AppCompatImageView
                android_id="@+id/iv_flag"
                android_layout_width="100dp"
                android_layout_height="70dp"
                android_layout_marginStart="10dp"
                android_adjustViewBounds="true"
                android_src="@drawable/ic_share"
                app_layout_constraintBottom_toBottomOf="parent"
                app_layout_constraintLeft_toLeftOf="parent"
                app_layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatTextView
                android_id="@+id/tvCountryName"
                android_layout_width="0dp"
                android_layout_height="wrap_content"
                android_layout_marginStart="10dp"
                android_text="@{data.countryName}"
                app_layout_constraintBottom_toBottomOf="@+id/iv_flag"
                app_layout_constraintLeft_toRightOf="@+id/iv_flag"
                app_layout_constraintTop_toTopOf="@+id/iv_flag" />
        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.cardview.widget.CardView>
    < /RelativeLayout>
 </layout>

screenshot

READ  [FIXED] kotlin - How to get a message sent from View Model using a sealed class?
Powered by Inline Related Posts

Without scrolling

After scrolling.

Android Tags:android, android-recyclerview, imageview, kotlin

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] android – How can I start a Fragment from within another Fragment that’s part of a viewPager? Android
[FIXED] android – Unable to navigate through bottom tabs in React-Native Android
[FIXED] Android not getting proper navigation bar height for different device it not match with actual height which visible in nav bar Android
[FIXED] java – android studio SharedPreferences doesn’t work Android
[FIXED] css – How to use @media (prefers-color-scheme) in responsive email in Gmail? Android
[FIXED] android – Bottom navigation view crashes app after I add a new item Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme