当前位置:网站首页>Efficient use of RecyclerView Section 3
Efficient use of RecyclerView Section 3
2022-07-31 14:56:00 【[email protected]】
In the iteration of requirements development, the implementation of some specific interactive functions will always be encountered.
One, get the visible Item subscript
When developing functions, sometimes when you need to slide to the specified subscript or slide to the bottom or head of the screen, you will think of getting the currently visible item subscript for processing.
//Get the first visible Item subscriptlayoutManager.findFirstCompletelyVisibleItemPosition()//Get the last visible Item subscriptlayoutManager.findLastCompletelyVisibleItemPosition()Mainly use RecyclerView's LayoutManager to get the first and last visible Item subscripts
Second, ItemDecorationCount
When using RecyclerView to nest RecyclerView, if ItemDecoration is added to the child RecyclerView, and some operations such as page pull-down refresh, you will find that the spacing of the list keeps getting wider. This is caused by the reuse of Item by RecyclerView.When you need to use ItemDecorationCount to judge, to prevent repeated addition of ItemDecoration.
//Determine whether the RecyclerView adds a dividing line, without this judgment, when the pull-down refresh is performed, the spacing will be added repeatedly, causing the spacing to become largerif (recyclerView.itemDecorationCount==0){//Add custom divider or spacing}Third, Item slides to the center
When implementing the option list, in some scenarios, the selected item will be required to be centered, so you need to customize the LayoutManager to implement it, as follows:
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);}@Overridepublic 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);}@Overridepublic int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);}@Overrideprotected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {float newDuration = duration / (Math.abs(targetPosition - lastPosition));return newDuration / displayMetrics.densityDpi;}@Overrideprotected int calculateTimeForScrolling(int dx) {return super.calculateTimeForScrolling(dx);}}}When using
1) Create a custom LayoutManager
private val centerLayoutManager by lazy {CenterLinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)}2) Slide to the specified subscript
centerLayoutManager.smoothScrollToPosition(recyclerView,RecyclerView.State(),position //specified subscript)In some scenarios, it is necessary to obtain the first and last visible subscripts in one, and calculate the item subscripts in the middle of the screen for some interactive processing.
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207311451011501.html
边栏推荐
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 文件的作用
- OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry
- The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
- Selenium自动化中无头浏览器的应用
- 网线RJ45接口针脚[通俗易懂]
- Web自动化实战——Selenium4(自动化测试环境的搭建)
- 三角恒等变换公式
- 力扣:738.单调递增的数字
- Sentinel安装与部署
- redhat/openssl generates a self-signed ca certificate and uses it
猜你喜欢

The JVM a class loader

乡村基冲刺港交所:5个月期内亏2224万 SIG与红杉中国是股东

Sentinel安装与部署

I summed up the bad MySQL interview questions

Small test knife: Go reflection helped me convert Excel to Struct

Motion capture system for end-positioning control of flexible manipulators

UnityShader入门学习(二)——渲染流水线

Jmeter常用的十大组件

Getting started with UnityShader (3) - Unity's Shader

微信聊天记录中搜索红包
随机推荐
格林美瑞交所IPO:募资3.8亿美元 更多中国企业将赴欧洲上市
R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
网线RJ45接口针脚[通俗易懂]
The role of /etc/profile, /etc/bashrc, ~/.bash_profile, ~/.bashrc files
什么是消息队列呢?
[Pytorch] torch.argmax() usage
Introductory UnityShader learning (2) - the rendering pipeline
最近很火的国产接口神器Apipost体验
英文语法-时与态
常用工具命令速查表
三角恒等变换公式
Detailed guide to compare two tables using natural full join in SQL
大健云仓冲刺美股:增营收反减利润 京东与DCM是股东
消息队列消息数据存储MySQL表设计
Nuget打包并上传教程
谷歌CTS测试(cta测试)
《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件
TRACE32——基于SNOOPer的变量记录
组合系列--有排列就有组合
Spark学习(2)-Spark环境搭建-Local