RecyclerView.itemDecoration onDraw c.drawLine incompatible with pre-installed devices?

I am trying to create my own CustomDivider class that extends to RecyclerView

. ItemDecoration is based on Google swatches that have a separator. Luckily, the code I tried works, but I only tried it in an emulator that works in Lollipop

, and I found it doesn't work on my device Kitkat

.

Here is a screenshot of the emulator and the result.

Emulator

Here is a screenshot from my Asus Zenfone 4 running Kitkat.

Zenfone 4

Here is my code:

public class CustomDivider extends RecyclerView.ItemDecoration {

public static final int LINEAR_HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int LINEAR_VERTICAL_LIST = LinearLayoutManager.VERTICAL;
public static final int LINE_FLAT = 0;

private Paint paint = new Paint();
private int color;
private int orientation;
private int top, bottom, left, right;
private int lineType; //TODO LineTypes
private View child;

public CustomDivider(Context context, int orientation, int resourceColor) {
    color = context.getResources().getColor(resourceColor);
    paint.setColor(color);
    paint.setStrokeWidth(3);
    checkOrientation(orientation);
    lineType = LINE_FLAT;
}

private void checkOrientation(int orientation) {
    if (orientation != LINEAR_HORIZONTAL_LIST && orientation != LINEAR_VERTICAL_LIST) {
        throw new IllegalArgumentException("Invalid Orientation");
    } else
        this.orientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);

    if (orientation == LINEAR_HORIZONTAL_LIST) {
        //drawLinearHorizontalDivider(c, parent);
    } else if (orientation == LINEAR_VERTICAL_LIST) {
        drawLinearVerticalDivider(c, parent);
    }

}

private void drawLinearVerticalDivider(Canvas c, RecyclerView parent) {
    left = parent.getLeft();
    right = parent.getWidth();

    for (int i = 0; i < parent.getChildCount(); i++) {
        child = parent.getChildAt(i);
        top = child.getTop();
        bottom = child.getBottom();
        Log.d("Dimensions: ", "Top is: " + top + ", Bottom is " + bottom);
        if (i != parent.getChildCount() - 1) {
            c.drawLine(left, bottom, right, bottom, paint);
        }
    }
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
  }
}

      

Can anyone provide more information on whether I am missing something?

+3


source to share


2 answers


Below is a good example, take a look at it:

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

      

And here is my Snippet, it works well which divisor is clear and subtle:



public static class RecyclerViewFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_recyclerview, container, false);

        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity(), getResources()
            .getStringArray(R.array.countries));
        recyclerView.setAdapter(adapter);

        return root;
    }
}

      

Hope you get inspired.

+2


source


After reading this answer: fooobar.com/questions/2227661 / ...

Which is also indicated for overriding onDrawOver, I edited my code and put my code that was inside onDraw in onDrawOver and found that the divider is now showing in Pre-Lollipop devices.



@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    if (orientation == LINEAR_HORIZONTAL_LIST) {
        //drawLinearHorizontalDivider(c, parent);
    } else if (orientation == LINEAR_VERTICAL_LIST) {
        drawLinearVerticalDivider(c, parent);
    }
}

      

+1


source







All Articles