当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型
[Pytorch] F.softmax() method description
Redis与分布式:哨兵模式
组合系列--有排列就有组合
Advanced Mathematics - Commonly Used Indefinite Integral Formulas
《微信小程序-进阶篇》Lin-ui组件库源码分析-Icon组件
Jmeter常用的十大组件
安装Xshell并使用其进行Ymodem协议的串口传输
MySQL [subquery]
thread_local 变量的析构顺序
Sentinel流量控制
OAuth2:单点登陆客户端
R语言检验样本是否符合正态性(检验样本是否来自一个正态分布总体):shapiro.test函数检验样本是否符合正态分布(normality test)
Sentinel热点参数限流
学习笔记12--路径-速度分解法之局部路径搜索
Node实现数据加密
DeepLab Series Learning
charles进行弱网测试(app弱网测试怎么做)
sentinel与nacos持久化
[QNX Hypervisor 2.2 User Manual] 9.13 rom









