Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HorizontalGridView/ RecyclerView scroll position resets once Picasso image loads

I have a HorizontalGridView and all is working well, however I am loading images with Picasso and whenever an image loads, the HorizontalGridView snaps back to the first item/starting scroll position. I also have a map fragment on the activity and notice that when the map is interacted with, the HorizontalGridView displays the the same behavior. Below is the adapter code. Please help, have been stuck on this one for a couple of days now...

public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{

private Context context;
private Deal[] mDeals;


public GridElementAdapter(Context context, Deal[] deals){
    this.context = context;
    this.mDeals = deals;

}

public static class SimpleViewHolder extends RecyclerView.ViewHolder {

    public final ImageView dealImage;
    public final TextView dealTitle;
    public final TextView dealSubtitle;
    public final TextView dealPrice;
    public final TextView dealTime;
    public final TextView dealDays;


    public SimpleViewHolder(View view) {
        super(view);

        dealImage = (ImageView) view.findViewById(R.id.gridDealImageView);
        dealTitle = (TextView) view.findViewById(R.id.gridTitleLabel);
        dealSubtitle = (TextView) view.findViewById(R.id.gridSubtitleLabel);
        dealPrice = (TextView) view.findViewById(R.id.gridPriceLabel);
        dealTime = (TextView) view.findViewById(R.id.gridAvailableTimeLabel);
        dealDays = (TextView) view.findViewById(R.id.gridAvailableDaysLabel);
    }
}

@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(this.context).inflate(R.layout.deal_grid_item, parent, false);
    return new SimpleViewHolder(view);
}

@Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {

    String dealImageUrl = "http://api.cheapeat.com.au/deals/" + mDeals[position].getPhotoId() + "/photo";

    Context context = holder.dealImage.getContext();
    Picasso.with(context).load(dealImageUrl).placeholder(R.drawable.deal_image_placeholder).into(holder.dealImage);
    holder.dealTitle.setText(mDeals[position].getName());
    holder.dealPrice.setText(mDeals[position].getPriceData());
    holder.dealSubtitle.setText(mDeals[position].getDescription());
    holder.dealTime.setText(mDeals[position].getAvailabilityTime());
    holder.dealDays.setText(mDeals[position].getFormattedAvailableDays(mDeals[position].getAvailabilityDay()));


}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemCount() {
    return this.mDeals.length;
}

}

like image 756
mossy321 Avatar asked Feb 02 '16 23:02

mossy321


1 Answers

So I finally managed to solve this one. Turns out I hadn't set the LayoutManager for the HorizontalGridView. Specifically, in the activity where I set the adapter:

// Had this
horizontalGridView = (HorizontalGridView) findViewById(R.id.gridView);
GridElementAdapter adapter = newGridElementAdapter(VenueProfileActivity.this, mDeals);
horizontalGridView.setAdapter(adapter);

// This needed to be added
HorizontalGridView.LayoutManager layoutManager = new LinearLayoutManager(VenueProfileActivity.this, LinearLayoutManager.HORIZONTAL, false);
horizontalGridView.setLayoutManager(layoutManager);
horizontalGridView.setHasFixedSize(true);

Hopefully this saves some headaches, as I've seen this question floating around with no correct solution yet.

like image 89
mossy321 Avatar answered Nov 15 '22 09:11

mossy321