当前位置:网站首页>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
边栏推荐
- 49. The copy constructor and overloaded 】
- 使用 PyTorch 检测眼部疾病
- In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
- 架构实战营模块8消息队列表结构设计
- 最小费用最大流问题详解
- 深入浅出边缘云 | 4. 生命周期管理
- MySQL 23 classic interviews hang the interviewer
- Recommendation System - Recall Phase - 2013: DSSM (Twin Towers Model) [Embedding (Semantic Vector) Recall] [Microsoft]
- AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型
- 名创优品斥资6.95亿购买创始人叶国富所持办公楼股权
猜你喜欢
随机推荐
OAuth2:资源服务器
名创优品斥资6.95亿购买创始人叶国富所持办公楼股权
The recently popular domestic interface artifact Apipost experience
谷歌CTS测试(cta测试)
最小费用最大流问题详解
element-plus虚拟表格virtual-list组件中是怎么实现清理lodash.memoize缓存的?
STM32(十)------- SPI通信
R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
R语言的画图代码及差异性分析[通俗易懂]
BigDecimal 简介,常用方法
Selenium自动化中无头浏览器的应用
OAuth2:四种授权方式
我把问烂了的MySQL面试题总结了一下
[Pytorch] torch.argmax() usage
学习笔记12--路径-速度分解法之局部路径搜索
公告
为什么要分库分表?
"Listen to me, thank you" can be said in ancient poetry?Tsinghua University has developed an artifact of "Searching Sentences According to Meaning", which can search for the famous sayings you want wi
TRACE32——基于SNOOPer的变量记录
redhat/openssl generates a self-signed ca certificate and uses it









