content: /*
   *
   *  * Copyright  2017 [AllenCoderr]
   *  *
   *  * 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.allen.appmanager;
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.graphics.Canvas;
  import android.graphics.Rect;
  import android.graphics.drawable.Drawable;
  import android.os.Build;
  import android.support.annotation.DrawableRes;
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.StaggeredGridLayoutManager;
  import android.view.View;
  
  /**
   * RecyclerView的GridLayoutManager和StaggeredGridLayoutManager的Item分割线
   */
  public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
  
      private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
      private Drawable mDivider;
  
      public DividerGridItemDecoration(Context context) {
          final TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
          mDivider = typedArray.getDrawable(0);
          typedArray.recycle();
      }
  
      public DividerGridItemDecoration(Context context, @DrawableRes int resId) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              mDivider = context.getDrawable(resId);
          } else {
              mDivider = context.getResources().getDrawable(resId);
          }
      }
  
      @Override
      public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
          drawHorizontal(canvas, parent);
          drawVertical(canvas, parent);
      }
  
      private int getSpanCount(RecyclerView parent) { // 列数
          int spanCount = -1;
          RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
          if (layoutManager instanceof GridLayoutManager) {
              spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
          } else if (layoutManager instanceof StaggeredGridLayoutManager) {
              spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
          }
          return spanCount;
      }
  
      private void drawHorizontal(Canvas c, RecyclerView parent) {
          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.getLeft() - params.leftMargin;
              final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();
              final int top = child.getBottom() + params.bottomMargin;
              final int bottom = top + mDivider.getIntrinsicHeight();
              mDivider.setBounds(left, top, right, bottom);
              mDivider.draw(c);
          }
      }
  
      private void drawVertical(Canvas c, RecyclerView parent) {
          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.getTop() - params.topMargin;
              final int bottom = child.getBottom() + params.bottomMargin;
              final int left = child.getRight() + params.rightMargin;
              final int right = left + mDivider.getIntrinsicWidth();
  
              mDivider.setBounds(left, top, right, bottom);
              mDivider.draw(c);
          }
      }
  
      private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
          RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
          if (layoutManager instanceof GridLayoutManager) {
              if ((pos + 1) % spanCount == 0) { // 如果是最后一列，则不需要绘制右边
                  return true;
              }
          } else if (layoutManager instanceof StaggeredGridLayoutManager) {
              int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
              if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                  if ((pos + 1) % spanCount == 0) { // 如果是最后一列，则不需要绘制右边
                      return true;
                  }
              } else {
                  childCount = childCount - childCount % spanCount;
                  if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
                      return true;
              }
          }
          return false;
      }
  
      private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
          RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
          if (layoutManager instanceof GridLayoutManager) {
              childCount = childCount - childCount % spanCount;
              if (pos >= childCount)// 如果是最后一行，则不需要绘制底部
                  return true;
          } else if (layoutManager instanceof StaggeredGridLayoutManager) {
              int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation(); // StaggeredGridLayoutManager 且纵向滚动
              if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                  childCount = childCount - childCount % spanCount; // 如果是最后一行，则不需要绘制底部
                  if (pos >= childCount) {
                      return true;
                  }
              } else { // StaggeredGridLayoutManager 且横向滚动
                  if ((pos + 1) % spanCount == 0) { // 如果是最后一行，则不需要绘制底部
                      return true;
                  }
              }
          }
          return false;
      }
  
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
          int spanCount = getSpanCount(parent);
          int childCount = parent.getAdapter().getItemCount();
          int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
          if (isLastRaw(parent, itemPosition, spanCount, childCount)) { // 如果是最后一行，则不需要绘制底部
              outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
          } else if (isLastColum(parent, itemPosition, spanCount, childCount)) { // 如果是最后一列，则不需要绘制右边
              outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
          } else {
              outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
          }
      }
  
  }
  