android - 使用 StaggeredGridLayoutManager 时阻止项目四处移动

标签 android android-recyclerview staggered-gridview

我在 android 中将 recyclerview 与 staggredGridLayoutManager 结合使用。问题是,有时当滚动项目四处移动以适应屏幕时。通常这没什么好担心的,但在我的情况下,它把一切都搞砸了! 那么有没有办法阻止这种行为呢?不仅仅是动画。整个项目重新排列的东西。 谢谢你

最佳答案

如果您使用的是 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() {

            }
        });

关于android - 使用 StaggeredGridLayoutManager 时阻止项目四处移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523516/

相关文章:

android - 以编程方式将 ABOVE 添加到 LayoutParams 会导致 View 高度为 0

android - 尝试在 Kotlin 中创建一个简单的 recyclerView,但适配器未正确应用

android - 在 Staggered Recyclerview 中显示类似结构的标签

android - 所选图像的交错相框

android - 播放媒体商店中的下一个音频

android - 如何列出应用程序公开的所有 Activity ?

android - 调用 RecyclerView.Adapter.notifyItemInserted() 时,不应用 DividerItemDecoration

java - RecyclerView notifydatasetchanged 未从 fragment 调用

android - 如何在 StaggeredGridView 中使用 PullToRefresh?

java - 使用 onSwipe 时考虑 RecyclerView 中的边距空间