RecyclerView滑动距离计算
来源:互联网 发布:ipad 看图软件 编辑:程序博客网 时间:2024/06/03 16:01
最近在写关于RecyclerView滑动到最底部时,给出一个判断,是否到达最底部。但不知道为什么,总是得不到ScrollY,一直为0。没办法,只能去网上找一些相关资料。但因为是实习生,自定义RecycleView这些很多都看不懂,只能去找最简单的办法。下面就是我找到的办法,在这里记录一下。非常感谢知乎用户-张宇 给的解法。
废话不多说,代码贴上:
/** * 还能向下滑动多少 */ private int getDistance(){ LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); View firstVisibItem = mRecyclerView.getChildAt(0); int firstItemPosition = layoutManager.findFirstVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); int recycleViewHeight = mRecyclerView.getHeight(); int itemHeight = firstVisibItem.getHeight(); int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibItem); return (itemCount - firstItemPosition - 1)* itemHeight - recycleViewHeight+firstItemBottom; }
当前的getDistance()就是剩余的距离。
/** * 已滑动的距离 */ private int getDistance(){ LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); View firstVisibItem = mRecyclerView.getChildAt(0); int firstItemPosition = layoutManager.findFirstVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); int recycleViewHeight = mRecyclerView.getHeight(); int itemHeight = firstVisibItem.getHeight(); int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibItem); return (firstItemPosition + 1)*itemHeight - firstItemBottom; }
当前的getDistance()就是已经滑动的距离了。然后我根据这个人的算法,判断自己有没有滑倒最底部了。
/** * 获取RecyclerView滚动位置 */ private int getDistance(){ LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); View firstVisibItem = mRecyclerView.getChildAt(0); int firstItemPosition = layoutManager.findFirstVisibleItemPosition(); int itemCount = layoutManager.getItemCount(); int recycleViewHeight = mRecyclerView.getHeight(); int itemHeight = firstVisibItem.getHeight(); int firstItemBottom = layoutManager.getDecoratedBottom(firstVisibItem); return (itemCount - firstItemPosition - 1)* itemHeight - recycleViewHeight; }这样当getDistance()等于0时,我就知道已经滑动到最底部了。如果你们RecyclerView上面有间距,自己看一下getDistance()等于多少。
0 0
- RecyclerView滑动距离计算
- 获取RecyclerView滑动的距离
- RecyclerView滑动距离计算 包含多个Header的万能开挂情况
- 如何获取Android RecyclerView滑动的距离
- 如何获取Android RecyclerView滑动的距离
- 如何获取Android RecyclerView滑动的距离
- Android 开发笔记: 获取RecyclerView 滑动距离
- android listview 计算滑动距离
- touch事件计算滑动距离
- 计算ScrollView控件的滑动距离
- Android RecyclerView滑动固定距离 滚动指定长度
- 关于RecyclerView与ListView控件滑动距离的计算方法
- android 标题动态变换(listview scrollview 滑动距离计算)
- 安卓开发监听recyclerview的滑动时某一控件距离顶部的距离来做显示隐藏某些布局
- RecyclerView嵌套RecyclerView滑动冲突
- 计算距离
- 距离计算
- 计算距离
- Educational Codeforces Round 5 D. Longest k-Good Segment
- GATTServApp_WriteCharCfg和GATTServApp_ProcesscccWriteReq 和GATTServApp_ProcessCharCfg的区别
- 小波分析个例应用
- Android:单元测试Junit的配置
- java 连接mysql数据库
- RecyclerView滑动距离计算
- 在Android应用中自动跳转到开发市场
- 真正的两张图片循环滚动
- 小波函数总结
- js--属性和方法(私有/公有)
- egret自定义事件
- javascript编译环境搭建
- 希尔排序
- Bundle Adjustment 原理推导