我有一个Horizo​​ntalGridView,并且一切正常,但是我正在用Picasso加载图像,并且每当加载图像时,Horizo​​ntalGridView都会捕捉回到第一项/开始滚动位置。我在 Activity 上还有一个地图片段,请注意,当与地图进行交互时,Horizo​​ntalGridView会显示相同的行为。以下是适配器代码。请帮助,已经被困在这上了几天了...

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

}

最佳答案

所以我终于设法解决了这一问题。原来我没有为LayoutManager设置HorizontalGridView。具体来说,在设置适配器的 Activity 中:

// 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);

希望这可以减轻一些麻烦,因为我已经看到这个问题四处徘徊,而目前尚无正确的解决方案。

07-24 19:10