Skip to content

Commit 6f547d4

Browse files
pubiqqleticiarossi
authored andcommitted
[Divider] Fixed logic of determining whether to draw divider in MaterialDividerItemDecoration.
Resolves #2818 Resolves #2817 Resolves #2771 GIT_ORIGIN_REV_ID=25b8815490fb3c362337613d5af710bc0c7533fb PiperOrigin-RevId: 469171223
1 parent d1c5a52 commit 6f547d4

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

lib/java/com/google/android/material/divider/MaterialDividerItemDecoration.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)