0

I need to add a divider between each grid of the RecyclerView.

RecyclerView recyclerView= (RecyclerView) profileView.findViewById(R.id.profile_recycler_view);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Adapter Adapter = new Adapter(getActivity());
recyclerView.setAdapter(profileAdapter);

Please help me.

Example:

Example

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29

2 Answers2

4

You need Decoration for this.
Here is the example:

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
    private int offset;

    public ItemOffsetDecoration(int offset) {
        this.offset = offset;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = offset;
        outRect.right = offset;
        outRect.bottom = offset;
        if(parent.getChildAdapterPosition(view) == 0) {
            outRect.top = offset;
        }
    }
}

And in your activity/fragment

grid.setLayoutManager(new GridLayoutManager(this, 2));
grid.addItemDecoration(new ItemOffsetDecoration(1));
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Nikola Milutinovic
  • 731
  • 1
  • 8
  • 23
  • Please read the question..it will add the divider to every item. I dont want that. i need the divider as shown in the image. – Jagadesh Seeram Sep 18 '15 at 14:58
  • Ok, I understand. You cannot copy/paste this code, but please see that you can define which component will be affected with the decoration. But if I'm getting you right you want black line in between? If that is the case I will write you solution for that. – Nikola Milutinovic Sep 18 '15 at 15:02
1

I think, that Jagadesh Seeram was talking about horizontal divider between items, but RecyclerView.ItemDecoration adds divider everywhere.

If you don't use autoscroll to some item, you should write like this:

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int offset;

public ItemOffsetDecoration(int offset) {
    this.offset = offset;
}

@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {
    int bottonIndex;
    if (parent.getAdapter().getItemCount() % 2 == 0){
        bottonIndex = parent.getAdapter().getItemCount() - 2;
    } else {
        bottonIndex = parent.getAdapter().getItemCount() - 1;
    }

    if (parent.getChildAdapterPosition(view) < bottonIndex){
        outRect.bottom = offset;
    } else {
        outRect.bottom = 0;
    }

    if(parent.getChildAdapterPosition(view) > 1 ) {
        outRect.top = offset;
    } else {
        outRect.top = 0;
    }

    if( (parent.getChildAdapterPosition(view) % 2 ) == 0) {
        outRect.right = offset;
        outRect.left = 0;
    } else {
        outRect.right = 0;
        outRect.left = offset;
    }

}
TheSecond
  • 183
  • 1
  • 1
  • 8