I have a recyclerview that is populated from an array that contains images of cars and their marks through the Glide library. Below each image I placed a textview that is where the brand of each car appears, it turns out that when running the application it locates the marks corresponding to each image, but when you scroll and change the view of the recyclerview the marks no longer correspond to each auto and when returning with scroll upwards the concordance of each image with its respective textview is also lost. I suppose it is in the assignment of the textview with the respective item of the arrangement, but I have searched for it anyway and I can not find the problem. It will be possible for some of you to give me a hand, I will be very grateful. I leave the related layouts and code. Thank you very much in advance.
Activity Main:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
ImageGalleryAdapter adapter = new ImageGalleryAdapter(this, SpacePhoto.getSpacePhotos());
recyclerView.setAdapter(adapter);
}
private class ImageGalleryAdapter extends RecyclerView.Adapter<ImageGalleryAdapter.MyViewHolder> {
private TextView nombreCoche;
@Override
public ImageGalleryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View photoView = inflater.inflate(R.layout.item_photo, parent, false);
ImageGalleryAdapter.MyViewHolder viewHolder = new ImageGalleryAdapter.MyViewHolder(photoView);
nombreCoche = (TextView) photoView.findViewById(R.id.nombre_coche);
return viewHolder;
}
@Override
public void onBindViewHolder(ImageGalleryAdapter.MyViewHolder holder, int position) {
SpacePhoto spacePhoto = mSpacePhotos[position];
ImageView imageView = holder.mPhotoImageView;
final SpacePhoto item = getItem(position);
Glide.with(mContext)
.load(spacePhoto.getIdDrawable())
//.placeholder(R.drawable.ic_cloud_off_red)
.into(imageView);
nombreCoche.setText(item.getNombre());
}
public SpacePhoto getItem(int position) {
return (SpacePhoto.getSpacePhotos()[position]);
}
@Override
public int getItemCount() {
return (mSpacePhotos.length);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView mPhotoImageView;
public MyViewHolder(View itemView) {
super(itemView);
mPhotoImageView = (ImageView) itemView.findViewById(R.id.iv_photo);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION) {
SpacePhoto spacePhoto = mSpacePhotos[position];
Intent intent = new Intent(mContext, SpacePhotoActivity.class);
intent.putExtra(SpacePhotoActivity.EXTRA_SPACE_PHOTO, spacePhoto);
startActivity(intent);
}
}
}
private SpacePhoto[] mSpacePhotos;
private Context mContext;
public ImageGalleryAdapter(Context context, SpacePhoto[] spacePhotos) {
mContext = context;
mSpacePhotos = spacePhotos;
}
}
}
Data source:
public class SpacePhoto implements Parcelable {
private String nombre;
private int idDrawable;
public SpacePhoto(String nombre, int idDrawable) {
this.nombre = nombre;
this.idDrawable = idDrawable;
}
protected SpacePhoto(Parcel in) {
nombre = in.readString();
idDrawable = in.readInt();
}
public static final Creator<SpacePhoto> CREATOR = new Creator<SpacePhoto>() {
@Override
public SpacePhoto createFromParcel(Parcel in) {
return new SpacePhoto(in);
}
@Override
public SpacePhoto[] newArray(int size) {
return new SpacePhoto[size];
}
};
public String getNombre() {
return nombre;
}
public int getIdDrawable() {
return idDrawable;
}
public int getId() {
return nombre.hashCode();
}
public static SpacePhoto[] getSpacePhotos() {
return new SpacePhoto[]{
new SpacePhoto("Jaguar F-Type 2015", R.drawable.jaguar_f_type_2015),
new SpacePhoto("Mercedes AMG-GT", R.drawable.mercedes_benz_amg_gt),
new SpacePhoto("Mazda MX-5", R.drawable.mazda_mx5_2015),
new SpacePhoto("Porsche 911 GTS", R.drawable.porsche_911_gts),
new SpacePhoto("BMW Serie 6", R.drawable.bmw_serie6_cabrio_2015),
new SpacePhoto("Ford Mondeo", R.drawable.ford_mondeo),
new SpacePhoto("Volvo V60 Cross Country", R.drawable.volvo_v60_crosscountry),
new SpacePhoto("Jaguar XE", R.drawable.jaguar_xe),
new SpacePhoto("VW Golf R Variant", R.drawable.volkswagen_golf_r_variant_2015),
new SpacePhoto("Seat León ST Cupra", R.drawable.seat_leon_st_cupra),
new SpacePhoto("Aston Martin One-77", R.drawable.aston_martin_one_77),
new SpacePhoto("Bugatti Chiron", R.drawable.bugatti_chiron),
new SpacePhoto("Ferrari F60 América", R.drawable.ferrari_f60_america),
new SpacePhoto("Koenigsegg CCXR Trevita", R.drawable.koenigsegg_ccxr_trevita),
new SpacePhoto("Koenigsegg One", R.drawable.koenigsegg_one),
new SpacePhoto("Koenigsegg Regera", R.drawable.koenigsegg_regera),
new SpacePhoto("Lamborghini Veneno", R.drawable.lamborghini_veneno),
new SpacePhoto("Lykan Hypersport", R.drawable.lykan_hypersport),
new SpacePhoto("Maybach Exelero", R.drawable.maybach_exelero),
new SpacePhoto("Pagani Huayra_BC", R.drawable.pagani_huayra_bc),
};
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(nombre);
parcel.writeInt(idDrawable);
}
public static SpacePhoto getItem(int id) {
for (SpacePhoto item : getSpacePhotos()) {
if (item.getId() == id) {
return item;
}
}
return null;
}
}
Layout Activity Main:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Layout of the detail of the RecyclerView:
<ImageView
android:id="@+id/iv_photo"
android:adjustViewBounds="true"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:layout_margin="0dp"
android:layout_width="match_parent"/>
<TextView
android:id="@+id/nombre_coche"
android:layout_width="match_parent"
android:layout_height="33dp"
android:layout_gravity="bottom"
android:maxLines="1"
android:padding="0dp"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="@android:color/black" />