在recyclerview

时间:2018-06-05 10:26:16

标签: android android-recyclerview item-decoration

  

我想在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>

0 个答案:

没有答案