Skip to content Skip to sidebar Skip to footer

Center Items Horizontally In Recyclerview(using Gridlayoutmanager)

I'm trying to center all my items horizontally for each row in RecyclerView I tried many things: RelativeLayout as a parent, then use layout_centerInParent, use the basic android:l

Solution 1:

you can use a SpanSizeLookup to make views/items with different widths for your grid view. So with that you can do some cleverness with empty views to adjust accordingly. Might be more complicate or simpler depending on how variable your layout actually is.

reference: https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.SpanSizeLookup.html

for this specific case you show on your drawing you would do something like:

GridLayoutManagerlm=newGridLayoutManager(context, 6);
lm.setSpanSizeLookup(newMySizeLookup());

publicstaticclassMySizeLookupextendsSpanSizeLookup {

   publicintgetSpanSize(int position) {
      if(position >= 0 && position <= 2) {
          return2;
      } elseif (position == 3 || position == 6) {
          return1;
      } else {
          return2;
      }
   }
}

and with that you'll have to make on your adapter, the position 3 and 6 to be an empty/invisible element. Something like new View(context); just to occupy that extra space

Solution 2:

Use the gridLayoutManager = new GridLayoutManager(context,6) and override setSpanSizeLookup.

Example:

int totalSize=list.size();
gridLayoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
                    @OverridepublicintgetSpanSize(int position) {
                        int span;
                        span = totalSize % 3;
                        if (totalSize < 3) {
                            return6;
                        } elseif (span == 0 || (position <= ((totalSize - 1) - span))) {
                            return2;
                        } elseif (span == 1) {
                            return6;
                        } else {
                            return3;
                        }
                    }
                });

This will work if you want to display 3 items in a row and remaining in the center of the grid.

Change the grid layout manager span counts accordingly your requirement.

Solution 3:

GridLayoutManagerlayoutManager=newGridLayoutManager(getContext(), 6);
finalinttotalSize= customList.size();

layoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
    @OverridepublicintgetSpanSize(int position) {
        int extra;
        extra = totalSize % 3;

        if(extra == 0)
            return2;

        if (totalSize - (position + 1) < extra) {
            return (int) 6 / extra;
        }

        return2;
    }
});

recycler.setLayoutManager(layoutManager);

Solution 4:

GridLayoutManagerlayoutManager=newGridLayoutManager(getContext(), 6);
    finalinttotalSize= customList.size();

    layoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {
        @OverridepublicintgetSpanSize(int position) {
            introws= Math.round((totalSize / 3f) + 0.4f);
            intitemInRow= Math.round(((position + 1) / 3f) + 0.4f);
            if (rows == 1 || rows == itemInRow) {
                intspan= totalSize % 3;
                if (span == 0) {
                    return2;
                }
                return6 / span;
            }
            return2;
        }
    });

Post a Comment for "Center Items Horizontally In Recyclerview(using Gridlayoutmanager)"