适配RecycleView的 ItemDecoration 的封装

1. ItemDecoration类的定义

ItemDecoration-->RecycleView的item系统间距以及系统item绘制封装类

/**
 * ItemDecoration允许程序对适配器的数据源中的特殊的条目      
 * view 添加一些特别的图形和布局的偏移,这被用于在条目之      
 * 间画间隔线,高亮显示,视觉分组界限和更多。
 * 所有的ItemDecorations
 * <p>所有的ItemDecorations都是按照它们添加的顺序绘制的, 
 * 在所有的条目view绘制之前
*  (in {@link ItemDecoration#onDraw(Canvas, RecyclerView, RecyclerView.State) onDraw()}
 * 在所有的条目view绘制之后 (in {@link ItemDecoration#onDrawOver(Canvas, RecyclerView,
 * RecyclerView.State)}.</p>
 */
public abstract static class ItemDecoration {
   ...
   ...
}

2. ItemDecoration的方法分析

此类下的六个方法有三个为过时方法,只需要复写另外三个未过时的方法即可,谷歌对这三个方法的定义如下:

  • 在提供给RecyclerView的Canvas中绘制任何适当的装饰。 此方法绘制的任何内容将在绘制项目视图之前绘制, 因此将被视图覆盖.
  public void onDraw(Canvas c, RecyclerView parent, State state{
      onDraw(c, parent);
  }
  • 在提供给RecyclerView的Canvas中绘制任何适当的装饰。 此方法绘制的任何内容将在绘制项目视图之后绘制, 因此将覆盖视图
  public void onDrawOver(Canvas c, RecyclerView parent, State state) {
        onDrawOver(c, parent);
  }
  • 检索给定条目的所有偏移量, outRect 的每个字段指定条目视图应该被插入的像素大小,类似于填充或边距。默认实现将outRect的边界设置为0并返回。
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
        getItemOffsets(outRect, ((LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent);
  }

注:onDraw 和 onDrawOver的区别在于,若未规定条目视图的左上右下的偏移量,在条目视图的基础上,使用canvas 所做的任何装饰,前者会被条目视图所覆盖,而后者方法则会在条目视图的基础上作画。

3. 封装 getItemOffsets

  • 通过构造函数传入左上右下的间距
private int   mSpaceHorizontal;
private int   mSpaceVertical;
private Paint mPaint;

DividerDecoration(Activity activity, int spaceHorizontal, int spaceVertical) {
    mSpaceHorizontal = spaceHorizontal;
    mSpaceVertical = spaceVertical;
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);
    mPaint.setColor(ContextCompat.getColor(activity, android.R.color.transparent));
}

DividerDecoration(Activity activity, int color, int spaceHorizontal, int spaceVertical) {
    mSpaceHorizontal = spaceHorizontal;
    mSpaceVertical = spaceVertical;
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);
    mPaint.setColor(ContextCompat.getColor(activity, color));
}
  • 筛选RecycleView的布局管理器 ,对应做单独处理
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
      super.getItemOffsets(outRect, view, parent, state);
      RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
      if (layoutManager instanceof GridLayoutManager) {
          layoutGridItem(outRect, view, parent, (GridLayoutManager) layoutManager);
      } else if (layoutManager instanceof LinearLayoutManager) {
          layoutLinearItem(outRect, view, parent, (LinearLayoutManager) layoutManager);
      } else if (layoutManager instanceof StaggeredGridLayoutManager) {
          layoutStaggeredItem(outRect, view, parent, (StaggeredGridLayoutManager) layoutManager);
      }
  }
  • LinearLayoutManager 的布局偏移封装
private void layoutLinearItemSpace(Rect outRect, View view, RecyclerView parent, LinearLayoutManager linearManager) {
    int viewPosition = parent.getChildAdapterPosition(view);

    if (linearManager.getOrientation() == LinearLayoutManager.VERTICAL) {
        outRect.left = mSpaceHorizontal;
        outRect.right = mSpaceHorizontal;
        if (viewPosition == 0) {
            outRect.top = mSpaceVertical;
            outRect.bottom = mSpaceVertical / 2;
        } else if (viewPosition == linearManager.getItemCount() - 1) {
            outRect.top = mSpaceVertical / 2;
            outRect.bottom = mSpaceVertical;
        } else {
            outRect.top = mSpaceVertical / 2;
            outRect.bottom = mSpaceVertical / 2;
        }
    } else {
        outRect.top = mSpaceVertical;
        outRect.bottom = mSpaceVertical;

        if (viewPosition == 0) {
            outRect.left = mSpaceHorizontal;
            outRect.right = mSpaceHorizontal / 2;
        } else if (viewPosition == linearManager.getItemCount() - 1) {
            outRect.left = mSpaceHorizontal / 2;
            outRect.right = mSpaceHorizontal;
        } else {
            outRect.left = mSpaceHorizontal / 2;
            outRect.right = mSpaceHorizontal / 2;
        }
    }
}
  • GridLayoutManager 的布局偏移封装
private void layoutGridItemSpace(Rect outRect, View view, RecyclerView parent, GridLayoutManager gridManager) {
    int viewPosition = parent.getChildAdapterPosition(view);
    int spanCount = gridManager.getSpanCount();
    int index = viewPosition % spanCount;


    if (viewPosition < spanCount) {//第一行
        outRect.top = mSpaceVertical;
        outRect.bottom = mSpaceVertical / 2;
    } else if (viewPosition / spanCount == (gridManager.getItemCount() - 1) / spanCount) {//多宫格如何判断最后一行?
        outRect.top = mSpaceVertical / 2;
        outRect.bottom = mSpaceVertical;
    } else {
        outRect.top = mSpaceVertical / 2;
        outRect.bottom = mSpaceVertical / 2;
    }


    if (index == 0) {//第一列
        outRect.left = mSpaceHorizontal;
        outRect.right = mSpaceHorizontal / 2;

    } else if (index == spanCount - 1) {//最后一列
        outRect.left = mSpaceHorizontal / 2;
        outRect.right = mSpaceHorizontal;

    } else {
        outRect.left = mSpaceHorizontal / 2;
        outRect.right = mSpaceHorizontal / 2;
    }

}
  • StaggeredGridLayoutManager的布局偏移封装
private void layoutStaggeredItemSpace(Rect outRect, View view, RecyclerView parent, StaggeredGridLayoutManager staggeredManager) {

}
  • LinearLayoutManager 的布局偏移颜色封装
private void drawLinearItemSpace(Canvas c, RecyclerView parent, LinearLayoutManager linearManager) {

    if (linearManager.getOrientation() == LinearLayoutManager.VERTICAL) {
        for (int viewPosition = 0; viewPosition < linearManager.getItemCount(); viewPosition++) {
            View childView = parent.getChildAt(viewPosition);

            if (childView == null) {
                continue;
            }

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();

            int left;
            int top;
            int right;
            int bottom;
            left = childView.getLeft() - params.leftMargin;
            right = childView.getRight() + params.rightMargin;
            top = childView.getBottom() + params.bottomMargin;
            bottom = top + mSpaceVertical;
            c.drawRect(left, top, right, bottom, mPaint);
            if (viewPosition == 0) {
                top = childView.getTop() - params.topMargin - mSpaceVertical;
                bottom = top + mSpaceVertical;
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    } else {


        for (int viewPosition = 0; viewPosition < linearManager.getItemCount(); viewPosition++) {
            View childView = parent.getChildAt(viewPosition);

            if (childView == null) {
                continue;
            }

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();

            int left;
            int top;
            int right;
            int bottom;


            left = childView.getRight() + params.rightMargin;
            top = childView.getTop() - params.topMargin;
            right = left + mSpaceHorizontal;
            bottom = childView.getBottom() + params.bottomMargin;
            c.drawRect(left, top, right, bottom, mPaint);

            if(viewPosition == 0) {
                left = childView.getLeft() - params.leftMargin - mSpaceHorizontal;
                top = childView.getTop() - params.topMargin;
                right = left + mSpaceHorizontal;
                bottom = childView.getBottom() + params.bottomMargin;
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }
}
  • GridLayoutManager 的布局颜色填充封装
private void drawGridItemSpace(Canvas c, RecyclerView parent, GridLayoutManager gridManager) {
    for (int viewPosition = 0; viewPosition < gridManager.getItemCount(); viewPosition++) {
        View childView = parent.getChildAt(viewPosition);

        if (childView == null) {
            continue;
        }

        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();

        int spanCount = gridManager.getSpanCount();
        int index = viewPosition % spanCount;

        int left;
        int top;
        int right;
        int bottom;

        left = childView.getLeft() - params.leftMargin;
        right = childView.getRight() + params.rightMargin + mSpaceHorizontal;
        if (viewPosition < spanCount) {//第一行
            top = childView.getTop() - params.topMargin - mSpaceVertical;
            bottom = top + mSpaceVertical;
            c.drawRect(left, top, right, bottom, mPaint);

            top = childView.getBottom() + params.bottomMargin;
            bottom = top + mSpaceVertical;
            c.drawRect(left, top, right, bottom, mPaint);
        } else {
            top = childView.getBottom() + params.bottomMargin;
            bottom = top + mSpaceVertical;
            c.drawRect(left, top, right, bottom, mPaint);
        }


        if (index == 0) {//第一列
            top = childView.getTop() - params.topMargin;
            bottom = childView.getBottom() + params.bottomMargin + mSpaceVertical;
            left = childView.getLeft() - params.leftMargin - mSpaceHorizontal;
            right = left + mSpaceHorizontal;
            c.drawRect(left, top, right, bottom, mPaint);

            top = childView.getTop() - params.topMargin;
            bottom = childView.getBottom() + params.bottomMargin;
            left = childView.getRight() + params.rightMargin;
            right = left + mSpaceHorizontal;
            c.drawRect(left, top, right, bottom, mPaint);

            if (viewPosition == 0) {
                top = childView.getTop() - params.topMargin - mSpaceVertical;
                bottom = top + mSpaceVertical;
                left = childView.getLeft() - params.leftMargin - mSpaceHorizontal;
                right = left + mSpaceHorizontal;
                c.drawRect(left, top, right, bottom, mPaint);
            }

        } else {
            top = childView.getTop() - params.topMargin;
            bottom = childView.getBottom() + params.bottomMargin;
            left = childView.getRight() + params.rightMargin;
            right = left + mSpaceHorizontal;
            c.drawRect(left, top, right, bottom, mPaint);
        }
    }
}
  • StaggeredGridLayoutManager的布局偏移颜色填充封装

StaggeredGridLayoutManager 相关的目前正在研究,后面会补充完善

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容