mxs2649
mxs2649

Reputation: 71

Images loaded with picasso in recycler view in android disappear when changing visibility of a view within the listitem

So i have a recycler view loaded with items containing text and image and a checkbox. All the data is loaded just fine initially including the images using picasso. The checkbox is initially hidden, once i tap a button, which is not part of the recycler view. The checkboxes should appear in all the items of recycler view. Everything works fine except for the fact that the images disappear once i do that.

The way i am doing it is that i set the adapter to my recyclerview to null and then initialize a new recycler view adapter which has checkboxes visible and set it to my recycler view.

The same thing happens if i try to search for a particular list item using a search bar on top. Any idea why? Here is the code about how i am setting the adapter to my recycler view.

  // set the adapter to null on cards list view.
  cards_list_view.setAdapter(null);

 // Initialize the adapter again.
 deckCardRecycleAdapter = new DeckCardRecycleAdapter(getActivity(), cards_list, show_checkbox);

// Set the adapter again to list view.                          
 cards_list_view.setAdapter(deckCardRecycleAdapter);

And here is my onBindViewHolder function in adapter.

 @Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

    typeface = Typeface.createFromAsset(context.getAssets(), "fonts/futura-medium.otf");

    viewHolder.user_name.setTypeface(typeface);
    viewHolder.user_name.setText(cards.get(position).getName().toString());

    viewHolder.position.setTypeface(typeface);
    viewHolder.position.setText(cards.get(position).getPosition().toString());

    viewHolder.dot.setTypeface(typeface);

    viewHolder.event_saved.setTypeface(typeface);

    if(cards.get(position).getEvent_name().toString().equals("None")) {
        viewHolder.event_saved.setVisibility(View.GONE);
        viewHolder.dot.setVisibility(View.GONE);
    }
    else
        viewHolder.event_saved.setText(cards.get(position).getEvent_name().toString());

    viewHolder.date_saved.setTypeface(typeface);
    String timestamp = cards.get(position).getTimestamp().toString();
    viewHolder.date_saved.setText(toDate(timestamp));


    if(cards.get(position).image_url.toString().equals("None"))
        viewHolder.user_image.setImageResource(R.drawable.anonymous);
    else {

        String url_image = cards.get(position).image_url.toString();
        Picasso.with(this.context).cancelRequest(viewHolder.user_image);
        Picasso.with(context).load(url_image).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

                viewHolder.user_image.setImageBitmap(bitmap);
                cards.get(position).setImage_url(getStringImage(bitmap));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

    }

Please help. Thanks in advance.

Upvotes: 0

Views: 409

Answers (1)

Alireza Sharifi
Alireza Sharifi

Reputation: 1162

I don't think it's a good idea to reload data each time, it's make a lot of performance instead you can create interface and let the item listening to it, and just with one line of code you can change the visibility

Check this answer

interface Listener { 
public void onVisibilityChanged(bool visible); 
}
  • create an interface (set bool parameter for visible/invisible)

  • Create separate class for manage listener's (like this)

  • add listener for each item (make checkbox visible or invisible)

  • remove listener when item removed (avoid NullpointException)

  • clear listener's if recycler reset (avoid NullpointException)

  • call the method that have foreach and all the listeners will be triged

Upvotes: 0

Related Questions