每个项目多一行的RecyclerView不会给分隔符充气

时间:2015-09-30 11:44:09

标签: android android-recyclerview

我在这里和那里都有RecyclerView,我发现当列表项只有一行DividerItemDecoration时效果非常好,但是当涉及两行项目时它就不会显示出来。

我在这里夸大它:

CenaListRecAdapter mAdapter = new CenaListRecAdapter(getApplicationContext(), cenaLokalList, point);
recyclerView.setAdapter(mAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL_LIST));

和标准DividerItemDecoration类:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{
        android.R.attr.listDivider
};

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

public 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 onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        drawVertical(c, parent);
    } else {
        drawHorizontal(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 + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

public 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, int itemPosition, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}

}

修改:这就是我的行视图项目的样子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/imageView"
        android:transitionName="obrazek"
        android:layout_marginRight="5dp" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:background="?android:attr/selectableItemBackground">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/nazwa"
            android:maxLines="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/odleglosc"
            android:layout_gravity="right"
            android:gravity="end"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text=" "
            android:id="@+id/cena"
            android:layout_below="@+id/nazwa"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>
</LinearLayout>`

2 个答案:

答案 0 :(得分:0)

从官方文档中可能会有所帮助:

  

所有ItemDecorations都是按照添加顺序绘制的   项目视图,位于ItemDecoration#onDraw()ItemDecoration#onDrawOver(Canvas, RecyclerView, RecyclerView.State)

中的项目之后

您应该改为覆盖onDrawOver方法。

答案 1 :(得分:0)

好的,我自己找到了答案。

如果你在Activity(而不是片段)中使用RecyclerView,那么context的{​​{1}}应该是DividerItemDecoration,而不是this - 你可以使用它匿名类 - 在Fragment类中你应该使用getApplicationContext()

哦,在getActivity()之后立即设置装饰并在此之后设置适配器是好的 - 即使您的列表为空。我认为它只是这样工作。