@Test public void testGetItemOffsetsSkipBoth() { final ItemSpaceDecoration decoration = new ItemSpaceDecoration(1, 2, 3, 4); final ItemSpaceDecoration.Precondition mockPrecondition = mock(ItemSpaceDecoration.Precondition.class); when(mockPrecondition.check(itemView, mockRecyclerView, mockRecyclerViewState)).thenReturn(true); decoration.setPrecondition(mockPrecondition); decoration.setSkipFirst(true); decoration.setSkipLast(true); final Rect rect = new Rect(); final int itemCount = mockRecyclerViewState.getItemCount(); for (int i = 0; i < itemCount; i++) { when(mockRecyclerView.getChildAdapterPosition(itemView)).thenReturn(i); decoration.getItemOffsets(rect, itemView, mockRecyclerView, mockRecyclerViewState); assertThat(rect.left, is(i == 0 || i == itemCount - 1 ? 0 : 1)); assertThat(rect.right, is(i == 0 || i == itemCount - 1 ? 0 : 2)); assertThat(rect.top, is(i == 0 || i == itemCount - 1 ? 0 : 3)); assertThat(rect.bottom, is(i == 0 || i == itemCount - 1 ? 0 : 4)); } verify(mockPrecondition, times(itemCount - 2)).check(itemView, mockRecyclerView, mockRecyclerViewState); verifyNoMoreInteractions(mockPrecondition); }
@Override public void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) { if (skipFirst || skipLast) { final int position = parent.getChildAdapterPosition(view); if (position == RecyclerView.NO_POSITION) { rect.setEmpty(); return; } if ((skipFirst && position == 0) || (skipLast && position == state.getItemCount() - 1)) { rect.setEmpty(); return; } } if (precondition.check(view, parent, state)) { this.updateItemOffsets(rect, ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL); } else { rect.setEmpty(); } }
ItemSpaceDecoration extends RecyclerViewItemDecoration { @Override public void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) { if (skipFirst || skipLast) { final int position = parent.getChildAdapterPosition(view); if (position == RecyclerView.NO_POSITION) { rect.setEmpty(); return; } if ((skipFirst && position == 0) || (skipLast && position == state.getItemCount() - 1)) { rect.setEmpty(); return; } } if (precondition.check(view, parent, state)) { this.updateItemOffsets(rect, ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL); } else { rect.setEmpty(); } } }
ItemSpaceDecoration extends RecyclerViewItemDecoration { @Override public void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) { if (skipFirst || skipLast) { final int position = parent.getChildAdapterPosition(view); if (position == RecyclerView.NO_POSITION) { rect.setEmpty(); return; } if ((skipFirst && position == 0) || (skipLast && position == state.getItemCount() - 1)) { rect.setEmpty(); return; } } if (precondition.check(view, parent, state)) { this.updateItemOffsets(rect, ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL); } else { rect.setEmpty(); } } ItemSpaceDecoration();  ItemSpaceDecoration(final int horizontal, final int vertical);  ItemSpaceDecoration(final int horizontalStart, final int horizontalEnd, final int verticalStart, final int verticalEnd);  ItemSpaceDecoration(@Nullable final Context context);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr, @StyleRes final int defStyleRes); }
ItemSpaceDecoration extends RecyclerViewItemDecoration { @Override public void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) { if (skipFirst || skipLast) { final int position = parent.getChildAdapterPosition(view); if (position == RecyclerView.NO_POSITION) { rect.setEmpty(); return; } if ((skipFirst && position == 0) || (skipLast && position == state.getItemCount() - 1)) { rect.setEmpty(); return; } } if (precondition.check(view, parent, state)) { this.updateItemOffsets(rect, ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL); } else { rect.setEmpty(); } } ItemSpaceDecoration();  ItemSpaceDecoration(final int horizontal, final int vertical);  ItemSpaceDecoration(final int horizontalStart, final int horizontalEnd, final int verticalStart, final int verticalEnd);  ItemSpaceDecoration(@Nullable final Context context);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr, @StyleRes final int defStyleRes); int getHorizontalStart(); int getHorizontalEnd(); int getVerticalStart(); int getVerticalEnd(); @Override void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state); }
ItemSpaceDecoration extends RecyclerViewItemDecoration { @Override public void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state) { if (skipFirst || skipLast) { final int position = parent.getChildAdapterPosition(view); if (position == RecyclerView.NO_POSITION) { rect.setEmpty(); return; } if ((skipFirst && position == 0) || (skipLast && position == state.getItemCount() - 1)) { rect.setEmpty(); return; } } if (precondition.check(view, parent, state)) { this.updateItemOffsets(rect, ViewCompat.getLayoutDirection(parent) == ViewCompat.LAYOUT_DIRECTION_RTL); } else { rect.setEmpty(); } } ItemSpaceDecoration();  ItemSpaceDecoration(final int horizontal, final int vertical);  ItemSpaceDecoration(final int horizontalStart, final int horizontalEnd, final int verticalStart, final int verticalEnd);  ItemSpaceDecoration(@Nullable final Context context);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr);  ItemSpaceDecoration(@Nullable final Context context, @Nullable final AttributeSet attrs, @AttrRes final int defStyleAttr, @StyleRes final int defStyleRes); int getHorizontalStart(); int getHorizontalEnd(); int getVerticalStart(); int getVerticalEnd(); @Override void getItemOffsets(@NonNull final Rect rect, @NonNull final View view, @NonNull final RecyclerView parent, @NonNull final RecyclerView.State state);  }