com.superrecycleview.superlibrary.utils.SuperDivider.java Source code

Java tutorial

Introduction

Here is the source code for com.superrecycleview.superlibrary.utils.SuperDivider.java

Source

/**
 * Copyright 2016 bingoogolapple
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.superrecycleview.superlibrary.utils;

import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntRange;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.superrecycleview.superlibrary.R;

/**
 * Created by super? on 07/28/16. <br/>
 * blog: http://supercwn.github.io/ <br/>
 * GitHub: https://github.com/supercwn <br/>
 * RecyclerView 
 */
public class SuperDivider extends RecyclerView.ItemDecoration {
    private Drawable mDividerDrawable;
    private int mMarginLeft;
    private int mMarginRight;
    private int mOrientation = LinearLayout.VERTICAL;// ????Grid?
    private int mStartSkipCount = 2;// SuperRecycleView
    private int mEndSkipCount = 1;// ?SuperRecycleView
    private int mSize = 1;

    private SuperDivider(@DrawableRes int drawableResId) {
        mDividerDrawable = ContextCompat.getDrawable(SuperAdapterUtil.getApp(), drawableResId);
        mSize = Math.min(mDividerDrawable.getIntrinsicHeight(), mDividerDrawable.getIntrinsicWidth());
    }

    /**
     *  drawable ?
     *
     * @param drawableResId
     * @return
     */
    public static SuperDivider newDrawableDivider(@DrawableRes int drawableResId) {
        return new SuperDivider(drawableResId);
    }

    /**
     *  shape ?
     *
     * @return
     */
    public static SuperDivider newShapeDivider() {
        return new SuperDivider(R.drawable.bga_adapter_divider_shape);
    }

    /**
     * 
     *
     * @return
     */
    public static SuperDivider newBitmapDivider() {
        return new SuperDivider(R.mipmap.bga_adapter_divider_bitmap);
    }

    /**
     * ???
     *
     * @param bothMarginDp ?? dp
     * @return
     */
    public SuperDivider setBothMarginDp(int bothMarginDp) {
        mMarginLeft = SuperAdapterUtil.dp2px(bothMarginDp);
        mMarginRight = mMarginLeft;
        return this;
    }

    /**
     * ?
     *
     * @param marginLeftDp ?? dp
     * @return
     */
    public SuperDivider setMarginLeftDp(int marginLeftDp) {
        mMarginLeft = SuperAdapterUtil.dp2px(marginLeftDp);
        return this;
    }

    /**
     * ??
     *
     * @param marginRightDp ?? dp
     * @return
     */
    public SuperDivider setMarginRightDp(int marginRightDp) {
        mMarginRight = SuperAdapterUtil.dp2px(marginRightDp);
        return this;
    }

    /**
     * ? id
     *
     * @param resId
     * @return
     */
    public SuperDivider setColor(@ColorRes int resId) {
        return setColorResource(SuperAdapterUtil.getColor(resId));
    }

    /**
     * 
     *
     * @param color
     * @return
     */
    private SuperDivider setColorResource(@ColorInt int color) {
        mDividerDrawable.setColorFilter(color, PorterDuff.Mode.SRC);
        return this;
    }

    /**
     * ?
     *
     * @return
     */
    public SuperDivider setHorizontal() {
        mOrientation = LinearLayout.HORIZONTAL;
        return this;
    }

    /**
     *  Bitmap UI ???
     *
     * @return
     */
    public SuperDivider rotateDivider() {
        if (mDividerDrawable != null && mDividerDrawable instanceof BitmapDrawable) {
            mDividerDrawable = SuperAdapterUtil.rotateBitmap(((BitmapDrawable) mDividerDrawable).getBitmap());
        }
        return this;
    }

    /**
     * ? 1? item 
     *
     * @param startSkipCount
     * @return
     */
    public SuperDivider setStartSkipCount(@IntRange(from = 0) int startSkipCount) {
        mStartSkipCount = startSkipCount;
        if (mStartSkipCount < 0) {
            mStartSkipCount = 0;
        }
        return this;
    }

    /**
     * ?
     *
     * @param endSkipCount
     * @return
     */
    public SuperDivider setEndSkipCount(@IntRange(from = 0) int endSkipCount) {
        mEndSkipCount = endSkipCount;
        if (mEndSkipCount < 0) {
            mEndSkipCount = 0;
        }
        return this;
    }

    /**
     * 
     *
     * @param sizeDp ?? dp
     * @return
     */
    public SuperDivider setSizeDp(int sizeDp) {
        mSize = SuperAdapterUtil.dp2px(sizeDp);
        return this;
    }

    private boolean isNeedSkip(int childAdapterPosition, int realItemCount) {
        int lastPosition = realItemCount - 1;
        // ? mEndSkipCount 
        if (childAdapterPosition > lastPosition - mEndSkipCount) {
            return true;
        }

        // ? mStartSkipCount 
        // 1
        if (childAdapterPosition < mStartSkipCount) {
            return true;
        }
        // ?
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null || parent.getAdapter() == null) {
            return;
        }

        int childAdapterPosition = parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter().getItemCount();

        int realChildAdapterPosition = childAdapterPosition;
        int realItemCount = itemCount;

        if (isNeedSkip(childAdapterPosition, realItemCount)) {
            outRect.set(0, 0, 0, 0);
        } else {
            if (mOrientation == LinearLayout.VERTICAL) {
                getVerticalItemOffsets(outRect);
            } else {
                outRect.set(mSize, 0, 0, 0);
            }
        }
    }

    public void getVerticalItemOffsets(Rect outRect) {
        outRect.set(0, mSize, 0, 0);
    }

    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() == null || parent.getAdapter() == null) {
            return;
        }
        int itemCount = parent.getAdapter().getItemCount();
        if (mOrientation == LinearLayout.VERTICAL) {
            drawVertical(canvas, parent, itemCount);
        } else {
            drawVertical(canvas, parent, itemCount);
        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent, int itemCount) {
        int dividerLeft = parent.getPaddingLeft() + mMarginLeft;
        int dividerRight = parent.getWidth() - parent.getPaddingRight() - mMarginRight;
        View itemView;
        RecyclerView.LayoutParams itemLayoutParams;

        for (int childPosition = 0; childPosition < itemCount; childPosition++) {
            itemView = parent.getChildAt(childPosition);
            if (itemView == null || itemView.getLayoutParams() == null) {
                continue;
            }

            int childAdapterPosition = parent.getChildAdapterPosition(itemView);

            if (isNeedSkip(childAdapterPosition, itemCount)) {
                continue;
            }

            itemLayoutParams = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            int dividerBottom = itemView.getTop() - itemLayoutParams.topMargin;

            drawVertical(canvas, dividerLeft, dividerRight, dividerBottom);
        }
    }

    public void drawVertical(Canvas canvas, int dividerLeft, int dividerRight, int itemTop) {
        int dividerBottom = itemTop;
        int dividerTop = dividerBottom - mSize;
        mDividerDrawable.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
        mDividerDrawable.draw(canvas);
    }

}