'Weird recyclerview refreshing issue

I'm trying to create a file explorer synchronized with an FTP explorer, one of its features is to notify the new downloaded files. For that I store in Sharedpreferences the path of my file/folders and then I compare them with the new element stored in sharedprefs in my adapter, if my condition is true, I make a dot appear next to the folder/file name. It works really great except when I update my recyclerview as I switch back to the previous activity with notifyDataSetChanged() to update the file/folder if they have been read, or when I sort my list with notifyItemRangeChanged(). As you can see in my GIF, dots are appearing randomly everywhere in bad locations. I hope I explained well, how can I fix this issue? I'm really close, it's very frustrating.

Here's my onResume() :

    override fun onResume() {
    super.onResume()
    if(::recyclerViewRecents.isInitialized) {
        getRecentFiles()

        recyclerViewFolders.adapter?.notifyDataSetChanged()
        recyclerViewRecents.adapter?.notifyDataSetChanged()
    }
    }

And here my notifydatasetchange, I even try to clear the recycledViewPool but nothing change:

        recyclerViewFiles.adapter?.notifyItemRangeChanged(0,filesList.size)

Thanks

Edit:

Here's is my adapter code:

I check in onBindViewHolder if the file is registered to display the dot:

        val isRead: String? = prefNewFile.getString(currentFile.absolutePath, null)

    if (isRead!=null) {
        holder.fileIsRead.visibility = View.VISIBLE
        println(currentFile.name + " is unread")
    }

And here how I mark it as read:

private fun putAsRead(file: String) {
    println("$file read!")
    val editor: SharedPreferences.Editor = prefNewFile.edit()
    editor.remove(file)
    editor.apply()
    val folder = file.substring(0,file.lastIndexOf("/"))
    if(file.substring(file.lastIndexOf("/")) != "/CalvezDocs")
        putAsRead(folder)
}

demo



Solution 1:[1]

The solution is just to add else option on Visibility. For example, holder.fileIsRead.isVisible = isRead == true or isRead != null It's short version of if (isRead != null ) View.VISIBLE else View.GONE

The reason of the problem is that RecyclerView reuses ViewHolders, when the adapter reuses a ViewHolder with holder.fileIsRead was set to View.VISIBLE previously, so you have extra item on your list.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jake