I have a recycler view which loads an image and a text. When the image is loaded from the url though picasso other elements is the recycler view loose their text. The elements who loose their text they dont load their image from www but from android drawable folder. All the images are the same size and i tried it with the same image and it still happens. If you could provide any insights because i dont know what to do or try.
Here is a gif of my problem: https://giphy.com/gifs/hR5w7IrCZ14T9M8o7A
--- Edit ----
Tried the answer from Radesh didnt work. Tried glide instead of picasso (seems glide to be much faster). The problem seems to be the way recycler updates.
My onBindViewHolder
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
myViewHolder.textItem.setText(categoryItem.get(i).getEng_name());
if (categoryItem.get(i).getDefault_image().equals("empty") || categoryItem.get(i).getDefault_image().length() == 0) {
Picasso.get().load(R.drawable.no_image).into(myViewHolder.image);
} else {
Glide.with(myViewHolder.context).load("https://i.ibb.co/wwT602t/lake-plastira.jpg").into(myViewHolder.image);
//Picasso.get().load("https://i.ibb.co/wwT602t/lake-plastira.jpg").placeholder(R.drawable.loading_image).error(R.drawable.failed_to_load).into(myViewHolder.image);
}
}
My xml for the items loaded
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="?attr/color4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="@drawable/no_image"
android:scaleType="fitStart" />
<TextView
android:id="@+id/item_name"
android:textColor="?attr/color6"
android:textSize="20sp"
android:gravity="center"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v7.widget.CardView>
My recyclerView code
recyclerView = (RecyclerView) findViewById(R.id.categories_rec);
recyclerView.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new SpecificCategoryAdapter(categoryItem);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
So I find a solution I used a runnable and the problem was solved. If you have any other idea please post it. I believe that this behaviour happened because that function never ended because it was waiting for the image.
myViewHolder.textItem.setText(categoryItem.get(i).getEng_name());
if (categoryItem.get(i).getDefault_image().equals("empty") || categoryItem.get(i).getDefault_image().length() == 0) {
Picasso.get().load(R.drawable.no_image).into(myViewHolder.image);
} else {
final Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
Glide.with(myViewHolder.context).load("https://i.ibb.co/wwT602t/lake-plastira.jpg").into(myViewHolder.image);
}
};
handler.postDelayed(r, 1000);
}