HorizontalGridView / RecyclerView scroll position shift after loading Picasso image

I have a HorizontalGridView and everything works fine, however I load images using Picasso and whenever the image loads, the HorizontalGridView is snapped to the first position of the position / start of scrolling. I also have an activity map fragment, and note that when the map interacts, the HorizontalGridView displays the same behavior. Below is the adapter code. Please help, stuck with this for a couple of days ...

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; } 

}

+5
source share
1 answer

So, I finally managed to solve this problem. Turns out I didn't set the LayoutManager for the HorizontalGridView . In particular, in the case when I install 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); 

I hope this saves you some headaches, as I saw this question pop up without the right solution.

+4
source

Source: https://habr.com/ru/post/1242137/


All Articles