RecyclerView 获取 item 的位置
来源:互联网 发布:淘宝怎么买食品 编辑:程序博客网 时间:2024/05/22 03:13
/** * Created by JokAr on 2016/2/17. */public class RecyclerViewPositionHelper { final RecyclerView recyclerView; final RecyclerView.LayoutManager layoutManager; public RecyclerViewPositionHelper(RecyclerView recyclerView) { this.recyclerView = recyclerView; this.layoutManager = recyclerView.getLayoutManager(); } public static RecyclerViewPositionHelper createHelper(RecyclerView recyclerView) { if (recyclerView == null) { throw new NullPointerException("Recycler View is null"); } return new RecyclerViewPositionHelper(recyclerView); } /** * Returns the adapter item count. * * @return The total number on items in a layout manager */ public int getItemCount() { return layoutManager == null ? 0 : layoutManager.getItemCount(); } /** * Returns the adapter position of the first visible view. This position does not include * adapter changes that were dispatched after the last layout pass. * * @return The adapter position of the first visible item or {@link RecyclerView#NO_POSITION} if * there aren't any visible items. */ public int findFirstVisibleItemPosition() { final View child = findOneVisibleChild(0, layoutManager.getChildCount(), false, true); return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child); } /** * Returns the adapter position of the first fully visible view. This position does not include * adapter changes that were dispatched after the last layout pass. * * @return The adapter position of the first fully visible item or * {@link RecyclerView#NO_POSITION} if there aren't any visible items. */ public int findFirstCompletelyVisibleItemPosition() { final View child = findOneVisibleChild(0, layoutManager.getChildCount(), true, false); return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child); } /** * Returns the adapter position of the last visible view. This position does not include * adapter changes that were dispatched after the last layout pass. * * @return The adapter position of the last visible view or {@link RecyclerView#NO_POSITION} if * there aren't any visible items */ public int findLastVisibleItemPosition() { final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true); return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child); } /** * Returns the adapter position of the last fully visible view. This position does not include * adapter changes that were dispatched after the last layout pass. * * @return The adapter position of the last fully visible view or * {@link RecyclerView#NO_POSITION} if there aren't any visible items. */ public int findLastCompletelyVisibleItemPosition() { final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, true, false); return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child); } View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible, boolean acceptPartiallyVisible) { OrientationHelper helper; if (layoutManager.canScrollVertically()) { helper = OrientationHelper.createVerticalHelper(layoutManager); } else { helper = OrientationHelper.createHorizontalHelper(layoutManager); } final int start = helper.getStartAfterPadding(); final int end = helper.getEndAfterPadding(); final int next = toIndex > fromIndex ? 1 : -1; View partiallyVisible = null; for (int i = fromIndex; i != toIndex; i += next) { final View child = layoutManager.getChildAt(i); final int childStart = helper.getDecoratedStart(child); final int childEnd = helper.getDecoratedEnd(child); if (childStart < end && childEnd > start) { if (completelyVisible) { if (childStart >= start && childEnd <= end) { return child; } else if (acceptPartiallyVisible && partiallyVisible == null) { partiallyVisible = child; } } else { return child; } } } return partiallyVisible; }}
阅读全文
0 0
- RecyclerView 获取 item 的位置
- RecyclerView 获取显示item的位置
- RecyclerView获取item的count
- RecyclerView 获取item,删除item的坑
- 关于recyclerview item的乱换位置的解决方案
- Android使用ItemTouchHelper实现RecyclerView的item拖动位置交换
- 解决RecyclerView删除Item导致位置错乱的问题
- 【Android】- RecyclerView删除时,解决Item位置错乱的问题
- 如何获取recyclerView的第一个Item的视图
- RecyclerView 获取滑动停止时展示出的item范围
- RecyclerView删除Item导致位置错乱问题
- recyclerview中item可见位置研究
- RecyclerView的item监听
- RecyclerView的Item监听
- TV中RecyclerView添加item的点击事件和删除item之后获取焦点解决
- ListView 中Item 位置获取
- 利用RecyclerView实现的一个动画给变item的位置和左右滑动删除该RcyclervView的Item条目
- 解决RecyclerView删除Item导致位置错乱的问题 以及删除最后一个出错的问题
- java只使用try和finally不使用catch的原因和场景
- Android log.e(),log.d(),log.i()等的区别
- Oracle之数据库恢复到某个时间点-yellowcong
- spring技术内幕读书笔记--IOC
- WebView java.lang.NoSuchMethodException:callDrawGLFunction
- RecyclerView 获取 item 的位置
- 数据库查询优化之用exists替换in,not exists替换not in
- BugKu MISC 部分题目Write_up(一)
- 1121: 电梯
- BitmapUtil 工具类
- spring autowired
- servlet与cgi的区别与关系(1)--两者区别
- Maven学习(五)-- 聚合与继承
- Android四大组件之Activity<中>