2

i am developing this app where i have used bottom navigation view. on my home fragment i have used two recycler views.i am fetchiing some data and images to display on home screen fragment. on start of the app the images are visible in both the recycler views but if i rotate the screen or navigate to other fragment the images are gone from the second recycler view while first recycler view is still showing images.

below is the code

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) { 
 mFavSubCatDatabase = 
 FirebaseDatabase.getInstance().getReference().child("FavSubCat");
    mFavSubCatDatabase.keepSynced(true);
    mFavCatDatabase = 
 FirebaseDatabase.getInstance().getReference().child("FavCat");
    mFavCatDatabase.keepSynced(true);
    //    mLayoutManager = new LinearLayoutManager(getContext());
    RecyclerView.LayoutManager mLayoutManager = new 
 GridLayoutManager(getContext(), 3);
    mFavSubCatList = (RecyclerView) 
    view.findViewById(R.id.HomesubcatOpeningRecler);
    mFavSubCatList.setHasFixedSize(true);
    mFavSubCatList.setLayoutManager(mLayoutManager);
    LinearLayoutManager mLayoutManager2
            = new LinearLayoutManager(getContext(), 
     LinearLayoutManager.HORIZONTAL, false);
    mFavCatList = (RecyclerView) 
    view.findViewById(R.id.HomeCatRecyclerOpening);
    mFavCatList.setHasFixedSize(true);
    mFavCatList.setLayoutManager(mLayoutManager2);
    return view;
   }


@Override
public void onStart() {
    super.onStart();
    startListening();

  }


      public void startListening()
    {



    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("FavCat")
            .limitToLast(50);
    FirebaseRecyclerOptions<HomeCatitems> options =
            new FirebaseRecyclerOptions.Builder<HomeCatitems>()
                    .setQuery(query, HomeCatitems.class)
                    .build();
    FirebaseRecyclerAdapter adapter = new 
    FirebaseRecyclerAdapter<HomeCatitems, CategoryViewHolder>(options) {
        @Override
        public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int 
    viewType) {              
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_cat_layout, parent, false);
            return new CategoryViewHolder(view);
        }
        @Override
        protected void onBindViewHolder(final CategoryViewHolder holder, int 
     position, final HomeCatitems model) {
            holder.setDisplayName(model.getName());
            holder.setUserImage(model.getImageUrl(), getContext());

        }
    };
    mFavCatList.setAdapter(adapter);
    adapter.startListening();






    Query query1 = FirebaseDatabase.getInstance()
            .getReference()
            .child("FavSubCat")
            .limitToLast(50);

    FirebaseRecyclerOptions<CategoryItems> options1 =
            new FirebaseRecyclerOptions.Builder<CategoryItems>()
                    .setQuery(query1, CategoryItems.class)
                    .build();



    FirebaseRecyclerAdapter adaptersubcat = new 
    FirebaseRecyclerAdapter<CategoryItems, SubCategoryViewHolder>(options1)  
    {
        @Override
        public SubCategoryViewHolder onCreateViewHolder(ViewGroup parent, 
    int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.home_subcat_items, parent, false);
            return new SubCategoryViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(final SubCategoryViewHolder holder, 
     int position, final CategoryItems model) {

            holder.setDisplayName(model.getName());
            holder.setUserImage(model.getImageUrl(), getContext());


        }
    };
    mFavSubCatList.setAdapter(adaptersubcat);
    adaptersubcat.startListening();
    }

one of the inner viewHolder class is given below public static class SubCategoryViewHolder extends RecyclerView.ViewHolder {

    View mView;
    ImageView subcategoryImageView;
    TextView subcategoryName;

    public SubCategoryViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

    }

    public void setDisplayName(String name){

        subcategoryName = (TextView) 
    mView.findViewById(R.id.home_subcat_name);
        subcategoryName.setText(name);


    }


    public void setUserImage(final String thumb_image, final Context ctx){

        subcategoryImageView = (ImageView) 
   mView.findViewById(R.id.home_subcat_image);
        Picasso.with(ctx).setIndicatorsEnabled(false);

    Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE)

    .placeholder(R.drawable.default_avatar).into(subcategoryImageView, new 
    Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {



 Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar)
       .into(subcategoryImageView);

            }
        });

    }



}
Jimmy
  • 87
  • 2
  • 9
  • remove your second question and add it as a separate question. – Alan May 19 '18 at 13:20
  • @Alan ok, but can you please solve the first one – Jimmy May 19 '18 at 13:55
  • This seems either a problem with the `ViewHolder` implementation or with caching. Can you post the implementations of your `VewHolder`s. – Eduardo Naveda May 19 '18 at 16:04
  • @Eddnav please see the first code above i have used the onCreateViewHolder and onBindViewHolder inside the startListening mathod in that code. – Jimmy May 19 '18 at 16:20
  • @Alan can you please look at this please https://stackoverflow.com/questions/50592252/view-setparams-not-working-properly-in-firebase-recycler-view – Jimmy May 30 '18 at 21:51
  • @Eddnav can you please help me with this one https://stackoverflow.com/questions/50592252/view-setparams-not-working-properly-in-firebase-recycler-view – Jimmy May 30 '18 at 21:51

3 Answers3

0

Try using onResume method to set the Image into imageview again after rotation. Or just use onSavedInstanceState and reassign the image into imageview as answered in this question : ImageView not retaining Image when Screen rotation occurs

saikrishna279
  • 135
  • 1
  • 10
  • can you please explain it a bit more because i am using a inner model class where i am assigning the image to imageView which is later shown in the recycler view and why is the problem occuring with only one fragment and not the other one. After navigating to other fragment and coming back to home , only one fragment is showing the images in home fragment and not the other one. – Jimmy May 19 '18 at 15:20
  • can you please look at this one also https://stackoverflow.com/questions/50592252/duplicate-values-in-recycler-view – Jimmy May 30 '18 at 10:32
0

As I mentioned in my comment, this seems to be related to either the ViewHolder implementations or Picasso's caching. The most suspicious bit of your code is:

Picasso.with(ctx).load(thumb_image).networkPolicy(NetworkPolicy.OFFLINE);

You might want to remove the network policy declaration there, is there a reason for you to be using that callback on error to load your image in the first place? Picasso will do this for you automatically (fetching from memory/disk cache, if not available from network). Try removing this and load the images using only:

Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(subcategoryImageView);

EDIT: also, here are the docs for NetworkPolicy in case you need more info: https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/NetworkPolicy.html.

Eduardo Naveda
  • 1,880
  • 3
  • 15
  • 30
  • i tried using only Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(subcategoryImageView); but it still not working . the reason why i was using NetworkPolicy.Offline was because i wanted to load the images offline first and if the image is not available then from the network. The code is working fine else where even with the first recycler view that i have used in the same code. the problem lies with the second one only after navigation its not displaying anything , the whole recycler view is gone – Jimmy May 20 '18 at 07:11
  • Yes, that's what I'm telling you, you don't need the callback to do that, Picasso handles that automatically. As for why it's crashing on navigation then, I advise you to isolate the issue in debugging, add some background to the recycler view, see if it's actually there on navigation, maybe if there's a problem with the `Fragment` replacements. – Eduardo Naveda May 20 '18 at 18:14
  • i tried giving recycler view a color background and you were right its not there after navigation. can you please tell me the issue why this is happening – Jimmy May 20 '18 at 18:25
  • can you please help me a little on this one also https://stackoverflow.com/questions/50433123/how-to-create-a-mapview-with-filter-categories-using-geofire – Jimmy May 21 '18 at 17:07
0

After Searching a lot I found that I was using setHasFixedSize(true) with wrap_content of recycler view. I removed the setHasFixedSize(true) and it works perfectly fine now.

Jimmy
  • 87
  • 2
  • 9