【问题标题】:RecyclerView with StaggeredGridLayoutManager with images loading from Glide带有 StaggeredGridLayoutManager 的 RecyclerView 以及从 Glide 加载的图像
【发布时间】:2016-07-09 00:49:54
【问题描述】:

我在显示RecyclerViewStaggeredGridLayoutManager 时遇到问题,其项目包含imageview,并且图像使用glide 加载到imageview。 我面临的问题是,加载图像后,它们没有交错并且大小相同,两列之间也有很大的差距。如何改变图像的高度而没有列间的间隙?

【问题讨论】:

  • 如果列之间的差距很大,那么您还应该为您的图像提供scaleType,我建议您设置 centerCrop 或 fitXY scaleType。
  • 给出你作品的任何图像+代码会更不稳定
  • 你可以看看这里你可以使用滑翔与Glide/Picasso-RecyclerView-StaggeredGridLayoutManager

标签: android android-recyclerview android-glide staggered-gridview staggeredgridlayout


【解决方案1】:

我遇到了同样的问题。对我有用的是为我的 ImageView 设置android:adjustViewBounds="true"。我什至没有设置 scaleType。

这是我的布局文件 item.xml 的完整内容。

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gif_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorFiller"
    android:adjustViewBounds="true"/>

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我终于能够找到灵魂。我必须创建自定义图像视图以在交错回收器视图中改变其纵横比。

    public class DynamicHeightNetworkImageView extends ImageView {
    private float mAspectRatio = 1.5f;
    
    public DynamicHeightNetworkImageView(Context context) {
        super(context);
    }
    
    public DynamicHeightNetworkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public DynamicHeightNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public void setAspectRatio(float aspectRatio) {
        mAspectRatio = aspectRatio;
        requestLayout();
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        setMeasuredDimension(measuredWidth, (int) (measuredWidth / mAspectRatio));
    }
    }
    

    然后在适配器的 onBindViewHolder 中,我通过 - 设置图像的纵横比 -

    Picasso.with(this).load(THUMB_URL).into(holder.thumbnailView);
    holder.thumbnailView.setAspectRatio(ASPECT_RATIO));
    

    【讨论】:

    • 谢谢你.. 2 年后,你仍然拯救了我的项目和我的生命:D
    • 要传递 ASPECT_RATIO 什么?如何获得?
    【解决方案3】:

    android:adjustViewBounds="true" 添加到项目布局中的ImageView 并在Glide 中使用CenterCropFitCenter,如下所示:

    Glide.with(vh.view.getContext())
            .load(item.getUrl())
            .centerCrop()
            .fitCenter()
            .thumbnail(0.3f)
            .placeholder(R.drawable.img_placeholder)
            .into(vh.image);
    

    【讨论】:

      【解决方案4】:

      首先初始化你的 RecyclerView

       RecyclerView recyclerGallery = (RecyclerView)findViewById(R.id.gallery_recycler_view);
       gaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 1);
       recyclerGallery.setLayoutManager(gaggeredGridLayoutManager);
       galleryAdapter = new GalleryAdaptor(ProfImageGallery.this, imgsUrl);
       recyclerGallery.setAdapter(galleryAdapter);
      

      然后是您的适配器(根据您的需要进行编辑)

        public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.ViewHolder> {
      
              private Context mContext;
              private ArrayList<String> mDataset;
      
      
        // Provide a suitable constructor (depends on the kind of dataset)
              public GalleryAdaptor(Context mContext, ArrayList<String> mdataList) {
                    this.mContext = mContext;
                    this.mDataset = mdataList;
              }
      
              // Create new views (invoked by the layout manager)
              @Override
              public GalleryAdaptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      
                    // create a new view
                    View v = LayoutInflater.from(parent.getContext())
                                   .inflate(R.layout.row_gallery, parent, false);
      
                    // set the view's size, margins, paddings and layout parameters 
                    ViewHolder vh = new ViewHolder(v);
                    return vh;
              }
      
              // Replace the contents of a view (invoked by the layout manager)
              @Override
              public void onBindViewHolder(final ViewHolder holder, final int position) {
                    // - get element from your dataset at this position
                    // - replace the contents of the view with that element
      
      
                    String imageUrl = mDataset.get(position);
      
                    holder.progressBar.setVisibility(View.VISIBLE); 
      
                    //load the image url with a callback to a callback method/class
      
                    Picasso.with(mContext)
                          .load(imageUrl)
                          .into(holder.workImage, 
                                new ImageLoadedCallback(holder.progressBar) {
                                      @Override
                                      public void onSuccess() {
                                            if (holder.progressBar != null) {
                                                  holder.progressBar.setVisibility(View.GONE);
                                            }
                                      }
                                });
              }
      

      替换毕加索代码即

        Picasso.with(mContext)
              .load(imageUrl)
              .into(holder.workImage, 
                    new ImageLoadedCallback(holder.progressBar) {
                          @Override
                          public void onSuccess() {
                                if (holder.progressBar != null) {
                                      holder.progressBar.setVisibility(View.GONE);
                                }
                          }
                    });
      

      使用滑行。

        Glide.with(this).load(imageUrl).into(imageView);
      

      【讨论】:

      • R.layout.row_gallery 的布局是什么?
      • 这是您在回收站视图中使用的布局行
      【解决方案5】:

      对我来说它正在工作

              Picasso.get()
                      .load(uploadCurrent.getImageUrl())
                      .placeholder(R.drawable.loading)
                      .config(Bitmap.Config.RGB_565)
                      .into(holder.imageView);
      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="2dp"
          xmlns:app="http://schemas.android.com/apk/res-auto">
          <ImageView
              android:id="@+id/id_imgItem"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:adjustViewBounds="true"/>
          <TextView
              android:id="@+id/textview"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignBottom="@+id/id_imgItemGets"
              android:paddingTop="4dp"
              android:paddingBottom="4dp"
              android:gravity="center_horizontal"
              android:textSize="18sp"/>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-14
        • 2019-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多