Android RecyclerView StaggeredGrid items change position when scrolling top

后端 未结 2 1923
挽巷
挽巷 2020-12-11 10:18

In my Staggered Grid with 2 columns I use the effect of infinite list to automatically load elements 10 by 10, everything works fine except when I scroll up. The first two e

相关标签:
2条回答
  • 2020-12-11 10:47

    This is a feature. Reordering and repositioning view for a seamless tream of items.

    Use setGapStrategy(int gapStrategy)

    Sets the gap handling strategy for StaggeredGridLayoutManager. If the gapStrategy parameter is different than the current strategy, calling this method will trigger a layout request.

    You will probably want GAP_HANDLING_NONE.

    Does not do anything to hide gaps.

    0 讨论(0)
  • 2020-12-11 11:05

    This happens because the holder dose not recognize the width and height of the Image view when you scroll up and down. It clears the upper view when you scroll down and vice versa.

    Use like this :

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
    
    MyViewHolder vh = (MyViewHolder) viewHolder;
    ImageModel item = imageModels.get(position);
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams)vh.imageView.getLayoutParams();
    float ratio = item.getHeight() / item.getWidth();
    rlp.height = (int) (rlp.width * ratio);
    vh.imageView.setLayoutParams(rlp);
    vh.positionTextView.setText("pos: " + position);
    vh.imageView.setRatio(item.getRatio());
    Picasso.with(mContext).load(item.getUrl()).
    placeholder(PlaceHolderDrawableHelper.getBackgroundDrawable(position)).
    into(vh.imageView);
    }
    

    For clear see this link: Picasso/Glide-RecyclerView-StaggeredGridLayoutManager

    0 讨论(0)
提交回复
热议问题