Reputation: 349
I cannot find an answer to this question anywhere online, so the answer must be so simple that no one cared to ask it, and I simply don't know it.
Essentially, I want to add a horizontal line between every item in my recyclerView AND to the bottom of the RecyclerView. I have found a way to place a divider between every item in the RecyclerView, but not one at the end.
According to the documentation (found here: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#addItemDecoration(android.support.v7.widget.RecyclerView.ItemDecoration, int) )
putting a negative int into the addItemDecoration(RecyclerView.ItemDecoration decor, int index). should solve this problem, stating:
If this value is negative the decoration will be added at the end.
This is the constructor method for my custom RecyclerView:
public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setHasFixedSize(true);
setLayoutManager(new LinearLayoutManager(context));
decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);
}
As you can see, I am using a negative integer in addItemDecoration(), and I cannot figure out why it is not working the way it is supposed to.
Any help would be greatly appreciated!
Upvotes: 2
Views: 21562
Reputation: 349
As per Janice Kartika's suggestion, I copied and pasted the code into my project. After I copied and pasted Janice's code, Android Studio gave me this suggestion:
Replace with android.support.v7.widge.DividerItemDecoration?
Older versions of the RycyclerView library did not include a divider decorator, but one was provided as a sample in the support demos. This divider class has been widely copy/pasted into various projects.
In recent versions of the support library, the divider decorator is now included, so you can replace custom copies with the "built-in" version, android.support.v7.widget.DividerItemDecoration
So instead of using HorizontalDividerItemDecoration (which was part of an external library that we had imported) like this:
decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);
I used the default "DividerItemDecoration" like this:
decor = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
addItemDecoration(decor);
and it worked. Turns out if you do not put and "int index" into addItemDecoration, it will default to -1 and add the decoration to the bottom by default.
Upvotes: 7
Reputation: 502
I got the same problem like this long time ago and I was able to solve this by copy-pasting DividerItemDecoration
class from this link. But now it seems like some methods are deprecated, so I made a slight modification to it:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
private static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
private 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 + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private void drawHorizontal(Canvas c, 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);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
Copy-paste it to your project, and use it like this:
public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setHasFixedSize(true);
setLayoutManager(new LinearLayoutManager(context));
addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
}
Upvotes: 3