0

I have created a Firebase RecyclerView which contains a CardView. Inside the CardView there is an ImageView and a TextView. If an image is uploaded along with its descriptions (TextView) then both ImageView and TextView are visible, else only the TextView will be visible. The problem is, when the RecyclerView contains more than 11 items, the ImageViews disappear automatically and only the TextViews are visible. Not sure why this is happening. Below is the code:

public class MainView extends 
    AppCompatActivity {
    private RecyclerView mPlacelist; protected void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState); mPlacelist= (RecyclerView) findViewById(R.id.Place_list); mPlacelist.setHasFixedSize(true);

LinearLayoutManager mLayoutManager=new LinearLayoutManager(MainView.this); mLayoutManager.setReverseLayout(true); mLayoutManager.setStackFromEnd(true); mPlacelist.setLayoutManager(mLayoutManager); } public static class  MainViewHolder extends RecyclerView.ViewHolder {
    View mView;


    public MainViewHolder(View itemView,int viewType) {
        super(itemView);
        mView = itemView;


    }
    public void setDescription(String description) {
        TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
        post_desc.setText(description);
    }

    public void setImage(final Context ctx, final String image) {
        ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
        //If image exist
        if (image != null) {

            Picasso.with(ctx).load(image).fit().centerInside().into(post_image);

        } else if (image == null) {

           //no image then imageview invisible
            Picasso.with(ctx).cancelRequest(post_image);
            post_image.setVisibility(View.INVISIBLE);
            post_image.setVisibility(View.GONE);

        }
    }

   }

final FirebaseRecyclerAdapter<MainBlogView, MainView.MainViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MainBlogView, MainView.MainViewHolder>(
        MainBlogView.class, R.layout.recyclerplace, MainView.MainViewHolder.class, databaseReference ) {
    @Override
    protected void populateViewHolder(final MainView.MainViewHolder viewHolder, MainBlogView model, int position) {





       viewHolder.setDescription(model.getDescription());
        viewHolder.setImage(getApplicationContext(), model.getImage());
        viewHolder.setTimestamp(model.getTimestamp());

}  @Override
        public int getItemViewType(int position) {

            return super.getItemViewType(position);
        }

    };
    firebaseRecyclerAdapter.notifyDataSetChanged();
    mPlacelist.setAdapter(firebaseRecyclerAdapter);



} }

Any help is appreciated. Thank you

ymutlu
  • 6,585
  • 4
  • 35
  • 47
yaha kaha
  • 65
  • 4
  • Have you tried to comment this line of code `mPlacelist.setHasFixedSize(true);`? – Alex Mamo May 30 '18 at 13:31
  • Yes i did.. but doing so removes the imageview altogether irrespective of its position. @Alex Mamo – yaha kaha May 30 '18 at 13:34
  • What happens when you remove or adapt the following lines `Picasso.with(ctx).cancelRequest(post_image); post_image.setVisibility(View.INVISIBLE); post_image.setVisibility(View.GONE);`? My guess is that the 12th image is in fact null and then your code in that block is making all future image views invisible? – Benjamin Scholtz May 30 '18 at 13:35
  • @YodaScholtz if i remove these lines then i will get an empty space for imageview since there is no image in it. – yaha kaha May 30 '18 at 13:39
  • @YodaScholtz if this is the case how to tackle this wierd situation? – yaha kaha May 30 '18 at 13:41
  • The below answer I think might be the correct one. – Benjamin Scholtz May 30 '18 at 13:43

1 Answers1

4

Instead of this code:

if (image != null) {

        Picasso.with(ctx).load(image).fit().centerInside().into(post_image);
}

Use this:

if (image != null) {

        post_image.setVisibility(View.VISIBLE);
Picasso.with(ctx).load(image).fit().centerInside().into(post_image);

    }

You got this "error" because your 12th row hasn't image and your code hide it, and the RecyclerView reuse the rows for performance.

just
  • 1,900
  • 4
  • 25
  • 46