使用StaggeredGridLayoutManager时如何停止项目移动

10

我在安卓中使用带有StaggredGridLayoutManager的RecyclerView。问题是,有时候当滚动时,项目会移动以适应屏幕大小。通常情况下这不是什么大问题,但在我的情况下它搞砸了一切! 那么有没有办法停止这种行为?不仅仅是动画,还有整个项目重新排列的东西。 谢谢


你尝试过使用 mSGLM.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); 吗? - MojoTosh
重复的https://dev59.com/BF4c5IYBdhLWcg3wssFs。 - Bitcoin Cash - ADA enthusiast
4个回答

2
使用Randy的建议中提到的ImageView,你可以使用Glide完成同样的操作:
  Glide.with(context)
            .load(imageURL)
            .asBitmap()
            .dontAnimate()
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {

                    if (bitmap != null)
                    {
                        holder.image.setHeightRatio(bitmap.getHeight()/bitmap.getWidth());
                        holder.image.setImageBitmap(bitmap);
                    }
                }
            });

2
如果您正在使用Picasso,则首先创建自定义ImageView。
public class DynamicHeightImageView extends ImageView  {

private double mHeightRatio;

public DynamicHeightImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public DynamicHeightImageView(Context context) {
    super(context);
}

//Here we will set the aspect ratio
public void setHeightRatio(double ratio) {
    if (ratio != mHeightRatio) {
        mHeightRatio = ratio;
        requestLayout();
    }
}

public double getHeightRatio() {
    return mHeightRatio;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mHeightRatio > 0.0) {
        // set the image views size
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) (width * mHeightRatio);
        setMeasuredDimension(width, height);
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}


}

然后在你的onBindViewHolder方法中:

Picasso.with(context).load(modal.image.mediumUrl).into(holder.profileImage, new Callback() {
            @Override
            public void onSuccess() {
                holder.profileImage.setHeightRatio(holder.profileImage.getHeight()/holder.profileImage.getWidth());
            }

            @Override
            public void onError() {

            }
        });

非常感谢,我使用Glide时也遇到了同样的问题。 - Clive Jefferies

2

经过一番搜索,我意识到没有可行的解决方案!StaggeredGridLayoutManager会重新排列其项目,导致项目移动。除非我们编写一个完全定制的LayoutManager版本,这并不是很容易,否则我怀疑是否有一种处理方式。


-1

要禁用重新排列,只需调用 recyclerView.itemAnimator = null

此外,如果您使用ImageView,请确保将adjustViewBounds设置为true


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接