RecyclerView瀑布流优化方案探讨,Android屏幕适配很难嘛其实也就那么回事

本文主要是介绍RecyclerView瀑布流优化方案探讨,Android屏幕适配很难嘛其实也就那么回事,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

int itemCount = adapter.getItemCount();
int childCount = layoutManager.getChildCount();
RefreshLogUtils.d(“SpaceViewItemLine–count–”+itemCount + “-----”+childCount+"—索引–"+position+"—"+spanIndex);
if (position<itemCount && spanCount2) {
if (childCount % 2 == 0){
//这个是右边item
outRect.left = 5;
outRect.right = 20;
} else {
//这个是左边item
outRect.left = 20;
outRect.right = 5;
}
if (childCount
1 || childCount==2){
outRect.top = 0;
} else {
outRect.top = 20;
}
RefreshLogUtils.d(“SpaceViewItemLine–间距–”+childCount+"----"+outRect.left+"-----"+outRect.right);
}
}
});

recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int position = parent.getChildAdapterPosition(view);
int spanCount = 0;
int spanIndex = 0;
RecyclerView.Adapter adapter = parent.getAdapter();
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (adapternull || layoutManagernull){
return;
}
if (layoutManager instanceof StaggeredGridLayoutManager){
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
}
//普通Item的尺寸
int itemCount = adapter.getItemCount();
int childCount = layoutManager.getChildCount();
RefreshLogUtils.d(“SpaceViewItemLine–count–”+itemCount + “-----”+childCount+"—索引–"+position+"—"+spanIndex);
if (position<itemCount && spanCount2) {
if (spanIndex != GridLayoutManager.LayoutParams.INVALID_SPAN_ID) {
//getSpanIndex方法不管控件高度如何,始终都是左右左右返回index
if (spanIndex % 2 == 0) {
//这个是左边item
outRect.left = 20;
outRect.right = 5;
} else {
//这个是右边item
outRect.left = 5;
outRect.right = 20;
}
if (childCount
1 || childCount==2){
outRect.top = 0;
} else {
outRect.top = 20;
}
}
//outRect.top = space;
RefreshLogUtils.d(“SpaceViewItemLine–间距–”+spanIndex+"----"+outRect.left+"-----"+outRect.right);
}
}
});

05.自定义Manager崩溃

  • RecyclerView莫名的Inconsistency detected崩溃;

  • 出现这个异常原因:

  • 使用RecyclerView加官方下拉刷新的时候,如果绑定的List对象在更新数据之前进行了clear,而这时用户紧接着迅速上滑RV,就会造成崩溃,而且异常不会报到你的代码上,属于RV内部错误。

  • 自定义一个CustomStaggeredGridLayoutManager 在onLayoutChildren对异常进行捕获:

public class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
private static final String TAG = “LOG_CustomStaggered”;
public CustomStaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,

这篇关于RecyclerView瀑布流优化方案探讨,Android屏幕适配很难嘛其实也就那么回事的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N