如何从StaggeredGridLayoutManager中找到单元格宽度?

Tru*_*an1 5 android android-layout android-adapter staggered-gridview staggeredgridlayout

我有一个StaggeredGrid,RecyclerView我试图动态设置图像高度onBindViewHolder.我认为StaggeredGrid应该自动处理所有这些,所以我设置android:layout_width="match_parentandroid:scaleType="fitStart在上面ImageView,但图像周围有很多空隙.

由于StaggeredGrid没有达到它,我试图通过动态定义图像高度来帮助它onBindViewHolder.我提前有图像的宽度和高度,但网格的单元格宽度不可用.我试过holder.cardView.getLayoutParams().widthgetMeasuredWidth(),但他们都返回零个或小的数字.我知道还有其他地方可以使用单元格宽度,但我确实需要它,onBindViewHolder因此我可以设置和调整图像.

关于如何通过利用这个来实现这一目标的任何想法StaggeredGrid?任何建议或经验表示赞赏!

我的活动是这样的:

mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)

在我的适配器中:

@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);
}
Run Code Online (Sandbox Code Playgroud)

在我的布局中:

<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>
Run Code Online (Sandbox Code Playgroud)

我也尝试添加这个确实给我单元格宽度,但onGlobalLayout事件在图像设置和onBindViewHolder事件调整之后被触发,所以为时已晚.

@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;
}
Run Code Online (Sandbox Code Playgroud)

这是由于各种图像尺寸而随机的间隙.我想要的是所有图像都是单元格的宽度,并让图像和单元格的高度动态调整:

在此输入图像描述

Cha*_*mar 12

试试这个: - 在你的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>
Run Code Online (Sandbox Code Playgroud)