当前位置:网站首页>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
边栏推荐
- MySQL 23道经典面试吊打面试官
- Node实现数据加密
- 架构实战营模块8消息队列表结构设计
- [QNX Hypervisor 2.2用户手册]9.13 rom
- Trigonometric identity transformation formula
- Redis 】 【 publish and subscribe message
- R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗
- OAuth2:四种授权方式
- sentinel与nacos持久化
- R语言检验样本是否符合正态性(检验样本是否来自一个正态分布总体):shapiro.test函数检验样本是否符合正态分布(normality test)
猜你喜欢
随机推荐
NPM淘宝镜像(最新版本)于2021-11-21 16:53:52发布新版本npm镜像[通俗易懂]
R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
Detailed guide to compare two tables using natural full join in SQL
Numbers that appear only once in LeetCode
Description of Hikvision camera streaming RTSP address rules
QGIS 加载WMS数据,重新投影
深入浅出边缘云 | 4. 生命周期管理
最小费用最大流问题详解
学习笔记12--路径-速度分解法之局部路径搜索
看交互设计如何集成到Scrum敏捷流程中
[QNX Hypervisor 2.2 User Manual]9.14 safety
OAuth2:微服务权限校验Session共享
Redis与分布式:集群搭建
我把问烂了的MySQL面试题总结了一下
2021 OWASP TOP 10 漏洞指南
Small test knife: Go reflection helped me convert Excel to Struct
Use of el-tooltip
The meaning of node_exporter performance monitoring information collection in Prometheus
架构实战营模块8消息队列表结构设计
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!