@@ -318,15 +318,16 @@ private void drawForVerticalOrientation(@NonNull Canvas canvas, @NonNull Recycle
318318 right -= isRtl ? insetStart : insetEnd ;
319319
320320 int childCount = parent .getChildCount ();
321- int dividerCount = lastItemDecorated ? childCount : childCount - 1 ;
322- for (int i = 0 ; i < dividerCount ; i ++) {
321+ for (int i = 0 ; i < childCount ; i ++) {
323322 View child = parent .getChildAt (i );
324- parent .getDecoratedBoundsWithMargins (child , tempRect );
325- // Take into consideration any translationY added to the view.
326- int bottom = tempRect .bottom + Math .round (child .getTranslationY ());
327- int top = bottom - dividerDrawable .getIntrinsicHeight () - thickness ;
328- dividerDrawable .setBounds (left , top , right , bottom );
329- dividerDrawable .draw (canvas );
323+ if (shouldDrawDivider (parent , child )) {
324+ parent .getDecoratedBoundsWithMargins (child , tempRect );
325+ // Take into consideration any translationY added to the view.
326+ int bottom = tempRect .bottom + Math .round (child .getTranslationY ());
327+ int top = bottom - thickness ;
328+ dividerDrawable .setBounds (left , top , right , bottom );
329+ dividerDrawable .draw (canvas );
330+ }
330331 }
331332 canvas .restore ();
332333 }
@@ -352,15 +353,16 @@ private void drawForHorizontalOrientation(@NonNull Canvas canvas, @NonNull Recyc
352353 bottom -= insetEnd ;
353354
354355 int childCount = parent .getChildCount ();
355- int dividerCount = lastItemDecorated ? childCount : childCount - 1 ;
356- for (int i = 0 ; i < dividerCount ; i ++) {
356+ for (int i = 0 ; i < childCount ; i ++) {
357357 View child = parent .getChildAt (i );
358- parent .getDecoratedBoundsWithMargins (child , tempRect );
359- // Take into consideration any translationX added to the view.
360- int right = tempRect .right + Math .round (child .getTranslationX ());
361- int left = right - dividerDrawable .getIntrinsicWidth () - thickness ;
362- dividerDrawable .setBounds (left , top , right , bottom );
363- dividerDrawable .draw (canvas );
358+ if (shouldDrawDivider (parent , child )) {
359+ parent .getDecoratedBoundsWithMargins (child , tempRect );
360+ // Take into consideration any translationX added to the view.
361+ int right = tempRect .right + Math .round (child .getTranslationX ());
362+ int left = right - thickness ;
363+ dividerDrawable .setBounds (left , top , right , bottom );
364+ dividerDrawable .draw (canvas );
365+ }
364366 }
365367 canvas .restore ();
366368 }
@@ -372,12 +374,32 @@ public void getItemOffsets(
372374 @ NonNull RecyclerView parent ,
373375 @ NonNull RecyclerView .State state ) {
374376 outRect .set (0 , 0 , 0 , 0 );
375- if (lastItemDecorated || parent . getChildLayoutPosition ( view ) != state . getItemCount () - 1 ) {
377+ if (shouldAddOffset ( parent , view , state ) ) {
376378 if (orientation == VERTICAL ) {
377- outRect .bottom = dividerDrawable . getIntrinsicHeight () + thickness ;
379+ outRect .bottom = thickness ;
378380 } else {
379- outRect .right = dividerDrawable . getIntrinsicWidth () + thickness ;
381+ outRect .right = thickness ;
380382 }
381383 }
382384 }
385+
386+ private boolean shouldDrawDivider (@ NonNull RecyclerView parent , @ NonNull View child ) {
387+ if (lastItemDecorated ) {
388+ return true ;
389+ }
390+ int position = parent .getChildAdapterPosition (child );
391+ RecyclerView .Adapter <?> adapter = parent .getAdapter ();
392+ return position != RecyclerView .NO_POSITION
393+ && adapter != null
394+ && position != adapter .getItemCount () - 1 ;
395+ }
396+
397+ private boolean shouldAddOffset (
398+ @ NonNull RecyclerView parent , @ NonNull View child , @ NonNull RecyclerView .State state ) {
399+ if (lastItemDecorated ) {
400+ return true ;
401+ }
402+ int position = parent .getChildAdapterPosition (child );
403+ return position != RecyclerView .NO_POSITION && position != state .getItemCount () - 1 ;
404+ }
383405}
0 commit comments