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
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.