Last active
July 5, 2020 09:31
-
-
Save Faltenreich/10aa632595371929d8803c3950227fe7 to your computer and use it in GitHub Desktop.
Extension of androidx.recyclerview.widget.DividerItemDecoration that removes the divider after the last item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.view.View; | |
import androidx.recyclerview.widget.RecyclerView; | |
/** | |
* Extension of androidx.recyclerview.widget.DividerItemDecoration that removes the divider after the last item | |
* | |
* DividerItemDecoration is a {@link RecyclerView.ItemDecoration} that can be used as a divider | |
* between items of a {@link androidx.recyclerview.widget.LinearLayoutManager}. It supports both {@link #HORIZONTAL} and | |
* {@link #VERTICAL} orientations. | |
* | |
* <pre> | |
* mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), | |
* mLayoutManager.getOrientation()); | |
* recyclerView.addItemDecoration(mDividerItemDecoration); | |
* </pre> | |
*/ | |
public class DividerItemDecoration extends androidx.recyclerview.widget.DividerItemDecoration { | |
// Workaround: Duplicated properties as they are private in parent | |
private int mOrientation; | |
private final Rect mBounds = new Rect(); | |
public DividerItemDecoration(Context context, int orientation) { | |
super(context, orientation); | |
} | |
@Override | |
public void setOrientation(int orientation) { | |
super.setOrientation(orientation); | |
this.mOrientation = orientation; | |
} | |
@Override | |
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { | |
if (parent.getLayoutManager() == null || getDrawable() == null) { | |
return; | |
} | |
if (mOrientation == VERTICAL) { | |
drawVertical(c, parent); | |
} else { | |
drawHorizontal(c, parent); | |
} | |
} | |
private void drawVertical(Canvas canvas, RecyclerView parent) { | |
Drawable mDivider = getDrawable(); | |
if (mDivider == null) { | |
return; | |
} | |
canvas.save(); | |
final int left; | |
final int right; | |
if (parent.getClipToPadding()) { | |
left = parent.getPaddingLeft(); | |
right = parent.getWidth() - parent.getPaddingRight(); | |
canvas.clipRect(left, parent.getPaddingTop(), right, | |
parent.getHeight() - parent.getPaddingBottom()); | |
} else { | |
left = 0; | |
right = parent.getWidth(); | |
} | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount - 1; i++) { | |
final View child = parent.getChildAt(i); | |
parent.getDecoratedBoundsWithMargins(child, mBounds); | |
final int bottom = mBounds.bottom + Math.round(child.getTranslationY()); | |
final int top = bottom - mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(canvas); | |
} | |
canvas.restore(); | |
} | |
private void drawHorizontal(Canvas canvas, RecyclerView parent) { | |
Drawable mDivider = getDrawable(); | |
if (mDivider == null) { | |
return; | |
} | |
canvas.save(); | |
final int top; | |
final int bottom; | |
if (parent.getClipToPadding()) { | |
top = parent.getPaddingTop(); | |
bottom = parent.getHeight() - parent.getPaddingBottom(); | |
canvas.clipRect(parent.getPaddingLeft(), top, | |
parent.getWidth() - parent.getPaddingRight(), bottom); | |
} else { | |
top = 0; | |
bottom = parent.getHeight(); | |
} | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount - 1; i++) { | |
final View child = parent.getChildAt(i); | |
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); | |
if (layoutManager != null) { | |
layoutManager.getDecoratedBoundsWithMargins(child, mBounds); | |
final int right = mBounds.right + Math.round(child.getTranslationX()); | |
final int left = right - mDivider.getIntrinsicWidth(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(canvas); | |
} | |
} | |
canvas.restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment