Forked from alexfu/DividerItemDecoration.java
Last active
September 21, 2015 18:43
-
-
Save charlesmadere/26b02c593fbc27e8eb13 to your computer and use it in GitHub Desktop.
An ItemDecoration that draws dividers between items. Pulled from Android support demos.
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) 2014 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. | |
*/ | |
public class DividerItemDecoration extends RecyclerView.ItemDecoration { | |
public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL; | |
public static final int VERTICAL = LinearLayoutManager.VERTICAL; | |
private static final int[] ATTRS = { android.R.attr.listDivider }; | |
private final Drawable mDivider; | |
private final int mOrientation; | |
public DividerItemDecoration(final Context context) { | |
this(context, VERTICAL); | |
} | |
public DividerItemDecoration(final Context context, final int orientation) { | |
final TypedArray ta = context.obtainStyledAttributes(ATTRS); | |
mDivider = ta.getDrawable(0); | |
ta.recycle(); | |
if (orientation == HORIZONTAL || orientation == VERTICAL) { | |
mOrientation = orientation; | |
} else { | |
throw new IllegalArgumentException("invalid orientation"); | |
} | |
} | |
@Override | |
public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, | |
final RecyclerView.State state) { | |
if (mOrientation == VERTICAL) { | |
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); | |
} else { | |
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); | |
} | |
} | |
@Override | |
public void onDraw(final Canvas c, final RecyclerView parent, final RecyclerView.State state) { | |
if (mOrientation == VERTICAL) { | |
drawVertical(c, parent); | |
} else { | |
drawHorizontal(c, parent); | |
} | |
} | |
public void drawHorizontal(final Canvas c, final RecyclerView parent) { | |
final int top = parent.getPaddingTop(); | |
final int bottom = parent.getHeight() - parent.getPaddingBottom(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int left = child.getRight() + params.rightMargin; | |
final int right = left + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
public void drawVertical(final Canvas c, final RecyclerView parent) { | |
final int left = parent.getPaddingLeft(); | |
final int right = parent.getWidth() - parent.getPaddingRight(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int top = child.getBottom() + params.bottomMargin; | |
final int bottom = top + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment