com.mad.splitlist.util.DividerItemDecoration.java Source code

Java tutorial

Introduction

Here is the source code for com.mad.splitlist.util.DividerItemDecoration.java

Source

/*
 * Copyright (C) 2016 Kenneth Wong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mad.splitlist.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mad.splitlist.R;

/**
 * Creates a divider between each RecyclerView item using the line divider drawable. Overrides
 * methods found in ItemDecorator.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int STROKE_WIDTH = 1;
    private static final int DIVIDE_OFFSET = 2;

    private final Paint mPaint;
    private final int mAlpha;

    public DividerItemDecoration(Context context) {
        mPaint = new Paint();
        mPaint.setColor(ContextCompat.getColor(context, R.color.blackDivider));
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mAlpha = mPaint.getAlpha();
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        // Position in the list.
        final int position = params.getViewAdapterPosition();

        // Add space for the separator except the last.
        if (position < state.getItemCount()) {
            outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth());
        } else {
            outRect.setEmpty();
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int offset = (int) (mPaint.getStrokeWidth() / DIVIDE_OFFSET);

        for (int i = 0; i < parent.getChildCount(); i++) {
            final View view = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

            final int position = params.getViewAdapterPosition();

            // Draw separator using paint.
            if (position < state.getItemCount()) {
                // apply alpha to support animations
                mPaint.setAlpha((int) (view.getAlpha() * mAlpha));

                float positionY = view.getBottom() + offset + view.getTranslationY();
                // do the drawing
                c.drawLine(view.getLeft() + view.getTranslationX(), positionY,
                        view.getRight() + view.getTranslationX(), positionY, mPaint);
            }
        }
    }
}