0

我想在 recyclerview 中放置网格项目,它现在适用于所有方面(顶部,底部..等)

我只想从顶部应用空间,右侧任何人都可以帮助我更改现有代码。

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}

下面是适配器的项目 xml。

在这个 xml 中,我根据需要给出了边距,但它不影响布局。

虽然,我已经从装饰类和它的工作中评论了 2 行代码(来自其他部分)。/*if (position >= spanCount) { outRect.top = spacing; // item top }*/

xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="@dimen/margin_120dp"
    android:layout_height="@dimen/margin_120dp"
    android:background="@color/white"
    android:elevation="@dimen/margin_2"
    android:layout_marginRight="@dimen/margin_2"
    android:layout_marginTop="@dimen/margin_2"
    android:orientation="vertical"
    app:cardCornerRadius="@dimen/margin_2"
    app:cardPreventCornerOverlap="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/category_item"
            android:layout_width="@dimen/margin_60dp"
            android:layout_height="@dimen/margin_60dp"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/margin_10dp"
            android:src="@drawable/home" />

        <TextView
            android:id="@+id/cat_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:gravity="bottom"
            android:padding="@dimen/margin_5dp"
            android:text="@string/app_name" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
4

0 回答 0