RecyclerView分隔符在更改项目背景颜色时丢失

问题描述 投票:0回答:1

我具有RecyclerView项目装饰,并添加了我的RecyclerView。更改项目背景颜色时,RecyclerView分隔线丢失。

我的带有分隔线的RecyclerView:enter image description here

选择RecyclerView项目时,不显示分隔线:enter image description here

setBackgroundColor:

private void setBackgroundColor() {

    if (selectedItem.isEmpty() || !selectedItem.contains(item)) {
        this.setBackgroundColor(Color.TRANSPARENT);
    } else {
        this.setBackgroundColor(0xffffedc7);
    }
}

DividerItemDecoration:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;

    public DividerItemDecoration(int resId) {
        mDivider = ContextCompat.getDrawable(MyApplication.getContext(), resId);
    }

    @Override
    public void onDraw(@NonNull Canvas canvas, RecyclerView parent, @NonNull RecyclerView.State state) {
        int left = MyApplication.getContext().getResources().getDimensionPixelSize(R.dimen.profileImageSizeDivider);
        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(canvas);
        }
    }
}
android android-recyclerview divider
1个回答
0
投票

尝试一下:

/**
 * Draws left or right inset dividers at the bottom of every recycler item view. Only supports
 * vertical orientations.
 */
public class InsetDividerItemDecoration extends RecyclerView.ItemDecoration {

    private int mLeftInset = 0;
    private int mRightInset = 0;
    private int mStartPosition = 0;

    final private Paint mLinePaint = new Paint();
    final private List<Integer> mPositionsToIgnore = new ArrayList<>();

    public InsetDividerItemDecoration(int leftInset, int rightInset, int dividerColor) {
        mLeftInset = leftInset;
        mRightInset = rightInset;
        setColor(dividerColor);
    }

    public InsetDividerItemDecoration(int leftInset, int rightInset, int dividerColor, int startPosition) {
        mLeftInset = leftInset;
        mRightInset = rightInset;
        mStartPosition = startPosition;
        setColor(dividerColor);
    }

    public InsetDividerItemDecoration(int leftInset, int rightInset, int dividerColor, int startPosition, int dividerHeight) {
        mLeftInset = leftInset;
        mRightInset = rightInset;
        mStartPosition = startPosition;
        setColor(dividerColor);
        setDividerHeight(dividerHeight);
    }

    public InsetDividerItemDecoration(int leftInset, int rightInset) {
        this(leftInset, rightInset, 0);
    }

    public int getColor() {
        return mLinePaint.getColor();
    }

    public void setColor(int color) {
        mLinePaint.setColor(color);
    }

    public int getLeftInset() {
        return mLeftInset;
    }

    public void setLeftInset(int leftInset) {
        mLeftInset = leftInset;
    }

    public int getRightInset() {
        return mRightInset;
    }

    public void setRightInset(int rightInset) {
        mRightInset = rightInset;
    }

    public void setDividerHeight(int height) {
        mLinePaint.setStrokeWidth(height);
    }

    public int getDividerHeight() {
        return (int) mLinePaint.getStrokeWidth();
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int visibleItems = parent.getLayoutManager().getChildCount();

        for (int i = 0; i < visibleItems; i++) {
            View itemView = parent.getLayoutManager().getChildAt(i);
            final int adapterPosition = parent.getChildAdapterPosition(itemView);

            final boolean shouldDraw = adapterPosition != RecyclerView.NO_POSITION
                    && adapterPosition != parent.getAdapter().getItemCount() - 1
                    && adapterPosition >= mStartPosition
                    && !shouldIgnoreAdapterPosition(adapterPosition);

            if (shouldDraw) {
                Rect itemRect = new Rect();
                itemView.getDrawingRect(itemRect);

                parent.offsetDescendantRectToMyCoords(itemView, itemRect);

                final int lineStartX = mLeftInset;
                final int lineStartY = itemRect.bottom;

                final int lineEndX = itemRect.right - mRightInset;
                final int lineEndY = lineStartY;

                c.drawLine(lineStartX, lineStartY, lineEndX, lineEndY, mLinePaint);
            }
        }
    }

    protected boolean shouldIgnoreAdapterPosition(int position) {
        return mPositionsToIgnore.size() > 0 && mPositionsToIgnore.contains(position);
    }

    public void setStartPosition(int startPosition) {
        mStartPosition = startPosition;
    }

    public void setPositionsToIgnore(List<Integer> positionsToIgnore) {
        mPositionsToIgnore.clear();
        if (positionsToIgnore != null) {
            mPositionsToIgnore.addAll(positionsToIgnore);
        }
    }

    public void setPositionsToIgnore(Integer[] positionsToIgnore) {
        setPositionsToIgnore(Arrays.asList(positionsToIgnore));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.