DividerDecoration在完整背景下绘制

时间:2015-05-07 12:14:52

标签: android android-recyclerview appcompat-v7-r22.1

我在物品之间使用了一些带有分隔物的RecyclerView。我使用DividerDecorator绘制它们。但是在较旧的Android版本(API 8)上,它以我的分隔符颜色绘制完整的背景。这是我的代码:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

private final Drawable mDivider;
private final int mHeight;
private int mOrientation;

public DividerItemDecoration( final Context context, final int orientation) {
    mDivider = new ColorDrawable( context.getResources().getColor( R.color.divider) );
    mHeight = dpToPx( 1f, context.getResources() );
    setOrientation(orientation);
}

private int dpToPx( final float dp, final Resources resource ) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resource.getDisplayMetrics() );
    return Math.max( 1, (int) px );
}

public void setOrientation( final int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
        throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
}

@Override
public void onDraw( final Canvas c, final RecyclerView parent, final RecyclerView.State state) {
    if( mOrientation == VERTICAL_LIST ) {
        drawVertical(c, parent);
    } else {
        drawHorizontal(c, parent);
    }
}

public void drawVertical( final Canvas c, final RecyclerView parent ) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
    final int childCount = parent.getChildCount();

    View child;
    RecyclerView.LayoutParams params;
    int top;
    int bottom;

    for( int position = 0; position < childCount; position++) {
        child = parent.getChildAt(position);
        params = (RecyclerView.LayoutParams) child.getLayoutParams();
        top = child.getBottom() + params.bottomMargin;
        bottom = top + mHeight;
        mDivider.setBounds( left, top, right, bottom );
        mDivider.draw(c);
    }
}

public void drawHorizontal( final Canvas c, final RecyclerView parent ) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();
    final int childCount = parent.getChildCount();

    View child;
    RecyclerView.LayoutParams params;
    int left;
    int right;

    for( int position = 0; position < childCount; position++) {
        child = parent.getChildAt( position );
        params = (RecyclerView.LayoutParams) child.getLayoutParams();
        left = child.getRight() + params.rightMargin;
        right = left + mHeight;
        mDivider.setBounds( left, top, right, bottom );
        mDivider.draw( c );
    }
}

@Override
public void getItemOffsets( final Rect outRect, final View view, final RecyclerView parent, final RecyclerView.State state) {
    if (mOrientation == VERTICAL_LIST) {
        outRect.set( 0 /*left*/, 0 /*top*/, 0 /*right*/, mHeight /*bottom*/ );
    } else {
        outRect.set( 0 /*left*/, 0 /*top*/, mHeight /*right*/, 0 /*bottom*/ );
    }
}
}

Right behavior Wrong behavior

这是我从分隔符坐标的日志,它们看起来是正确的:

DividerItemDecoration:divider = 0,left = 0,top = 66,right = 240,bottom = 67
DividerItemDecoration:divider = 1,left = 0,top = 133,right = 240,bottom = 134
DividerItemDecoration:divider = 2,left = 0,top = 200,right = 240,bottom = 201
DividerItemDecoration:divider = 3,left = 0,top = 267,right = 240,bottom = 268

0 个答案:

没有答案