何找到StaggeredGridLayoutManager单元格

何找到StaggeredGridLayoutManager单元格

本文介绍了如何找到StaggeredGridLayoutManager单元格的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 StaggeredGrid RecyclerView 和我想动态设置图像高度在 onBindViewHolder 。我以为 StaggeredGrid 应该自动处理所有这一切,所以我设置的android:layout_width =match_parent 的android:scaleType =fitStart 的ImageView ,但有大量的图像周围的空白

I have a StaggeredGrid in a RecyclerView and I am trying to dynamically set the image height in the onBindViewHolder. I thought the StaggeredGrid was supposed to handle all this automatically, so I set android:layout_width="match_parent and android:scaleType="fitStart on the ImageView, but there is lots of gaps around the image.

由于 StaggeredGrid 不辜负它,我试图通过动态地定义图像的高度,以帮助它了一下 onBindViewHolder 。我有提前的时间的图像的宽度和高度,但网格的单元宽度不可用。我试图 holder.cardView.getLayoutParams()。宽度 getMeasuredWidth(),但他们都返回零或小的数字。我知道有其他地方的单元格的宽度将是可用的,但我真的需要它在 onBindViewHolder 事件,所以我可以设置和调整图像。

Since the StaggeredGrid isn't living up to it, I'm trying to help it a bit by defining the image height dynamically in the onBindViewHolder. I have the width and height of the image ahead of time, but the cell width of the grid isn't available. I tried holder.cardView.getLayoutParams().width and getMeasuredWidth(), but they both return zero or small numbers. I know there are other places the cell width would be available, but I really need it in the onBindViewHolder event so I can set and adjust the image.

这是如何利用来实现这个任何想法的 StaggeredGrid ?任何意见或经验AP preciated!

Any ideas on how to achieve this by leveraging the StaggeredGrid? Any advice or experience is appreciated!

在我的活动是这样的:

mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

在我的适配器:

@Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
    MyModel item = mData.get(position);

    int imageWidth = item.getFeatured_image().getWidth();
    int imageHeight = item.getFeatured_image().getHeight();
    int cellWidth = 540; // <==== how to find dynamically??!!

    ViewGroup.LayoutParams imageLayoutParams = holder.thumbnailImageView.getLayoutParams();
    imageLayoutParams.width = cellWidth;
    imageLayoutParams.height = imageHeight * cellWidth / imageWidth;
    holder.thumbnailImageView.setLayoutParams(imageLayoutParams);

    MediaHelper.displayImage(item, holder.thumbnailImageView);
}

在我的布局:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/row_my_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp">

    <ImageView
        android:id="@+id/row_my_thumbnail_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:scaleType="fitStart" />

    <TextView
        android:id="@+id/row_my_title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#CCFFFFFF"
        android:textStyle="bold"
        android:padding="5dp" />
</android.support.v7.widget.CardView>

我也尝试添加这里面确实给我的单元格的宽度,但图像设定和调整在<$ C $后的 onGlobalLayout 事件被触发方式C> onBindViewHolder 事件,因此一切都太迟了。

I also tried to add this which does give me the cell width, but the onGlobalLayout event gets triggered way after the image is set and adjusted in the onBindViewHolder event so it's too late.

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    final View view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.row_my_post, parent, false);

    final MyViewHolder viewHolder = new MyViewHolder(view);

    ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int viewWidth = view.getWidth();
                int viewHeight = view.getHeight();
            }
        });
    }

    return viewHolder;
}

下面是由于不同的图像尺寸是随机的空白。我想是所有图像是单元的宽度,让图像的高度和细胞动态调整:

Here is the gaps which are random due to the various image sizes. What I would like is for ALL images to be the width of the cell and let the height of the image and cell adjust dynamically:

推荐答案

试试这个: - 在您的ImageView添加此 - >机器人:adjustViewBounds =真正的

Try This:- add this in your ImageView--> android:adjustViewBounds="true"

<android.support.v7.widget.CardView
 xmlns:card_view="http://schemas.android.com/apk/res-auto"
 android:id="@+id/row_my_card"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="5dp"
 card_view:cardCornerRadius="2dp">

<ImageView
    android:id="@+id/row_my_thumbnail_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:adjustViewBounds="true"/>

<TextView
    android:id="@+id/row_my_title_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#CCFFFFFF"
    android:textStyle="bold"
    android:padding="5dp" />
 </android.support.v7.widget.CardView>

这篇关于如何找到StaggeredGridLayoutManager单元格的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:09