【问题标题】:Stop items from moving around when using StaggeredGridLayoutManager使用 StaggeredGridLayoutManager 时阻止项目四处移动
【发布时间】:2015-04-15 21:45:39
【问题描述】:

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

【问题讨论】:

标签: android android-recyclerview staggered-gridview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用 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);
                          }
                      }
                  });
      

      【讨论】:

        【解决方案4】:

        如果你使用的是 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 时遇到了同样的问题。
        猜你喜欢
        • 2015-02-22
        • 2021-01-30
        • 1970-01-01
        • 2012-06-20
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        相关资源
        最近更新 更多