当前位置:网站首页>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
边栏推荐
- Why do we need to sub-library and sub-table?
- [Pytorch] F.softmax() method description
- 谷歌CTS测试(cta测试)
- Redis与分布式:主从复制
- Uniapp WeChat small application reference standard components
- Motion capture system for end-positioning control of flexible manipulators
- Sentinel服务熔断和降级
- [QNX Hypervisor 2.2 User Manual]9.14 safety
- OpenShift 4 - Deploy Redis Cluster with Operator
- Detailed guide to compare two tables using natural full join in SQL
猜你喜欢

华医网冲刺港股:5个月亏2976万 红杉与姚文彬是股东

Redis 】 【 publish and subscribe message

Sentinel服务熔断和降级

2021 OWASP TOP 10 漏洞指南

Asynchronous processing business using CompletableFuture

The paper manual becomes 3D animation in seconds, the latest research of Wu Jiajun of Stanford University, selected for ECCV 2022

MySQL 23 classic interviews hang the interviewer

LeetCode二叉树系列——226.翻转二叉树

格林美瑞交所IPO:募资3.8亿美元 更多中国企业将赴欧洲上市

模板与泛型编程值typelist实现
随机推荐
Advanced Mathematics - Commonly Used Indefinite Integral Formulas
[QNX Hypervisor 2.2用户手册]9.14 safety
The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
自适应控制——仿真实验二 用Narendra方案设计模型参考自适应系统
Getting started with UnityShader (3) - Unity's Shader
UnityShader入门学习(三)——Unity的Shader
redhat/openssl生成自签ca证书并使用
MySql总结
三角恒等变换公式
The recently popular domestic interface artifact Apipost experience
R语言检验样本是否符合正态性(检验样本是否来自一个正态分布总体):shapiro.test函数检验样本是否符合正态分布(normality test)
梅克尔工作室-第一次
消息队列消息数据存储MySQL表设计
C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
How to grab configuration information for DELL SC compellent storage system
redhat/openssl generates a self-signed ca certificate and uses it
分成两栏后文字顺序混乱的问题解决【写期刊论文时】
LeetCode二叉树系列——222.完全二叉树的节点个数
R语言向前或者向后移动时间序列数据(自定义滞后或者超前的期数):使用dplyr包中的lag函数将时间序列数据向前移动一天(设置参数n为正值)
[QNX Hypervisor 2.2 User Manual]9.14 safety