RecyclerView 获取 item 的位置
2017-11-06 16:58
501 查看
/** * 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; } }
相关文章推荐
- RecyclerView中获取Item位置的两种方法
- RecyclerView 获取指定位置item的view并获取view的子控件
- RecyclerView 获取显示item的位置
- TV中RecyclerView添加item的点击事件和删除item之后获取焦点解决
- 关于RecyclerView实现瀑布流,上下滑动时item之间互换位置的问题
- Android 解决RecyclerView瀑布流刷新之后Item位置改变
- Android 解决RecyclerView删除Item导致位置错乱的问题
- 基于RecyclerView实现的仿微信聊天界面,item长按根据触摸位置弹出对话框
- 利用RecyclerView实现的一个动画给变item的位置和左右滑动删除该RcyclervView的Item条目
- adapter的 getView 获取ListView的Position位置的ItemView
- RecyclerView获取item的count
- 使用 RecyclerView 实现 Gallery 画廊效果,并控制 Item 停留位置
- Android RecyclerView ListView 获取不到Item点击事件
- 通过实现TextWatcher接口来获取RecyclerView item的EditText的值
- RecyclerView删除Item导致位置错乱问题
- 关于RecyclerView实现瀑布流,上下滑动时item之间互换位置的问题
- 解决RecyclerView删除Item导致位置错乱的问题 以及删除最后一个出错的问题
- androidTV中使用recyclerview并使其item在获取焦点后获取边框,并伴随放大,凸显效果
- recyclerview中item可见位置研究
- Recyclerview中最后一个可见Item的位置以及是否滑动到底部