-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kentaro Takiguchi
committed
May 29, 2015
1 parent
0c8aae0
commit 6cb85ef
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
app/src/main/java/rejasupotaro/mds/view/components/DividerItemDecoration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package rejasupotaro.mds.view.components; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Canvas; | ||
import android.graphics.Rect; | ||
import android.graphics.drawable.Drawable; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
public class DividerItemDecoration extends RecyclerView.ItemDecoration { | ||
|
||
private static final int[] ATTRS = new int[]{ | ||
android.R.attr.listDivider | ||
}; | ||
|
||
private Drawable divider; | ||
|
||
public DividerItemDecoration(Context context) { | ||
final TypedArray a = context.obtainStyledAttributes(ATTRS); | ||
divider = a.getDrawable(0); | ||
a.recycle(); | ||
} | ||
|
||
@Override | ||
public void onDraw(Canvas c, RecyclerView parent) { | ||
drawVertical(c, parent); | ||
} | ||
|
||
public void drawVertical(Canvas c, 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 + divider.getIntrinsicHeight(); | ||
divider.setBounds(left, top, right, bottom); | ||
divider.draw(c); | ||
} | ||
} | ||
|
||
@Override | ||
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | ||
outRect.set(0, 0, 0, divider.getIntrinsicHeight()); | ||
} | ||
} |
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