RecyclerView自定义StaggeredGridLayoutManager实现EPG布局

本文主要是介绍RecyclerView自定义StaggeredGridLayoutManager实现EPG布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现布局:

最近在做EPG功能,要求能够展示每个节目对应时间轴的预览节目信息,这种布局可以采用RecyclerView的瀑布流布局实现,但是测试的时候发现Item的position是没有规律的,这样就不能够很好的根据positoin去区分是哪个节目的EPG信息从而显示到不同行数上,所以需要从StaggeredGridLayoutManager源码改起

修改getNextSpan函数

/*** Finds the span for the next view.*/
private StaggeredGridLayoutManager.Span getNextSpan(LayoutState layoutState) {final boolean preferLastSpan = preferLastSpan(layoutState.mLayoutDirection);final int startIndex, endIndex, diff;if (preferLastSpan) {startIndex = mSpanCount - 1;endIndex = -1;diff = -1;} else {startIndex = 0;endIndex = mSpanCount;diff = 1;}//布局排列方向,表示从起始排到末尾if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {Span min = null;int minLine = Integer.MAX_VALUE;final int defaultLine = mPrimaryOrientation.getStartAfterPadding();//for循环是找到布局中哪个列的最后一个Item的末尾处坐标最小for (int i = startIndex; i != endIndex; i += diff) {final Span other = mSpans[i];int otherLine = other.getEndLine(defaultLine);if (otherLine < minLine) {min = other;minLine = otherLine;}}return min;} else { //布局排列方向,表示从末尾排到起始Span max = null;int maxLine = Integer.MIN_VALUE;final int defaultLine = mPrimaryOrientation.getEndAfterPadding();//for循环是找到布局中哪个列的第一个Item的起始处坐标值最大for (int i = startIndex; i != endIndex; i += diff) {final Span other = mSpans[i];int otherLine = other.getStartLine(defaultLine);if (otherLine > maxLine) {max = other;maxLine = otherLine;}}return max;}
}

该函数是获取下一个Span,每次获取都会先计算当前哪个Span的末尾处最小,最小则返回该Span,以便下一个View添加到这个Span上,我们可以增加一个int变量,每次调用+1,循环去Span数组获取,修改layoutState.mLayoutDirection == LayoutState.LAYOUT_END条件代码为

if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) {return mSpans[(index++) % mSpanCount];
}

这样我们就可以在adapter里面根据position去区分是哪一行了

虽然上面代码能够有规律的显示数据了,但是当我们回滚时,会发现行数据会概率发现交换,通过打印代码发现,行数据发生交换的时候会调用getNextSpan函数我们修改的代码块,index++了,所以行数据会发生交换,既然知道了原因,那么我们来看看为什么会调用到这个函数吧

在fill(EPGRecyclerView.Recycler recycler, LayoutState layoutState,EPGRecyclerView.State state)这个函数里,有这样一段代码

final int spanIndex = mLazySpanLookup.getSpan(position);
StaggeredGridLayoutManager.Span currentSpan;
final boolean assignSpan = spanIndex == StaggeredGridLayoutManager.LayoutParams.INVALID_SPAN_ID;
if (assignSpan) {currentSpan = lp.mFullSpan ? mSpans[0] : getNextSpan(layoutState);mLazySpanLookup.setSpan(position, currentSpan);if (DEBUG) {Log.d(TAG, "assigned " + currentSpan.mIndex + " for " + position);}
}

当我们回滚时,按道理assignSpan应该是false的,spanIndex应该是有确切值的,而且spanIndex设置只有一处,这说明滚动时候在某个地方spanIndex被重置了

通过对代码的debug调试,原来是onLayoutChildren函数中有一个判断2个相邻的子View位置是否合理的逻辑,如果不合理的话会进行变量的重置,也就是上面的spanIndex

onLayoutChildren函数中有这样一段代码

boolean hasGaps = false;
if (shouldCheckForGaps && !state.isPreLayout() ) {//LogUtil.e("StaggeredGridLayoutManager", "onLayoutChildren", "state.isPreLayout() = false");final boolean needToCheckForGaps = mGapStrategy != GAP_HANDLING_NONE&& getChildCount() > 0&& (mLaidOutInvalidFullSpan || hasGapsToFix() != null);if (needToCheckForGaps) {removeCallbacks(mCheckForGapsRunnable);//LogUtil.e("StaggeredGridLayoutManager", "onLayoutChildren", "checkForGaps()3");if (checkForGaps()) {hasGaps = true;}}
}
if (state.isPreLayout()) {mAnchorInfo.reset();
}
mLastLayoutFromEnd = anchorInfo.mLayoutFromEnd;
mLastLayoutRTL = isLayoutRTL();
if (hasGaps) {//进行重置mAnchorInfo.reset();onLayoutChildren(recycler, state, false);
}

接下来我们来看看hasGapsToFix里面的一段代码

if (mShouldReverseLayout) {// ensure child's end is below nextChild's endint myEnd = mPrimaryOrientation.getDecoratedEnd(child);int nextEnd = mPrimaryOrientation.getDecoratedEnd(nextChild);if (myEnd < nextEnd) {return child; //i should have a better position} else if (myEnd == nextEnd) {compareSpans = true;}
} else {int myStart = mPrimaryOrientation.getDecoratedStart(child);int nextStart = mPrimaryOrientation.getDecoratedStart(nextChild);//如果当前View的起始处大于下一个View的起始处,会返回当前Viewif (myStart > nextStart) {return child; //i should have a better position} else if (myStart == nextStart) {compareSpans = true;}
}
if (compareSpans) {// equal, check span indices.StaggeredGridLayoutManager.LayoutParams nextLp = (StaggeredGridLayoutManager.LayoutParams) nextChild.getLayoutParams();if (lp.mSpan.mIndex - nextLp.mSpan.mIndex < 0 != preferredSpanDir < 0) {//preferredSpanDir < 0 为truereturn child;}
}

既然知道了原因,那么我们可以修改为不检测间隙,这样就避免了行数据交换的问题

①增加一个needCheckGap的标志位,置为false

//增加一个是否需要检测间隙的标志
private boolean needCheckGap = false;

②在hasGapsToFix函数起始处直接返回

View hasGapsToFix() {//不需要检测间隙 -zbjif (!needCheckGap) {return null;}...
}

③onLayoutChild函数去除检测间隙

boolean hasGaps = false;
//不检测间隙
if (shouldCheckForGaps && !state.isPreLayout() && hasGaps) {//LogUtil.e("StaggeredGridLayoutManager", "onLayoutChildren", "state.isPreLayout() = false");final boolean needToCheckForGaps = mGapStrategy != GAP_HANDLING_NONE&& getChildCount() > 0&& (mLaidOutInvalidFullSpan || hasGapsToFix() != null);if (needToCheckForGaps) {removeCallbacks(mCheckForGapsRunnable);//LogUtil.e("StaggeredGridLayoutManager", "onLayoutChildren", "checkForGaps()3");if (checkForGaps()) {hasGaps = true;}}
}

到这里,EPG功能布局已经差不多了,可以正常在手机上显示了,但是在安卓TV上,用遥控器左右键来进行选择时,会出现焦点不在同一行的问题,经过对安卓焦点规律的研究,发现是当滚动到屏幕后面的时候,后面已经没有实际的View了,这时候安卓系统会就近选择View,也就会出现左右切换焦点不在同一行的问题,解决这个问题也挺简单的,就是让RecyclerView额外多加载一层View,onLayoutChildren函数主要是进行子View的添加的,所以我们要从这里开始研究,该函数中有这样的代码块

// Layout end.
setLayoutStateDirection(LayoutState.LAYOUT_END);
testNum = 3;
fill(recycler, mLayoutState, state);
// Layout start.
setLayoutStateDirection(LayoutState.LAYOUT_START);
mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection;
fill(recycler, mLayoutState, state);

这里有2个fill函数,其实都是填充布局的,只不过第1个是为了做一些边缘检测处理,第二个才是真正的进行填充布局,接下来看看fill函数,StaggeredGridLayoutManager有一个判断某列或行是否还需要进行填充布局的标志mRemainingSpans,fiil函数填充布局的时候就会去对这个标志位做判断和赋值,以便用来加载子View

fill函数起始处会对mRemainingSpans做一个复位,也就是所有的列或行都需要进行添加子View

mRemainingSpans.set(0, mSpanCount, true);

接着在while循环中有这样一段代码,主要是用于检测该列或行末尾是否已经达到需要展示的位置,如果达到的话就将该列的标志位置为false,也就不再需要进行添加子View了

if (lp.mFullSpan) {updateAllRemainingSpans(mLayoutState.mLayoutDirection, targetLine);
} else {updateSpanNum = 1;updateRemainingSpans(currentSpan, mLayoutState.mLayoutDirection, targetLine);
}
private void updateRemainingSpans(StaggeredGridLayoutManager.Span span, int layoutDir, int targetLine) {final int deletedSize = span.getDeletedSize();final int line;if (layoutDir == LayoutState.LAYOUT_START) {line = span.getStartLine();if (line + deletedSize <= targetLine) {mRemainingSpans.set(span.mIndex, false);}} else {line = span.getEndLine();if (line - deletedSize >= targetLine) {mRemainingSpans.set(span.mIndex, false);}}
}

知道了填充布局的原理,那么我们就添加多一个BitSet标志位来使mRemainingSpans延迟置位,以达到多填充一层子View的目的

定义一个全局BitSet

/*** 增加一个延迟置位的标志,使其每列/行能添加多一个View */
private BitSet mDelayFlagSpans;

在setSpanCount(int spanCount)函数进行初始化

public void setSpanCount(int spanCount) {...mRemainingSpans = new BitSet(mSpanCount);mDelayFlagSpans = new BitSet(mSpanCount);... 
}

接着在fill函数起始处添加置位标志

mRemainingSpans.set(0, mSpanCount, true);
mDelayFlagSpans.set(0, mSpanCount, true);

在updateRemainingSpans函数中对mRemainingSpans做延迟操作

private void updateRemainingSpans(StaggeredGridLayoutManager.Span span, int layoutDir, int targetLine) {final int deletedSize = span.getDeletedSize();final int line;if (layoutDir == LayoutState.LAYOUT_START) {line = span.getStartLine();if (line + deletedSize <= targetLine) {if (mDelayFlagSpans.get(span.mIndex)) {mDelayFlagSpans.set(span.mIndex, false);} else {mRemainingSpans.set(span.mIndex, false);}}} else {line = span.getEndLine();if (line - deletedSize >= targetLine) {if (mDelayFlagSpans.get(span.mIndex)) {mDelayFlagSpans.set(span.mIndex, false);} else {mRemainingSpans.set(span.mIndex, false);}}}
}

到这里差不多了,我们可以运行了,但是通过打印发现当一滚动的时候position会一下子多加载了几十个,这样的结果我们是不能接受的,既然一滚动就会多加载,那么肯定是在滚动的时候会调用到fill函数,果不其然,在scrollBy函数中调用到了fill函数

int scrollBy(int dt, EPGRecyclerView.Recycler recycler, EPGRecyclerView.State state) {...int consumed = fill(recycler, mLayoutState, state);...
}

由于scrollBy函数会调用很多次,所以会额外加载了更多的子View,所以我们不能在fill函数里面对mDelayFlagSpans做复位操作,通过对代码的研究,我发现在3处地方加入mDelayFlagSpans的复位操作比较好,第1,2处是在onLayoutChild函数

// Layout end.
setLayoutStateDirection(LayoutState.LAYOUT_END);
mDelayFlagSpans.set(0, mSpanCount, true);
fill(recycler, mLayoutState, state);
// Layout start.
setLayoutStateDirection(LayoutState.LAYOUT_START);
mLayoutState.mCurrentPosition = anchorInfo.mPosition + mLayoutState.mItemDirection;
testNum = 4;
mDelayFlagSpans.set(0, mSpanCount, true);
fill(recycler, mLayoutState, state);

一开始布局的时候我们可以对mDelayFlagSpans进行复位,后面滚动的时候是不会在调用到此段代码,第3处是在RecyclerView的dispatchKeyEvent函数对按键做一下判断,并在StaggeredGridLayoutManager中加个对mDelayFlagSpans的复位函数以便在RecyclerView中调用

@Override
public boolean dispatchKeyEvent(KeyEvent event) {int keyCode = event.getKeyCode();// 这里只考虑水平移动的情况(垂直移动相同的解决方案)if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {if (event.getAction() == KeyEvent.ACTION_DOWN) {if(getLayoutManager() instanceof StaggeredGridLayoutManager){StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager)getLayoutManager();staggeredGridLayoutManager.resetDelayFlagSpans();}}}return super.dispatchKeyEvent(event);
}

StaggeredGridLayoutManager增加函数

public void resetDelayFlagSpans(){mDelayFlagSpans.set(0, mSpanCount, true);
}

到这里,我们已经基本完成了对StaggeredGridLayoutManager的修改,这里的修改只适合屏幕不做横竖屏切换的情况,因为去除了判断间隙,所以横竖屏切换会出现间隙

这篇关于RecyclerView自定义StaggeredGridLayoutManager实现EPG布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/213978

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja