package com.example.wallpapermagazine;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class Recycle_view extends RecyclerView.ItemDecoration {

    private int h_Item;
    private int v_Item;
    private int e_Item;

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        int spanSise=1;
        int posation=parent.getChildAdapterPosition(view);
        int spanIndex=0;
        int spanCount=1;
        //确定recycleview内部布局
        RecyclerView.LayoutManager layoutManager=parent.getLayoutManager();

        if (layoutManager instanceof GridLayoutManager){
            GridLayoutManager layoutManager1=(GridLayoutManager) layoutManager;
            GridLayoutManager.LayoutParams layoutParams=(GridLayoutManager.LayoutParams) view.getLayoutParams();
            spanCount=layoutManager1.getSpanCount();
            spanSise=layoutManager1.getSpanSizeLookup().getSpanSize(posation);
            spanIndex= layoutParams.getSpanIndex();

        }
        //设置网格间是间距

        int itemAllSpacing = (v_Item * (spanCount + 1) + e_Item * 2) / spanCount;
        int left = v_Item * (spanIndex + 1) - itemAllSpacing * spanIndex + e_Item;
        int right = itemAllSpacing - left;
        outRect.left = left;
        outRect.right = right;
        outRect.bottom = h_Item;

    }
    public Recycle_view(int v_Item, int h_Item, int e_Item){
        this.e_Item=e_Item;
        this.h_Item=h_Item;
        this.v_Item=v_Item;


    }
}
