RecyclerView.ItemDecorator左侧填充

时间:2016-03-04 18:47:07

标签: android android-recyclerview

我正在使用https://gist.github.com/polbins/e37206fbc444207c0e92 ItemDecorator在回收站视图中的每个项目下绘制分隔线。然而,它从左到右画一条直线。

@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);
    }
}

如何设置左侧非绝对填充到分隔符,如下图所示?甚至可以使用ItemDecorator吗?

enter image description here

我可以修改这个

int left = parent.getPaddingLeft();

但这绝对是绝对的。有什么想法吗?

现在看起来如何:

enter image description here

项目布局:

<?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>

查看持有人:

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);
    }
}

我正在使用TextView作为带有自定义字体的图标,只是为了您的信息。

1 个答案:

答案 0 :(得分:1)

试一试

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);
    }
}