I have a basically all in one layout which has everything needed for my app's main feed. All variable items (images, video thumbnails.. Etc.) This layout creates from fragment which have a list of items that contains articles from one site and from that fragment I pass URL from which I parse information in my newsDetailsParser. In some views where I don't have a lot of text the images isn't duplicates , but in others that have a lot of text every time I scroll the screen from top to bottom or backwards it duplicates. And duplicates only image , any other item like text , title , subTitle I see correctly. I don't know why this is happening and I need any help.
Maybe you have advise how to off updating(onScroll or onClick , I don't know which exactly) of this view after first open?
This image is article with some rows of text and when I scroll it all is ok
article with a few text after scrolling(all is ok)
But this happens when I try to scroll article with more text
Function from which I start this intent
adapter.setOnItemClickListener { item: Item<GroupieViewHolder>, view ->
val newsAdapter = item as NewsAdapter
val intent = Intent(view.context, NewsDetails::class.java)
intent.putExtra("refUrl", newsAdapter.getRef())
startActivity(view.context, intent, intent.extras)
}
Item class
class NewsDetailsItem(val title: String, var imgRef: String,
val subTitle: String, val text: String,
val videoRef: String, val detailsName: String,
val detailsString: String, val titleText : String) {
constructor() : this("","","","","", "","","")
}
Main class
class NewsDetails : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_activity_news_details)
var refUrl: String? = intent.extras?.getString("refUrl")
intent.extras?.clear()
val adapter = GroupAdapter<GroupieViewHolder>()
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", "", "", "", "", "", "", "")))
recycler_view_news_details.adapter = adapter
NewsDetailsParser.fetchData(refUrl, recycler_view_news_details)
}
}
Class Parser
class NewsDetailsParser {
companion object {
fun fetchData(refUrl: String?, recycler_view_news_details: RecyclerView) {
GlobalScope.launch {
try {
val document = Jsoup.connect(refUrl).get()
val adapter = GroupAdapter<GroupieViewHolder>()
val elementHeader = document.select("div[class=article-page__header]")
val elementPagePoster = document.select("div[class=article-page__poster]")
val elementLeftAside = document.getElementsByClass("staff__item")
var elementContent1 = document.select("div[class=introduction]")
elementContent1 = elementContent1.select("p")
val elementContent2 = document.select("div[class=article-page__article layout__content-col]")
println("ELEMENT 1 : $elementContent1")
//println("ELEMENT 2 : $elementContent2")
for(i in 0 until elementHeader.size) {
val title = elementHeader
.select("h1")
.eq(i)
.text()
val subTitle = elementHeader
.select("p")
.eq(i)
.text()
adapter.add(NewsDetailsAdapter(NewsDetailsItem(title, "", "", "", "", "", "", "")))
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", "", subTitle, "", "", "", "", "")))
}
for(i in 0 until elementPagePoster.size) {
var imgRef = elementPagePoster
.select("img")
.attr("srcset")
imgRef = imgRef.dropLast(3)
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", imgRef, "", "", "", "", "", "")))
}
for(i in 0 until elementLeftAside.size) {
var detailsName = elementLeftAside[i]
.select("span[class=position uppercase]")
.text()
var detailsString = elementLeftAside[i]
.select("span")
.not("span[class=position uppercase]")
.text()
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", "", "", "", "", detailsName, "", "")))
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", "", "", "", "", "", detailsString, "")))
}
for(i in 0 until elementContent1.size) {
val text = elementContent1
.select("p")
.eq(i)
.text()
adapter.add(NewsDetailsAdapter(NewsDetailsItem("", "", "", text, "", "", "", "")))
}
GlobalScope.launch(Dispatchers.Main) {
recycler_view_news_details.adapter = adapter
}
} catch (e : IOException) {
e.printStackTrace()
}
}
}
}
}
Class Adapter
class NewsDetailsAdapter(private val newsDetailsItem: NewsDetailsItem) : Item<GroupieViewHolder>() {
@SuppressLint("SetTextI18n")
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
//title
viewHolder.itemView.row_title_news_details_item.text = newsDetailsItem.title
//subTitle
viewHolder.itemView.row_subTitle_news_details_item.text = newsDetailsItem.subTitle
//image
if(newsDetailsItem.imgRef != "") {
Picasso.get().load(newsDetailsItem.imgRef).into(viewHolder.itemView.row_img_news_details_item)
}
//detailsName
viewHolder.itemView.row_left_aside_title_news_details_name_item.text = newsDetailsItem.detailsName
//detailsString
viewHolder.itemView.row_left_aside_title_news_details_string_item.text = newsDetailsItem.detailsString
//text
viewHolder.itemView.row_text_news_details_item.text = " ${newsDetailsItem.text}"
}
override fun getLayout(): Int {
return R.layout.news_details_item
}
}
xml layout of item :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/row_img_news_details_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:layout_marginTop="-15dp"/>
<TextView
android:id="@+id/row_title_news_details_item"
android:layout_width="wrap_content"
android:layout_marginTop="3dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="30sp"
android:textColor="@color/black"
android:textStyle="bold"
android:gravity="center"
android:textColorHighlight="@color/black"/>
<TextView
android:id="@+id/row_subTitle_news_details_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:gravity="center"
android:textColor="@color/black"
android:textColorHighlight="@color/black"
android:textStyle="italic"/>
<TextView
android:id="@+id/row_left_aside_title_news_details_name_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:text=""
android:layout_marginStart="20dp"
android:layout_marginTop="0dp"
/>
<TextView
android:id="@+id/row_left_aside_title_news_details_string_item"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="18sp"
android:textColor="@color/black"
android:text=""
android:layout_marginStart="20dp"
android:layout_marginTop="-6dp"
/>
<TextView
android:id="@+id/row_text_title_news_details_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text=""
android:textColor="@color/black"/>
<TextView
android:id="@+id/row_text_news_details_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:textSize="18sp"
android:textStyle="italic"
android:text=""
android:textColor="@color/black" />
</RelativeLayout>
Layout of this intent (xml) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:id="@+id/fragment_news_details">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_news_details"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
RecyclerView
is recycling viewholders as you scroll which means one ViewHolder will undergo multipleonBindViewHolder
calls. Following yourif(newsDetailsItem.imgRef != "") { /* setting up image with Picasso */}
you must have anelse
case that will set image to default/null. Right now you're leaking image of whatever item viewholder was bound to before.Picasso.get().load("google.com").into(viewHolder.itemView.row_img_news_details_item)
but I think that there is a much better variant of this code