当前位置:网站首页>RecyclerView高效使用第三节
RecyclerView高效使用第三节
2022-07-31 14:51:00 【[email protected]】
在需求开发迭代时,总是会遇到一些特定的交互功能的实现。
一,获取可见的Item下标
功能开发时,有时需要滑动到指定的下标或滑动到屏幕底部或头部时,会想到获取当前可见的item下标进行进行处理。
//获取第一个可见的Item下标
layoutManager.findFirstCompletelyVisibleItemPosition()
//获取最后一个可见的Item下标
layoutManager.findLastCompletelyVisibleItemPosition()
主要使用了RecyclerView的LayoutManager获取第一个和最后一个可见的Item下标
二,ItemDecorationCount
使用RecyclerView嵌套RecyclerView时,如果给子RecyclerView添加了ItemDecoration后,页面下拉刷新等一些操作时,会发现列表的间距不断的变宽,这是因为RecyclerView对Item的复用导致的,这时就需要使用ItemDecorationCount进行判断,防止重复的添加ItemDecoration。
//判断RecyclerView是否添加分界线,不加此判断,下拉刷新时,间距会不停的重复添加导致间距变大
if (recyclerView.itemDecorationCount==0){
//添加自定义分割线或间距
}
三,Item滑动居中
实现选项列表时,有些场景下会要求选中的Item居中,就需要自己自定义LayoutManager进行实现,如下:
import android.content.Context;
import android.util.DisplayMetrics;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
public class CenterLinearLayoutManager extends LinearLayoutManager {
static int lastPosition = 0;
static int targetPosition = 0;
public CenterLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastPosition, int position) {
lastPosition = lastPosition;
targetPosition = position;
smoothScrollToPosition(recyclerView, state, position);
}
public static class CenterSmoothScroller extends LinearSmoothScroller {
private static float duration = 400f;
public CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
float newDuration = duration / (Math.abs(targetPosition - lastPosition));
return newDuration / displayMetrics.densityDpi;
}
@Override
protected int calculateTimeForScrolling(int dx) {
return super.calculateTimeForScrolling(dx);
}
}
}
使用时
1)创建自定义的LayoutManager
private val centerLayoutManager by lazy {
CenterLinearLayoutManager(
activity,
LinearLayoutManager.HORIZONTAL,
false
)
}
2)滑动到指定下标
centerLayoutManager.smoothScrollToPosition(
recyclerView,
RecyclerView.State(),
position //指定的下标
)
在有些场景下需要结合一中的获取第一个和最后一个可见的下标,计算出屏幕中间的item下标做一些交互处理。
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zdc9023/article/details/126058520
边栏推荐
- Introduction to BigDecimal, common methods
- R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
- Excel快速对齐表格的中姓名(两个字姓名和三个字姓名对齐)
- 2021 OWASP TOP 10 Vulnerability Guide
- 我把问烂了的MySQL面试题总结了一下
- 为什么毕业季不要表白?
- sentinel与nacos持久化
- Uniapp WeChat small application reference standard components
- R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(legend、修改可视化图像的图例在整图中的位置)
- 梅克尔工作室-第一次
猜你喜欢
随机推荐
微信聊天记录中搜索红包
MANIFEST.MF文件(PDB文件)
Sentinel安装与部署
[QNX Hypervisor 2.2 User Manual]9.14 safety
R语言ggstatsplot包ggbarstats函数可视化条形图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)、检验结果报告符合APA标准
Introductory UnityShader learning (2) - the rendering pipeline
Web自动化实战——Selenium4(自动化测试环境的搭建)
高等数学——常用不定积分公式
Why do we need to sub-library and sub-table?
OpenCV测量物体的尺寸技能 get~
[QNX Hypervisor 2.2用户手册]9.13 rom
Redis与分布式:主从复制
OAuth2:使用JWT令牌
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Trigonometric identity transformation formula
2021 OWASP TOP 10 Vulnerability Guide
LeetCode二叉树系列——110.平衡二叉树
Sentinel热点参数限流
深入浅出边缘云 | 4. 生命周期管理









