LayoutManager自定义
来源:互联网 发布:php msgpack pack 编辑:程序博客网 时间:2024/05/17 22:37
参考资料
参考资料1;
参考资料2;
参考资料3;
参考资料4;
背景介绍
RecyclerView由于其强大的扩展性,现在已经逐步的取代了ListView和GridView了。为了实现不同的布局效果,我们会用到官方提供的LinearLayoutManager、GridLayoutManager和StaggeredGridLayoutManager。但这些布局只能满足日常需求,在一些比较复杂的布局中,它们就力不从心了,强行拼凑实现,带来的后果就是较差的体验和性能。所以能够自定义LayoutManager还是十分必要的,它能够解放创造力,构造复杂的、流畅的滑动列表。上面几篇参考资料中就实现了一些不寻常的效果,我们可以看到,这些效果如果用常规的方案去实现将会十分蹩脚。
揭开LayoutManager中不为人知的秘密
自定义LayoutManager
主要要求我们完成三件事情:
- 计算每个ItemView的位置;
- 处理滑动事件;
- 缓存并重用ItemView;
而我们比较重要的工作是在onLayoutChildern()
这个回调方法中完成的。
下面我们就来一一解析。
预先准备
当我们extends RecyclerView.LayoutManager是,我们会被强制要求重写generateDefaultLayoutParams()
方法,如方法名字一样,我们需要提供一个默认的LayoutParams,这里为我们的每个ItemView
提供默认的LayoutParams
,所以它能够直接影响到我们的布局效果,这里我们设置成WRAP_CONTENT
,让ItemView
获得决定权。
@Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT); }
计算ItemView的位置
1.实现简单的LayoutManager
先看效果图:
再看代码:
@Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { super.onLayoutChildren(recycler, state); // 先把所有的View先从RecyclerView中detach掉,然后标记为"Scrap"状态,表示这些View处于可被重用状态(非显示中)。 // 实际就是把View放到了Recycler中的一个集合中。 detachAndScrapAttachedViews(recycler); calculateChildrenSite(recycler); } private void calculateChildrenSite(RecyclerView.Recycler recycler) { totalHeight = 0; for (int i = 0; i < getItemCount(); i++) { // 遍历Recycler中保存的View取出来 View view = recycler.getViewForPosition(i); addView(view); // 因为刚刚进行了detach操作,所以现在可以重新添加 measureChildWithMargins(view, 0, 0); // 通知测量view的margin值 int width = getDecoratedMeasuredWidth(view); // 计算view实际大小,包括了ItemDecorator中设置的偏移量。 int height = getDecoratedMeasuredHeight(view); Rect mTmpRect = new Rect(); //调用这个方法能够调整ItemView的大小,以除去ItemDecorator。 calculateItemDecorationsForChild(view, mTmpRect); // 调用这句我们指定了该View的显示区域,并将View显示上去,此时所有区域都用于显示View, //包括ItemDecorator设置的距离。 layoutDecorated(view, 0, totalHeight, width, totalHeight + height); totalHeight += height; } }
这段代码逻辑简单,它实现的其实就是一个简单的垂直线性布局,当然现在还不能滑动,也没有缓存机制。在这段代码中,我们先调用detachAndScrapAttachedViews(recycler);
将所有的ItemView标记为Scrap状态,然后在挨个取出来,计算他们应该布局到什么位置,并用成员变量totalHeight记录总高度,最后依次调用layoutDecorated()
将ItemView布局上去。
2.两列式的LayoutManager
先看效果图:
有了上例的基础,我们只需要稍作调整,直接看下面代码,注意注释部分。
private void calculateChildrenSite(RecyclerView.Recycler recycler) { totalHeight = 0; for (int i = 0; i < getItemCount(); i++) { View view = recycler.getViewForPosition(i); addView(view); //我们自己指定ItemView的尺寸。 measureChildWithMargins(view, DisplayUtils.getScreenWidth() / 2, 0); int width = getDecoratedMeasuredWidth(view); int height = getDecoratedMeasuredHeight(view); Rect mTmpRect = new Rect(); calculateItemDecorationsForChild(view, mTmpRect); if (i % 2 == 0) { //当i能被2整除时,是左,否则是右。 //左 layoutDecoratedWithMargins(view, 0, totalHeight, DisplayUtils.getScreenWidth() / 2, totalHeight + height); } else { //右,需要换行 layoutDecoratedWithMargins(view, DisplayUtils.getScreenWidth() / 2, totalHeight, DisplayUtils.getScreenWidth(), totalHeight + height); totalHeight = totalHeight + height; LogUtils.e(i + "->" + totalHeight); } } }
处理滑动
先来看一下效果:
滑动事件主要涉及到4个方法需要重写,我们直接来看代码:
@Override public boolean canScrollVertically() { //返回true表示可以纵向滑动 return true; } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { //列表向下滚动dy为正,列表向上滚动dy为负,这点与Android坐标系保持一致。 //实际要滑动的距离 int travel = dy; LogUtils.e("dy = " + dy); //如果滑动到最顶部 if (verticalScrollOffset + dy < 0) { travel = -verticalScrollOffset; } else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {//如果滑动到最底部 travel = totalHeight - getVerticalSpace() - verticalScrollOffset; } //将竖直方向的偏移量+travel verticalScrollOffset += travel; // 调用该方法通知view在y方向上移动指定距离 offsetChildrenVertical(-travel); return travel; } private int getVerticalSpace() { //计算RecyclerView的可用高度,除去上下Padding值 return getHeight() - getPaddingBottom() - getPaddingTop(); } @Override public boolean canScrollHorizontally() { //返回true表示可以横向滑动 return super.canScrollHorizontally(); } @Override public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) { //在这个方法中处理水平滑动 return super.scrollHorizontallyBy(dx, recycler, state); }
缓存并重用ItemView
在上面代码的基础上我们稍作改动,加入缓存,先看下面的log信息,它显示虽然有100个Item,但childCount稳定在26:
下面来看看代码的变化,我展示了完整的代码,留心注释。
public class CustomLayoutManager extends RecyclerView.LayoutManager { /** 用于保存item的位置信息 */ private SparseArray<Rect> allItemRects = new SparseArray<>(); /** 用于保存item是否处于可见状态的信息 */ private SparseBooleanArray itemStates = new SparseBooleanArray(); public int totalHeight = 0; private int verticalScrollOffset; @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { if (getItemCount() <= 0 || state.isPreLayout()) { return; } super.onLayoutChildren(recycler, state); detachAndScrapAttachedViews(recycler); /* 这个方法主要用于计算并保存每个ItemView的位置 */ calculateChildrenSite(recycler); recycleAndFillView(recycler, state); } private void calculateChildrenSite(RecyclerView.Recycler recycler) { totalHeight = 0; for (int i = 0; i < getItemCount(); i++) { View view = recycler.getViewForPosition(i); addView(view); // 我们自己指定ItemView的尺寸。 measureChildWithMargins(view, DisplayUtils.getScreenWidth() / 2, 0); calculateItemDecorationsForChild(view, new Rect()); int width = getDecoratedMeasuredWidth(view); int height = getDecoratedMeasuredHeight(view); Rect mTmpRect = allItemRects.get(i); if (mTmpRect == null) { mTmpRect = new Rect(); } if (i % 2 == 0) { // 当i能被2整除时,是左,否则是右。 // 左 mTmpRect.set(0, totalHeight, DisplayUtils.getScreenWidth() / 2, totalHeight + height); } else { // 右,需要换行 mTmpRect.set(DisplayUtils.getScreenWidth() / 2, totalHeight, DisplayUtils.getScreenWidth(), totalHeight + height); totalHeight = totalHeight + height; } // 保存ItemView的位置信息 allItemRects.put(i, mTmpRect); // 由于之前调用过detachAndScrapAttachedViews(recycler),所以此时item都是不可见的 itemStates.put(i, false); } } private void recycleAndFillView(RecyclerView.Recycler recycler, RecyclerView.State state) { if (getItemCount() <= 0 || state.isPreLayout()) { return; } // 当前scroll offset状态下的显示区域 Rect displayRect= new Rect(0, verticalScrollOffset, getHorizontalSpace(), verticalScrollOffset + getVerticalSpace()); /** * 将滑出屏幕的Items回收到Recycle缓存中 */ Rect childRect = new Rect(); for (int i = 0; i < getChildCount(); i++) { //这个方法获取的是RecyclerView中的View,注意区别Recycler中的View //这获取的是实际的View View child = getChildAt(i); //下面几个方法能够获取每个View占用的空间的位置信息,包括ItemDecorator childRect.left = getDecoratedLeft(child); childRect.top = getDecoratedTop(child); childRect.right = getDecoratedRight(child); childRect.bottom = getDecoratedBottom(child); //如果Item没有在显示区域,就说明需要回收 if (!Rect.intersects(displayRect, childRect)) { //移除并回收掉滑出屏幕的View removeAndRecycleView(child, recycler); itemStates.put(i, false); //更新该View的状态为未依附 } } //重新显示需要出现在屏幕的子View for (int i = 0; i < getItemCount(); i++) { //判断ItemView的位置和当前显示区域是否重合 if (Rect.intersects(displayRect, allItemRects.get(i))) { //获得Recycler中缓存的View View itemView = recycler.getViewForPosition(i); measureChildWithMargins(itemView, DisplayUtils.getScreenWidth() / 2, 0); //添加View到RecyclerView上 addView(itemView); //取出先前存好的ItemView的位置矩形 Rect rect = allItemRects.get(i); //将这个item布局出来 layoutDecoratedWithMargins(itemView, rect.left, rect.top - verticalScrollOffset, //因为现在是复用View,所以想要显示在 rect.right, rect.bottom - verticalScrollOffset); itemStates.put(i, true); //更新该View的状态为依附 } } LogUtils.e("itemCount = " + getChildCount()); } @Override public boolean canScrollVertically() { // 返回true表示可以纵向滑动 return true; } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { //每次滑动时先释放掉所有的View,因为后面调用recycleAndFillView()时会重新addView()。 detachAndScrapAttachedViews(recycler); // 列表向下滚动dy为正,列表向上滚动dy为负,这点与Android坐标系保持一致。 // 实际要滑动的距离 int travel = dy; LogUtils.e("dy = " + dy); // 如果滑动到最顶部 if (verticalScrollOffset + dy < 0) { travel = -verticalScrollOffset; } else if (verticalScrollOffset + dy > totalHeight - getVerticalSpace()) {// 如果滑动到最底部 travel = totalHeight - getVerticalSpace() - verticalScrollOffset; } // 调用该方法通知view在y方向上移动指定距离 offsetChildrenVertical(-travel); recycleAndFillView(recycler, state); //回收并显示View // 将竖直方向的偏移量+travel verticalScrollOffset += travel; return travel; } private int getVerticalSpace() { // 计算RecyclerView的可用高度,除去上下Padding值 return getHeight() - getPaddingBottom() - getPaddingTop(); } @Override public boolean canScrollHorizontally() { // 返回true表示可以横向滑动 return super.canScrollHorizontally(); } @Override public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) { // 在这个方法中处理水平滑动 return super.scrollHorizontallyBy(dx, recycler, state); } public int getHorizontalSpace() { return getWidth() - getPaddingLeft() - getPaddingRight(); }}
实现缓存最主要的就是先把每个ItemView的位置信息保存起来,然后在滑动过程中通过判断每个ItemView的位置是否和当前RecyclerView应该显示的区域有重合,若有就显示它,若没有就移除并回收。
总结
实现自己的自定义LayoutManager主要的三个步骤:
- 计算每个ItemView的位置;
- 添加滑动事件;
- 实现缓存。
我们需根据代码多理解,多思考,然后动手写属于自己的LayoutManager。
作者:CoorChice
链接:http://www.jianshu.com/p/715b59c46b74
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 自定义LayoutManager
- LayoutManager自定义
- android RecyclerView自定义 LayoutManager
- Recyclerview-自定义LayoutManager
- “傻瓜”式填充,自定义LayoutManager
- RecylerView 自定义 LayoutManager 基础一
- RecylerView 自定义 LayoutManager 基础二
- LayoutManager
- RecyclerView自定义LayoutManager,打造不规则布局
- 自定义LayoutManager的详解及其使用
- RecyclerView——实现自定义LayoutManager
- RecyclerView自定义LayoutManager实现横向瀑布流
- 自定义LayoutManager带你撸个LinearLayoutManager
- RecyclerView自定义LayoutManager,打造不规则布局
- RecyclerView系列(7)—自定义LayoutManager
- 自定义LayoutManager带你撸个LinearLayoutManager
- 利用自定义xml属性指定来RecyclerView的LayoutManager
- 【Android】掌握自定义LayoutManager(二) 实现流式布局
- 手机web端唤醒qq客户端以及电脑web端唤醒qq客户端
- SDK将彻底颠覆开发者的价值观?
- C++指针与引用
- Mysql的各种时间类型的区别以及使用场景
- Pixhawk室内自动控制:参数设置(SITL)
- LayoutManager自定义
- Android 系统属性SystemProperty分析
- Python 使用 pip 升级所有包
- 机器学习环境搭建
- 遗传算法例子(转)
- Tensorflow
- Cimage
- Thread的中断机制(interrupt)
- matlab 读写text文档