@@ -57,39 +57,59 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
5757
5858 private void getItemOffsetVertical (Rect outRect , RecyclerView parent , View child ) {
5959
60- int parentSize = parent .getWidth ();
60+ int parentSize = parent .getWidth () - parent . getPaddingLeft () - parent . getPaddingRight () ;
6161 GridLayoutManager manager = ((GridLayoutManager ) parent .getLayoutManager ());
6262 GridLayoutManager .LayoutParams layoutParams = ((GridLayoutManager .LayoutParams ) child .getLayoutParams ());
6363 int childAdapterPosition = parent .getChildAdapterPosition (child );
6464
6565 List <Pair <Integer , Integer >> calculatedSpacings = this .getCalculatedSpacings (parentSize , manager .getSpanCount ());
6666 Pair <Integer , Integer > spacing = calculatedSpacings .get (layoutParams .getSpanIndex ());
6767
68- if (childAdapterPosition < manager .getSpanCount ()) {
69- // Apply horizontal spacing only to the first row
70- outRect .set (spacing .first , 0 , spacing .second , 0 );
68+ // Assign left spacing
69+ outRect .left = spacing .first ;
70+
71+ // Find the end span of this item
72+ if (layoutParams .getSpanSize () > 1 ) {
73+ // This item is larger than 1 span, find the spacing for the last span this view occupies
74+ Pair <Integer , Integer > trailingSpacing = calculatedSpacings .get (layoutParams .getSpanIndex () + layoutParams .getSpanSize () - 1 );
75+ outRect .right = trailingSpacing .second ;
7176 } else {
77+ // This item has span size 1, assign right spacing from the same container
78+ outRect .right = spacing .second ;
79+ }
80+
81+ if (childAdapterPosition >= manager .getSpanCount ()) {
7282 // Add vertical spacing in addition to horizontal spacings
73- outRect .set ( spacing . first , this .spacing , spacing . second , 0 ) ;
83+ outRect .top = this .spacing ;
7484 }
7585 }
7686
7787 private void getItemOffsetHorizontal (Rect outRect , RecyclerView parent , View child ) {
7888
79- int parentSize = parent .getHeight ();
89+ int parentSize = parent .getHeight () - parent . getPaddingTop () - parent . getPaddingBottom () ;
8090 GridLayoutManager manager = ((GridLayoutManager ) parent .getLayoutManager ());
8191 GridLayoutManager .LayoutParams layoutParams = ((GridLayoutManager .LayoutParams ) child .getLayoutParams ());
8292 int childAdapterPosition = parent .getChildAdapterPosition (child );
8393
8494 List <Pair <Integer , Integer >> calculatedSpacings = this .getCalculatedSpacings (parentSize , manager .getSpanCount ());
8595 Pair <Integer , Integer > spacing = calculatedSpacings .get (layoutParams .getSpanIndex ());
8696
87- if (childAdapterPosition < manager .getSpanCount ()) {
88- // Apply vertical spacing only to the first column
89- outRect .set (0 , spacing .first , 0 , spacing .second );
97+ // Assign left spacing
98+ outRect .top = spacing .first ;
99+
100+ // Find the end span of this item
101+ if (layoutParams .getSpanSize () > 1 ) {
102+ // This item is larger than 1 span, find the spacing for the last span this view occupies
103+ Pair <Integer , Integer > trailingSpacing = calculatedSpacings .get (layoutParams .getSpanIndex () + layoutParams .getSpanSize () - 1 );
104+ outRect .bottom = trailingSpacing .second ;
90105 } else {
106+ // This item has span size 1, assign right spacing from the same container
107+ outRect .bottom = spacing .second ;
108+ }
109+
110+ if (childAdapterPosition >= manager .getSpanCount ()) {
91111 // Add horizontal spacing in addition to vertical spacings
92- outRect .set ( this . spacing , spacing . first , 0 , spacing . second ); ;
112+ outRect .left = this . spacing ;
93113 }
94114 }
95115
0 commit comments