|
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.LinearLayoutManager; |
|
import android.support.v7.widget.RecyclerView; |
|
import android.view.View; |
|
|
|
public class SimpleRecyclerViewDecoration extends RecyclerView.ItemDecoration { |
|
private Drawable mDivider; |
|
private boolean mShowFirstDivider = false; |
|
private boolean mShowLastDivider = false; |
|
|
|
public SimpleRecyclerViewDecoration(Context context){ |
|
final TypedArray a = context.obtainStyledAttributes(null, new int[]{android.R.attr.listDivider}); |
|
mDivider = a.getDrawable(0); |
|
a.recycle(); |
|
} |
|
|
|
public SimpleRecyclerViewDecoration(Context context, boolean showFirstDivider, boolean showLastDivider) { |
|
this(context); |
|
mShowFirstDivider = showFirstDivider; |
|
mShowLastDivider = showLastDivider; |
|
} |
|
|
|
public SimpleRecyclerViewDecoration(Drawable divider) { |
|
mDivider = divider; |
|
} |
|
|
|
public SimpleRecyclerViewDecoration(Drawable divider, boolean showFirstDivider, boolean showLastDivider) { |
|
this(divider); |
|
mShowFirstDivider = showFirstDivider; |
|
mShowLastDivider = showLastDivider; |
|
} |
|
|
|
@Override |
|
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { |
|
|
|
int currentItem = parent.getChildAdapterPosition(view); |
|
int totalItem = parent.getAdapter().getItemCount(); |
|
int orientation = getOrientation(parent); |
|
|
|
if (orientation == LinearLayoutManager.VERTICAL) { |
|
int mDividerHeight = mDivider.getIntrinsicHeight(); |
|
if(totalItem == 1){ |
|
if(mShowFirstDivider) outRect.top = mDividerHeight; |
|
if(mShowLastDivider) outRect.bottom = mDividerHeight; |
|
}else if(currentItem == 0 && mShowFirstDivider){ |
|
outRect.set(0, mDividerHeight, 0, mDividerHeight); |
|
}else if(currentItem == totalItem-1 && mShowLastDivider) { |
|
outRect.bottom = mDividerHeight; |
|
}else if(currentItem != totalItem-1 ){ |
|
outRect.bottom = mDividerHeight; |
|
} |
|
|
|
} else { |
|
int mDividerWidth = mDivider.getIntrinsicWidth(); |
|
if(totalItem == 1){ |
|
if(mShowFirstDivider) outRect.left = mDividerWidth; |
|
if(mShowLastDivider) outRect.right = mDividerWidth; |
|
}else if(currentItem == 0 && mShowFirstDivider){ |
|
outRect.set(mDividerWidth, 0, mDividerWidth, 0); |
|
}else if(currentItem == totalItem-1 && mShowLastDivider) { |
|
outRect.right = mDividerWidth; |
|
}else if(currentItem != totalItem-1 ){ |
|
outRect.right = mDividerWidth; |
|
} |
|
} |
|
|
|
} |
|
|
|
@Override |
|
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { |
|
int orientation = getOrientation(parent); |
|
int childCount = parent.getChildCount(); |
|
|
|
for (int i = 0; i < childCount; i++) { |
|
View child = parent.getChildAt(i); |
|
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); |
|
|
|
if(orientation == LinearLayoutManager.VERTICAL){ |
|
if(childCount == 1){ |
|
if(mShowFirstDivider) drawVerticalLayoutDivider_Top(parent, child, c, params); |
|
if(mShowLastDivider) drawVerticalLayoutDivider_Bottom(parent, child, c, params); |
|
} |
|
else if(i == 0 && mShowFirstDivider) { |
|
drawVerticalLayoutDivider_Top(parent, child, c, params); |
|
drawVerticalLayoutDivider_Bottom(parent, child, c, params); |
|
} |
|
else if(i == childCount-1 && mShowLastDivider) { |
|
drawVerticalLayoutDivider_Bottom(parent, child, c, params); |
|
} |
|
else if(i != childCount-1){ |
|
drawVerticalLayoutDivider_Bottom(parent, child, c, params); |
|
} |
|
}else { |
|
if(childCount == 1){ |
|
if(mShowFirstDivider) drawHorizontalLayoutDivider_Left(parent, child, c, params); |
|
if(mShowLastDivider) drawHorizontalLayoutDivider_Right(parent, child, c, params); |
|
} |
|
else if(i == 0 && mShowFirstDivider) { |
|
drawHorizontalLayoutDivider_Left(parent, child, c, params); |
|
drawHorizontalLayoutDivider_Right(parent, child, c, params); |
|
} |
|
else if(i == childCount-1 && mShowLastDivider) { |
|
drawHorizontalLayoutDivider_Right(parent, child, c, params); |
|
} |
|
else if(i != childCount-1){ |
|
drawHorizontalLayoutDivider_Right(parent, child, c, params); |
|
} |
|
} |
|
} |
|
} |
|
|
|
private void drawVerticalLayoutDivider_Top(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){ |
|
int left = parent.getPaddingLeft(); |
|
int right = parent.getWidth() - parent.getPaddingRight(); |
|
int top = child.getTop() - params.topMargin - mDivider.getIntrinsicHeight(); |
|
int bottom = top + mDivider.getIntrinsicHeight(); |
|
mDivider.setBounds(left, top, right, bottom); |
|
mDivider.draw(c); |
|
} |
|
|
|
private void drawVerticalLayoutDivider_Bottom(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){ |
|
int left = parent.getPaddingLeft(); |
|
int right = parent.getWidth() - parent.getPaddingRight(); |
|
int top = child.getBottom() + params.topMargin; |
|
int bottom = top + mDivider.getIntrinsicHeight(); |
|
mDivider.setBounds(left, top, right, bottom); |
|
mDivider.draw(c); |
|
} |
|
|
|
private void drawHorizontalLayoutDivider_Left(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){ |
|
int left = child.getLeft() - params.leftMargin - mDivider.getIntrinsicWidth(); |
|
int right = left + mDivider.getIntrinsicWidth(); |
|
int top = parent.getPaddingTop(); |
|
int bottom = parent.getHeight() - parent.getPaddingBottom(); |
|
mDivider.setBounds(left, top, right, bottom); |
|
mDivider.draw(c); |
|
} |
|
|
|
private void drawHorizontalLayoutDivider_Right(RecyclerView parent, View child, Canvas c, RecyclerView.LayoutParams params){ |
|
int left = child.getRight() + params.rightMargin; |
|
int right = left + mDivider.getIntrinsicWidth(); |
|
int top = parent.getPaddingTop(); |
|
int bottom = parent.getHeight() - parent.getPaddingBottom(); |
|
mDivider.setBounds(left, top, right, bottom); |
|
mDivider.draw(c); |
|
} |
|
|
|
private int getOrientation(RecyclerView parent) { |
|
if (parent.getLayoutManager() instanceof LinearLayoutManager) { |
|
LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager(); |
|
return layoutManager.getOrientation(); |
|
} else { |
|
throw new IllegalStateException("SimpleRecyclerViewDecoration can only be used with a LinearLayoutManager."); |
|
} |
|
} |
|
|
|
} |