decoration 英文意思:

英[ˌdekəˈreɪʃn] 美[ˌdɛkəˈreʃən] n. 装饰品; 装饰,装潢; 装饰图案,装饰风格; 奖章;

[例句]The decoration and furnishings had to be practical enough for a family home 房子的装潢和家具都必须很实用,适合家居生活。 [

其他] 复数:decorations

RecyclerView.ItemDecoration 装饰类,都装饰啥?

RecyclerView.ItemDecoration装饰的目标当然是RecyclerView里面每个item.

之前ListView有divier属性可以修改分隔线的样式

RecyclerView则是提供了

recyclerView.addItemDecoration()

看一下源码:

   public static abstract class ItemDecoration {
/**
* Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
* Any content drawn by this method will be drawn before the item views are drawn,
* and will thus appear underneath the views.
*
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView
*/
public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, parent);
} /**
* @deprecated
* Override {@link #onDraw(Canvas, RecyclerView, RecyclerView.State)}
*/
@Deprecated
public void onDraw(Canvas c, RecyclerView parent) {
} /**
* Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
* Any content drawn by this method will be drawn after the item views are drawn
* and will thus appear over the views.
*
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView.
*/
public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
} /**
* @deprecated
* Override {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)}
*/
@Deprecated
public void onDrawOver(Canvas c, RecyclerView parent) {
} /**
* @deprecated
* Use {@link #getItemOffsets(Rect, View, RecyclerView, State)}
*/
@Deprecated
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
outRect.set(0, 0, 0, 0);
} /**
* Retrieve any offsets for the given item. Each field of <code>outRect</code> specifies
* the number of pixels that the item view should be inset by, similar to padding or margin.
* The default implementation sets the bounds of outRect to 0 and returns.
*
* <p>
* If this ItemDecoration does not affect the positioning of item views, it should set
* all four fields of <code>outRect</code> (left, top, right, bottom) to zero
* before returning.
*
* <p>
* If you need to access Adapter for additional data, you can call
* {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the
* View.
*
* @param outRect Rect to receive the output.
* @param view The child view to decorate
* @param parent RecyclerView this ItemDecoration is decorating
* @param state The current state of RecyclerView.
*/
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}
}

会发现主要有下面三个方法:

public void onDraw(Canvas c, RecyclerView parent, State state)
public void onDrawOver(Canvas c, RecyclerView parent, State state)
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state)
 
那来看看都什么意思干么用:
直接看注释吧
onDraw():
/**
* Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
* Any content drawn by this method will be drawn before the item views are drawn,
* and will thus appear underneath the views.
*
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView
*/  

大概是说会在item画之前画些东西,会现在Item下面。那相当于是一个背景。

onDrawOver()

  /**
* Draw any appropriate decorations into the Canvas supplied to the RecyclerView.
* Any content drawn by this method will be drawn after the item views are drawn
* and will thus appear over the views.
*
* @param c Canvas to draw into
* @param parent RecyclerView this ItemDecoration is drawing into
* @param state The current state of RecyclerView.
*/
public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
}
will be drawn after the item views are drawn and will thus appear over the views.
很明白与上面的相对,会有Item画完之后去画,在浮在item内容上面。
getItemOffsets()
 /**
* Retrieve any offsets for the given item. Each field of <code>outRect</code> specifies
* the number of pixels that the item view should be inset by, similar to padding or margin.
* The default implementation sets the bounds of outRect to 0 and returns.
*
* <p>
* If this ItemDecoration does not affect the positioning of item views, it should set
* all four fields of <code>outRect</code> (left, top, right, bottom) to zero
* before returning.
*
* <p>
* If you need to access Adapter for additional data, you can call
* {@link RecyclerView#getChildAdapterPosition(View)} to get the adapter position of the
* View.
*
* @param outRect Rect to receive the output.
* @param view The child view to decorate
* @param parent RecyclerView this ItemDecoration is decorating
* @param state The current state of RecyclerView.
*/
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}

也说得很明白:similar to padding or margin.跟padding margin相似。给设置边距的。

从getItemOffsets()来开始实践

我们给recyclerview 加上背景以便观察。

<android.support.v7.widget.RecyclerView
android:id="@+id/rcv_qulitry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
</android.support.v7.widget.RecyclerView>

实现在自义定的decoration类

package com.lechang.fragment.decoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View; /**
* Created by hongtao on 2017/11/17.
*/ public class MyDecoration extends RecyclerView.ItemDecoration { @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.left = 5;
outRect.right = 5;
outRect.bottom = 10;
super.getItemOffsets(outRect, view, parent, state);
}
}

马上来用

recyclerView.addItemDecoration(new MyDecoration());

看看效果,嘿嘿,尼玛不是那么回事,怎么不起作用呢?

蒙逼了?

仔细看一下代码,方法调用了super.getItemOffsets(outRect, view, parent, state);再点进去看,

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
parent);
}
然后再调用了
@Deprecated
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
outRect.set(0, 0, 0, 0);
}

看到没

outRect.set(0, 0, 0, 0);

给设置成0了。

那我们

       outRect.left = 5;
outRect.right = 5;
outRect.bottom = 10;

设置的值就成白做了。

所以得把这个赋值放到super之后。

package com.lechang.fragment.decoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View; /**
* Created by hongtao on 2017/11/17.
*/ public class MyDecoration extends RecyclerView.ItemDecoration { @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//切记要先调super.
outRect.left = 5;
outRect.right = 5;
outRect.bottom = 10;
}
}
这回可以了。
看一下效果:

红色底出来了。相当于padding .

只是底红露出,不能当分隔线,你非要用也行。

主要的还是要用ondraw把分隔线画出来。

下面来试ondraw().

原型:如下

public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)

这是一个回调的方法,给了一个画布,RecyclerView ,和RecyclerView.State

RecyclerView.State:给了下面三个状态
static final int STEP_START = 1;
static final int STEP_LAYOUT = 1 << 1;
static final int STEP_ANIMATIONS = 1 << 2;

意思是我们可以在这三个不同时期做不同的事,画不同的东西。有需求可以安排。

RecyclerView parent

既然是parent 那他的child都是个个item.

那整个方法的意思就是用Canvas c给每个item都画上装饰(分隔线)。

来一段代码:

package com.lechang.fragment.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View; /**
* Created by hongtao on 2017/11/17.
*/ public class MyDecoration extends RecyclerView.ItemDecoration { Paint dividerPaint;
int dividerHeight = 3; public MyDecoration(Context context, int dividerHeight) {
dividerPaint = new Paint();
dividerPaint.setColor(Color.BLACK);
this.dividerHeight = dividerHeight;
} @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//切记要先调super.
// outRect.left = 5;
// outRect.right = 5;
outRect.bottom = 20;
} @Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight(); for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + dividerHeight;
//画了一个矩型
c.drawRect(left, top, right, bottom, dividerPaint);
}
}
}
outRect.bottom 给了 20的值,要是不给这个值,那将什么也看不到。因为是在item下层的;
最终效果:黑色线是onDraw结果,红是底

接下来看:
onDrawOver()方法效果
package com.lechang.fragment.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View; /**
* Created by hongtao on 2017/11/17.
*/ public class MyDecoration extends RecyclerView.ItemDecoration { Paint dividerPaint;
int dividerHeight = 3; public MyDecoration(Context context, int dividerHeight) {
dividerPaint = new Paint();
dividerPaint.setColor(Color.BLACK);
this.dividerHeight = dividerHeight;
} @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//切记要先调super.
// outRect.left = 5;
// outRect.right = 5;
// outRect.bottom = 20;
} @Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight(); for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + dividerHeight;
//画了一个矩型
c.drawRect(left, top, right, bottom, dividerPaint);
}
} @Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int childCount = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight(); for (int i = 0; i < childCount - 1; i++) {
View view = parent.getChildAt(i);
float top = view.getBottom();
float bottom = view.getBottom() + dividerHeight;
//画了一个矩型
c.drawRect(left, top, right, bottom, dividerPaint);
}
}
}

drawOver在上层画,所以不需要getItemOffsets配合。

至此,ImtemDecoration这个类基本我们就会用了。

如果对Item位置做一些逻辑处理,还能做出一些有意思的东西。接下来再写。告一段落。

												

RecyclerView.ItemDecoration的更多相关文章

  1. 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果

    [转] 原文 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果 字数1598 阅读302 评论2 喜欢23 1.背景   RecyclerView ...

  2. RecyclerView.ItemDecoration 间隔线

    内容已更新到:https://www.cnblogs.com/baiqiantao/p/19762fb101659e8f4c1cea53e7acb446.html 目录一个通用分割线ItemDecor ...

  3. 小甜点,RecyclerView 之 ItemDecoration 讲解及高级特性实践

    本篇文章摘自微信公众号 guolin_blog (郭霖)独家发布 毫无疑问,RecyclerView 是现在 Android 世界中最重要的系统组件之一,它的出现就是为了高效代替 ListView 和 ...

  4. android RecyclerView (二) ItemDecoration 详解

    RecyclerView 已经推出了一年多了,日常开发中也已经彻底从 ListView 迁移到了 RecyclerView,但前两天有人在一个安卓群里面问了个关于最顶上的 item view 加蒙层的 ...

  5. (转载)RecyclerView之ItemDecoration由浅入深

    RecyclerView之ItemDecoration由浅入深 作者 小武站台 关注 2016.09.19 18:20 字数 1155 阅读 10480评论 15喜欢 91赞赏 3 译文的GitHub ...

  6. RecyclerView使用大全

    RecylerView介绍 RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recyler ...

  7. Android RecyclerView 实现支付宝首页效果

    Android RecyclerView 实现支付宝首页效果 [TOC] 虽然我本人不喜欢支付宝的,但是这个网格本身其实还是不错的,项目更新中更改了一个布局为网格模式,类似支付宝.(估计是产品抄袭的= ...

  8. 安卓v7支持包下的ListView替代品————RecyclerView

    RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 项目已经同步至github:https://github.com/ ...

  9. Android 5.X新特性之RecyclerView基本解析及无限复用

    说到RecyclerView,相信大家都不陌生,它是我们经典级ListView的升级版,升级后的RecyclerView展现了极大的灵活性.同时内部直接封装了ViewHolder,不用我们自己定义Vi ...

随机推荐

  1. mysql的读写分离

    1.laravel实现数据库读写分离配置或者多读写分离配置 config\database.php里配置 'connections' => array(      //默认mysql配置,访问t ...

  2. canvas画布标签

    最近良师益友整理一些canvas的资料,加强学习了解! 当你创建一个<canvas>元素后,就拥有了它的绘图上下文. 一.简单图形 1.getContext()方法 为了在canvas上绘 ...

  3. hibernate和mybatis区别

    看图    Hibernate mybatis 难易度 难 简单,容易上手 编码 良好的映射机制,不需要关心 需要手动编写sql,resultMap 调优 制定合理的缓存策略: 尽量使用延迟加载特性: ...

  4. macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏问题

    macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏会消失,估计不止出现在PhpStorm,一系列jetbrains的产品可能都会有这个问题,包括eclipis ...

  5. coursera无法观看视频解决方法

    coursera无法观看视频解决方法 Coursera是国外的一款非常有名的公开课网站,值得大家一起学习,奈何"长城"太厚,经常被和谐,一些视频打不开,最近找到不用FQ的方法,共享 ...

  6. CodeForces - 556B Case of Fake Numbers

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  7. 补写:Best Coder #85 1001 Sum(前缀和)

    sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...

  8. java设计师初入职场,如何站稳脚跟

    本文内容一共由3部分展开 a:新人如何快速融入团队 b:如何在职场中提升自己影响力 c:如何规进行职业规划 a:如何快速融入团队   能在层层选拔下进入公司,说明你工作的能力还是得到公司的认可,不过这 ...

  9. Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库

    前两天有人私信我,让我爬这个网站,http://bbs.baobeihuijia.com/forum-191-1.html上的失踪儿童信息,准备根据失踪儿童的失踪时的地理位置来更好的寻找失踪儿童,这种 ...

  10. Hive 学习笔记(启动方式,内置服务)

    一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令 ...