Skip to content

Commit

Permalink
[Divider] Fixed extra space when lastItemDecorated = false
Browse files Browse the repository at this point in the history
Resolves #2511

PiperOrigin-RevId: 444556765
  • Loading branch information
Material Design Team authored and dsn5ft committed Apr 26, 2022
1 parent b2c3dd6 commit 714521b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Expand Up @@ -371,10 +371,12 @@ public void getItemOffsets(
@NonNull RecyclerView parent,
@NonNull RecyclerView.State state) {
outRect.set(0, 0, 0, 0);
if (orientation == VERTICAL) {
outRect.bottom = dividerDrawable.getIntrinsicHeight() + thickness;
} else {
outRect.right = dividerDrawable.getIntrinsicWidth() + thickness;
if (lastItemDecorated || parent.getChildLayoutPosition(view) != state.getItemCount() - 1) {
if (orientation == VERTICAL) {
outRect.bottom = dividerDrawable.getIntrinsicHeight() + thickness;
} else {
outRect.right = dividerDrawable.getIntrinsicWidth() + thickness;
}
}
}
}
Expand Up @@ -24,6 +24,7 @@
import android.graphics.Color;
import android.graphics.Rect;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
Expand All @@ -46,6 +47,7 @@ public final class MaterialDividerItemDecorationTest {
private final Context context = ApplicationProvider.getApplicationContext();

@Mock private RecyclerView.State state;
@Mock private View view;
@Mock private RecyclerView recyclerView;
private Rect rect;
private MaterialDividerItemDecoration divider;
Expand Down Expand Up @@ -141,7 +143,7 @@ public void setLastItemNotDecorated_succeeds() {

@Test
public void getItemOffsets_verticalOrientation_returnsCorrectRect() {
divider.getItemOffsets(rect, /* view= */ null, recyclerView, state);
divider.getItemOffsets(rect, view, recyclerView, state);

assertThat(rect).isEqualTo(new Rect(0, 0, 0, DIVIDER_THICKNESS));
}
Expand All @@ -150,7 +152,7 @@ public void getItemOffsets_verticalOrientation_returnsCorrectRect() {
public void getItemOffsets_horizontalOrientation_returnsCorrectRect() {
divider.setOrientation(LinearLayout.HORIZONTAL);

divider.getItemOffsets(rect, /* view= */ null, recyclerView, state);
divider.getItemOffsets(rect, view, recyclerView, state);

assertThat(rect).isEqualTo(new Rect(0, 0, DIVIDER_THICKNESS, 0));
}
Expand Down

0 comments on commit 714521b

Please sign in to comment.