Reputation: 70416
I am using the https://gist.github.com/polbins/e37206fbc444207c0e92 ItemDecorator to draw a separator line under each item in a recycler view. However it draws a straight line from left to right.
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
How can I set a left non absolute padding to the separator like in the picture below? Is this even possible using ItemDecorator?
I could modify this
int left = parent.getPaddingLeft();
but it would be absolute. Any ideas?
How it looks like right now:
Item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:clickable="true"
android:background="@drawable/recycler_item_background">
<TextView
android:id="@+id/mTextViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="@string/fa_search"
android:textSize="18sp"/>
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/mTextViewIcon"
android:layout_toEndOf="@id/mTextViewIcon"
android:orientation="vertical"
android:paddingLeft="25dp"
android:paddingStart="25dp">
<TextView
android:id="@+id/mTextViewTitle"
style="@style/Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mTextViewSubtitle"
style="@style/SmallGray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
View holder:
public static class ItemViewHolder extends BaseViewHolder {
@Bind(R.id.mTextViewIcon)
TextView mTextViewIcon;
@Bind(R.id.mTextViewTitle)
TextView mTextViewTitle;
@Bind(R.id.mTextViewSubtitle)
TextView mTextViewSubtitle;
public ParkzoneViewHolder(View mView) {
super(mView);
ButterKnife.bind(this, mView);
}
}
I am using TextView for the icon with a custom typeface, just for you're information.
Upvotes: 0
Views: 633
Reputation: 8277
Give this a try
public static class ItemViewHolder extends BaseViewHolder {
@Bind(R.id.mTextViewIcon)
TextView mTextViewIcon;
@Bind(R.id.mTextViewTitle)
TextView mTextViewTitle;
@Bind(R.id.mTextViewSubtitle)
TextView mTextViewSubtitle;
@Bind(R.id.wrapper)
LinearLayout mWrapperLayout;
public ParkzoneViewHolder(View mView) {
super(mView);
ButterKnife.bind(this, mView);
}
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
// You can get view holders from recyclerview in different ways,
// parent.getChildViewHolder(child)
// im thinking parent.getChildViewHolder(child)
// is reliable and so ill use that here.
// but you be sure to test it with the other methods too
// if the getChildViewHolder doesnt work
View child = parent.getChildAt(i);
ItemViewHolder holder = (ItemViewHolder) parent.getChildViewHolder(child);
int left = parent.getPaddingLeft() + child.getPaddingLeft()
+ holder.mTextViewIcon.getPaddingLeft() + holder.mTextViewIcon.getWidth()
+ holder.mWrapperLayout.getPaddingLeft();
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
Upvotes: 2