当前位置:网站首页>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
边栏推荐
猜你喜欢
Excel快速对齐表格的中姓名(两个字姓名和三个字姓名对齐)
Small test knife: Go reflection helped me convert Excel to Struct
Nuget package and upload tutorial
2021 OWASP TOP 10 漏洞指南
OAuth2:搭建授权服务器
Combination series - there are combinations when there are arrangements
Sentinel限流和异常处理
UnityShader入门学习(二)——渲染流水线
MySQL 23 classic interviews hang the interviewer
Analysis of the startup source code of hyperf (2) - how the request reaches the controller
随机推荐
Sentinel服务熔断和降级
LeetCode二叉树系列——110.平衡二叉树
ASP.NET Core 产生连续 Guid
三角恒等变换公式
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Small test knife: Go reflection helped me convert Excel to Struct
Nuget打包并上传教程
使用 Chainlink Keepers 实现智能合约函数的自动化执行
NPM淘宝镜像(最新版本)于2021-11-21 16:53:52发布新版本npm镜像[通俗易懂]
Advanced Mathematics - Commonly Used Indefinite Integral Formulas
Nuget打包并上传教程
Groupid(artifact id)
UnityShader入门学习(三)——Unity的Shader
Unity Shader入门精要学习——透明效果
分成两栏后文字顺序混乱的问题解决【写期刊论文时】
Redis与分布式:主从复制
Detailed guide to compare two tables using natural full join in SQL
c语言hello world代码(代码编程入门)
梅克尔工作室-第一次
基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)