recyclerview通用的分隔条

public class BaseMultiItemDecorationextends RecyclerView.ItemDecoration {

private int mRowSpace, mColumnSpace;

    @Override

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

int position = parent.getChildAdapterPosition(view);

        if (parent.getAdapter() !=null) {

RecyclerView.Adapter parentAdapter = parent.getAdapter();

            int type = parentAdapter.getItemViewType(position);

            RecyclerView.LayoutManager manager = parent.getLayoutManager();

            if (managerinstanceof GridLayoutManager) {

GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;

                int count = parentAdapter.getItemCount();

                int spanCount = gridLayoutManager.getSpanCount();

                if (LinearLayoutManager.VERTICAL == gridLayoutManager.getOrientation()) {

if (spanCount ==1) {

getLinearVerticalOffsets(outRect, parent, type, position);

                    }else {

getGridVerticalOffsets(gridLayoutManager,outRect, parent, type, position, spanCount, count);

                    }

}else if (LinearLayoutManager.HORIZONTAL == gridLayoutManager.getOrientation()) {

if (spanCount ==1) {

getLinearHorizontalOffsets(outRect, parent, type, position);

                    }else {

getGridHorizontalOffsets(gridLayoutManager,outRect, parent, type, position, spanCount, count);

                    }

}

}else if (managerinstanceof LinearLayoutManager) {

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) manager;

                if (LinearLayoutManager.VERTICAL == linearLayoutManager.getOrientation()) {

getLinearVerticalOffsets(outRect, parent, type, position);

                }else {

getLinearHorizontalOffsets(outRect, parent, type, position);

                }

}

}

}

private void getLinearHorizontalOffsets(@NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position) {

if (position ==0) {

outRect.left =mColumnSpace;

        }

if (type <0) {

return;

        }

outRect.bottom =mRowSpace;

        outRect.top =mRowSpace;

        outRect.right =mColumnSpace;

    }

private void getLinearVerticalOffsets(@NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position) {

if (position ==0) {

outRect.top =mRowSpace;

        }

if (type <0) {

return;

        }

outRect.bottom =mRowSpace;

        outRect.left =mColumnSpace;

        outRect.right =mColumnSpace;

    }

private void getGridHorizontalOffsets(GridLayoutManager gridLayoutManager, @NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position, int spanCount, int count) {

GridLayoutManager.SpanSizeLookup sizeLookup = gridLayoutManager.getSpanSizeLookup();

        if(sizeLookup==null){

return;

        }

final int groupIndex = sizeLookup.getSpanGroupIndex(position, spanCount);

        int index = sizeLookup.getSpanIndex(position, spanCount);

        int spanSize = sizeLookup.getSpanSize(position);

        if(groupIndex==0){

outRect.left =mColumnSpace;

        }

if (type <0) {

return;

        }

outRect.right=mColumnSpace;

        int column=(index+spanSize)%spanCount;

        outRect.top = index==0?mRowSpace:mRowSpace/2;

        outRect.bottom=column==0?mRowSpace:mRowSpace/2;

    }

private void getGridVerticalOffsets(GridLayoutManager gridLayoutManager, @NonNull Rect outRect, @NonNull RecyclerView parent, int type, int position, int spanCount, int count) {

GridLayoutManager.SpanSizeLookup sizeLookup = gridLayoutManager.getSpanSizeLookup();

        if(sizeLookup==null){

return;

        }

final int groupIndex = sizeLookup.getSpanGroupIndex(position, spanCount);

        int index = sizeLookup.getSpanIndex(position, spanCount);

        int spanSize = sizeLookup.getSpanSize(position);

        if(groupIndex==0){

outRect.top =mRowSpace;

        }

if (type <0) {

return;

        }

outRect.bottom =mRowSpace;

        int column=(index+spanSize)%spanCount;

        outRect.left = index==0?mColumnSpace:mColumnSpace/2;

        outRect.right=column==0?mColumnSpace:mColumnSpace/2;

    }

public BaseMultiItemDecoration(int space) {

this.mColumnSpace =mRowSpace = space;

    }

public BaseMultiItemDecoration(int rowSpace, int columnSpace) {

this.mRowSpace = rowSpace;

        this.mColumnSpace = columnSpace;

    }

}

你可能感兴趣的:(recyclerview通用的分隔条)