Skip to content

Commit b08257b

Browse files
leticiarossidrchen
authored andcommitted
[Divider] Change shouldDrawDivider from private to protected to allow for more user flexibility.
PiperOrigin-RevId: 490032010
1 parent 2977a75 commit b08257b

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ public void getItemOffsets(
374374
@NonNull RecyclerView parent,
375375
@NonNull RecyclerView.State state) {
376376
outRect.set(0, 0, 0, 0);
377-
if (shouldAddOffset(parent, view, state)) {
377+
// Only add offset if there's a divider displayed.
378+
if (shouldDrawDivider(parent, view)) {
378379
if (orientation == VERTICAL) {
379380
outRect.bottom = thickness;
380381
} else {
@@ -384,22 +385,25 @@ public void getItemOffsets(
384385
}
385386

386387
private boolean shouldDrawDivider(@NonNull RecyclerView parent, @NonNull View child) {
387-
if (lastItemDecorated) {
388-
return true;
389-
}
390388
int position = parent.getChildAdapterPosition(child);
391389
RecyclerView.Adapter<?> adapter = parent.getAdapter();
390+
boolean isLastItem = adapter != null && position == adapter.getItemCount() - 1;
391+
392392
return position != RecyclerView.NO_POSITION
393-
&& adapter != null
394-
&& position != adapter.getItemCount() - 1;
393+
&& (!isLastItem || lastItemDecorated)
394+
&& shouldDrawDivider(position, adapter);
395395
}
396396

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;
397+
/**
398+
* Whether a divider should be drawn below the current item that is being drawn.
399+
*
400+
* <p>Note: if lasItemDecorated is false, the divider below the last item will never be drawn even
401+
* if this method returns true.
402+
*
403+
* @param position the position of the current item being drawn.
404+
* @param adapter the {@link RecyclerView.Adapter} associated with the item being drawn.
405+
*/
406+
protected boolean shouldDrawDivider(int position, @Nullable RecyclerView.Adapter<?> adapter) {
407+
return true;
404408
}
405409
}

lib/javatests/com/google/android/material/divider/MaterialDividerItemDecorationTest.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@
2222

2323
import android.content.Context;
2424
import android.graphics.Color;
25-
import android.graphics.Rect;
26-
import androidx.recyclerview.widget.RecyclerView;
27-
import android.view.View;
2825
import android.widget.LinearLayout;
2926
import androidx.test.core.app.ApplicationProvider;
3027
import org.junit.Before;
3128
import org.junit.Rule;
3229
import org.junit.Test;
3330
import org.junit.rules.ExpectedException;
3431
import org.junit.runner.RunWith;
35-
import org.mockito.Mock;
3632
import org.robolectric.RobolectricTestRunner;
3733

3834
/** Tests for {@link MaterialDividerItemDecoration}. */
@@ -46,16 +42,11 @@ public final class MaterialDividerItemDecorationTest {
4642

4743
private final Context context = ApplicationProvider.getApplicationContext();
4844

49-
@Mock private RecyclerView.State state;
50-
@Mock private View view;
51-
@Mock private RecyclerView recyclerView;
52-
private Rect rect;
5345
private MaterialDividerItemDecoration divider;
5446

5547
@Before
5648
public void themeApplicationContext() {
5749
context.setTheme(R.style.Theme_MaterialComponents_Light_NoActionBar_Bridge);
58-
rect = new Rect();
5950
divider = new MaterialDividerItemDecoration(context, LinearLayout.VERTICAL);
6051
divider.setDividerThickness(DIVIDER_THICKNESS);
6152
}
@@ -140,20 +131,4 @@ public void setLastItemNotDecorated_succeeds() {
140131

141132
assertThat(divider.isLastItemDecorated()).isFalse();
142133
}
143-
144-
@Test
145-
public void getItemOffsets_verticalOrientation_returnsCorrectRect() {
146-
divider.getItemOffsets(rect, view, recyclerView, state);
147-
148-
assertThat(rect).isEqualTo(new Rect(0, 0, 0, DIVIDER_THICKNESS));
149-
}
150-
151-
@Test
152-
public void getItemOffsets_horizontalOrientation_returnsCorrectRect() {
153-
divider.setOrientation(LinearLayout.HORIZONTAL);
154-
155-
divider.getItemOffsets(rect, view, recyclerView, state);
156-
157-
assertThat(rect).isEqualTo(new Rect(0, 0, DIVIDER_THICKNESS, 0));
158-
}
159134
}

0 commit comments

Comments
 (0)