【问题标题】:Cells are not staggered in using RecyclerView with StaggeredGridLayoutManager in Android在 Android 中将 RecyclerView 与 StaggeredGridLayoutManager 一起使用时,单元格不会交错
【发布时间】:2016-09-27 13:13:28
【问题描述】:

我正在开发一个 Android 应用程序。我不擅长Android开发。在我的应用程序中,我将 RecyclerView 和 CardView 与 StaggeredGridLayoutManager 一起使用。因为我希望列表中的每个单元格大小彼此不同,并且如下图所示交错排列。

但是当我运行我的应用程序时,单元格并没有交错,它们的大小彼此相等。

这是截图

这是我的回收站视图 XML

<android.support.v7.widget.RecyclerView
        android:id="@+id/df_rv_destination"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

这是单元格项目的 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/di_iv_image"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:textSize="17dp"
            android:textColor="@color/white"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:id="@+id/di_tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

这就是我使用 StaggerGridLayoutManager 配置 RecyclerView 的方式

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1);
        rcDestinations.setLayoutManager(staggeredGridLayoutManager);
        rcDestinations.setItemAnimator(new DefaultItemAnimator());
        regionsList = new ArrayList<Region>();
        destinationsAdapter = new DestinationsAdapter(getActivity(),regionsList);
        rcDestinations.setAdapter(destinationsAdapter);

那么为什么我的 RecyclerView 没有交错呢?我该如何解决?

【问题讨论】:

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


    【解决方案1】:

    android:adjustViewBounds="true"设置你的图像视图

    【讨论】:

    【解决方案2】:

    您的单元格项目都具有相同的大小,因为没有什么会改变大小...我想如果您有更大的图片,您想获得更大的项目

    首先改变这个:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    

    到这里:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    

    然后代替:

    <ImageView
            android:id="@+id/di_iv_image"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    

    尝试:

    <ImageView
            android:id="@+id/di_iv_image"
            android:scaleType="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    

    并在添加图像之前将图像高度缩放到您想要的大小;) 使用cropCenter,您始终可以将图像裁剪为相同大小

    666

    【讨论】:

    • 如果我将图像比例类型设置为中心,图像宽度将与单元格宽度不匹配。对齐将不正确。尤其是文字。
    • 是的,没错,但在将其添加到 imageView 之前,您必须至少缩放图像高度。你能告诉我你添加图片的代码部分吗?
    • 没什么特别的。我只是直接使用毕加索设置图像。没有修改。
    • 但是交错布局本身就有这种特性。对吗?
    • 是的!问题是你得到了大图片,如果你不按高度缩放它们,你会得到很多高度像素,例如600(看起来很丑)所以你必须至少缩放图片。如果您使用 centerCrop 对其进行缩放,它将被裁剪为单元格的大小。尝试将 scaleY 设置为 0.5,例如缩放到一半高度
    【解决方案3】:

    您需要强制调整高度大小以获得您想要的效果。

    我正在使用数据绑定,所以设置高度如下:

    public int getHeight(int position) {
        Random r = new Random();
        int randomHeight = r.nextInt(Math.abs(mRecyclerViewHeight)/9) + Math.abs(mRecyclerViewHeight)/4;
        if(position%2 == 0){
            randomHeight += Math.abs(mRecyclerViewHeight)/9;
        }
        return randomHeight;
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
    
            ((DiscoveryViewHolder) holder).bindRepository(mPostList.get(position), getHeight(position));
    

    holder).bindRepository(mPostList.get(position)); }

    【讨论】:

    • 什么是 mRecyclerViewHeight 值或如何获取?
    • 抱歉回答晚了。 mRecyclerViewHeight 是布局中回收器视图组件的高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多