使用Grid Layout Manager进行Recyclerview的相等填充

时间:2018-11-29 14:04:25

标签: android padding gridlayoutmanager

我的recyclerview xml代码是:

<android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:id="@+id/blogrow"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_gravity="center_vertical"
            />

我的CardView代码是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="160dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#fff"
    app:cardCornerRadius="2dp"
    android:layout_gravity="center"
    app:cardElevation="0dp"
    android:layout_marginStart="3dp"
    android:layout_marginEnd="3dp"
    android:layout_marginBottom="4dp"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/profilePostImage"
            />

    </LinearLayout>

我使用过网格布局管理器:

mgridLayoutManager = new GridLayoutManager(getActivity(),2);
        mgridLayoutManager.setReverseLayout(true);

问题是我得到的图像填充不均匀。例如,左列图像的左填充较小。两列之间有大量填充。右列图像的右侧有大量的填充。 如何修复,以使列具有均匀的填充?

1 个答案:

答案 0 :(得分:0)

如果您想使recyclerview中的每个项目都具有相同的高度和宽度,则应使用recyclerview.setHasFixedSize(true)