Разделители элементов в RecyclerView
- package ua.naiksoftware.view.util;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- import java.util.List;
- /**
- * Created by naik on 23.06.2016.
- */
- public class ItemDividerDecorator extends RecyclerView.ItemDecoration {
- public static final int NO_COLOR = -1;
- public static final int FLAG_DIVIDER_ABOVE = 1 << 1;
- public static final int FLAG_DIVIDER_BELOW = 1 << 2;
- private final OffsetEvaluator mOffsetEvaluator;
- private final int mColor;
- private final int mVerticalOffset;
- private final List<?> mDataSet;
- private final Paint mPaint;
- /**
- * Draw offsets between items in data set
- *
- * @param dataSet items in recycler's view adapter
- * @param dividerColor if -1 transparent
- * @param vertical vertical offset
- * @param offsetEvaluator apply for evaluated items
- */
- public ItemDividerDecorator(List<?> dataSet, int dividerColor, int vertical, OffsetEvaluator offsetEvaluator) {
- mColor = dividerColor;
- mOffsetEvaluator = offsetEvaluator;
- mVerticalOffset = vertical;
- mDataSet = dataSet;
- mPaint = new Paint();
- mPaint.setColor(dividerColor);
- mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
- }
- @Override
- public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
- int childCount = parent.getChildCount();
- if (mColor != NO_COLOR) {
- for (int i = 0; i < childCount; i++) {
- View child = parent.getChildAt(i);
- int adapterPosition = parent.getChildAdapterPosition(child);
- int flags = mOffsetEvaluator.needsOffsetForItem(mDataSet.get(adapterPosition), adapterPosition);
- if (mVerticalOffset > 0) {
- // Draw above
- if ((flags & FLAG_DIVIDER_ABOVE) != 0 && adapterPosition != 0
- && (mOffsetEvaluator.needsOffsetForItem(mDataSet.get(adapterPosition - 1), adapterPosition - 1) & FLAG_DIVIDER_BELOW) == 0) {
- c.drawRect(
- child.getLeft() + child.getTranslationX(),
- child.getTop() - mVerticalOffset + child.getTranslationY(),
- child.getRight() + child.getTranslationX(),
- child.getY() + child.getTranslationY(),
- mPaint);
- }
- // Draw below
- if ((flags & FLAG_DIVIDER_BELOW) != 0 && adapterPosition < mDataSet.size() - 1) {
- c.drawRect(
- child.getLeft() + child.getTranslationX(),
- child.getBottom() + child.getTranslationY(),
- child.getRight() + child.getTranslationX(),
- child.getBottom() + mVerticalOffset + child.getTranslationY(),
- mPaint);
- }
- }
- }
- }
- }
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- int adapterPosition = parent.getChildAdapterPosition(view);
- int flags = mOffsetEvaluator.needsOffsetForItem(mDataSet.get(adapterPosition), adapterPosition);
- if (mVerticalOffset > 0) {
- // Draw above
- if ((flags & FLAG_DIVIDER_ABOVE) != 0 && adapterPosition != 0
- && (mOffsetEvaluator.needsOffsetForItem(mDataSet.get(adapterPosition - 1), adapterPosition - 1) & FLAG_DIVIDER_BELOW) == 0) {
- outRect.set(0, mVerticalOffset, 0, 0);
- }
- // Draw below
- if ((flags & FLAG_DIVIDER_BELOW) != 0 && adapterPosition < mDataSet.size() - 1) {
- outRect.set(0, 0, 0, mVerticalOffset);
- }
- }
- }
- public interface OffsetEvaluator {
- int needsOffsetForItem(Object item, int position);
- }
- }
Использование:
- List<? extends Model> dataSet =...; // Обьекты в списке
- ((RecyclerView) container.findViewById(R.id.recyclerView))
- .addItemDecoration(new ItemDividerDecorator(dataSet, Color.BLACK, 5, (item, position) -> {
- // Некоторая логика и условия если нужно
- return ItemDividerDecorator.FLAG_DIVIDER_BELOW | ItemDividerDecorator.FLAG_DIVIDER_ABOVE;
- }));
Скриншот