Java Code Examples for android.support.v7.widget.StaggeredGridLayoutManager#findFirstVisibleItemPositions()
The following examples show how to use
android.support.v7.widget.StaggeredGridLayoutManager#findFirstVisibleItemPositions() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScrollableViewCompat.java From AndroidDemo with MIT License | 6 votes |
@Override public boolean viewCanScrollUp() { if (view == null) return false; RecyclerView.LayoutManager manager = view.getLayoutManager(); if (manager == null) return false; if (manager instanceof LinearLayoutManager) { LinearLayoutManager lm = (LinearLayoutManager) manager; if (lm.getOrientation() == LinearLayoutManager.HORIZONTAL) { return false; } return lm.findFirstVisibleItemPosition() > 0 || lm.findViewByPosition(0).getTop() < view.getPaddingTop(); } else if (manager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager g = (StaggeredGridLayoutManager) manager; int[] info = g.findFirstVisibleItemPositions(null); return info[0] > 0; } return false; }
Example 2
Source File: CommonRecyclerView.java From TigerVideo with Apache License 2.0 | 6 votes |
/** * 获取第一个显示的位置 * * @return */ public int getFirstVisiblePosition() { int position; if (getLayoutManager() instanceof LinearLayoutManager) { position = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); } else if (getLayoutManager() instanceof GridLayoutManager) { position = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager(); int[] lastPositions = layoutManager.findFirstVisibleItemPositions(new int[layoutManager.getSpanCount()]); position = getMinPosition(lastPositions); } else { position = 0; } return position; }
Example 3
Source File: RecycleViewHelper.java From nono-android with GNU General Public License v3.0 | 6 votes |
public static int getRecycleViewFirstViewPosition(RecyclerView recyclerView){ int position; if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { position = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); } else if (recyclerView.getLayoutManager() instanceof GridLayoutManager) { position = ((GridLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager(); int[] lastPositions = layoutManager.findFirstVisibleItemPositions(new int[layoutManager.getSpanCount()]); int size = lastPositions.length; int minPosition = Integer.MAX_VALUE; for (int i = 0; i < size; i++) { minPosition = Math.min(minPosition, lastPositions[i]); } position = minPosition; } else { //position =recyclerView. getLayoutManager().getItemCount() - 1; position = 0; } return position; }
Example 4
Source File: LoadMoreRecyclerView.java From LoadMoreRecyclerView with Apache License 2.0 | 6 votes |
/** * 获取第一条展示的位置 * * @return */ private int getFirstVisiblePosition() { int position; if (getLayoutManager() instanceof LinearLayoutManager) { position = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); } else if (getLayoutManager() instanceof GridLayoutManager) { position = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager(); int[] lastPositions = layoutManager.findFirstVisibleItemPositions(new int[layoutManager.getSpanCount()]); position = getMinPositions(lastPositions); } else { position = 0; } return position; }
Example 5
Source File: MonthWeekMaterialCalendarView.java From monthweekmaterialcalendarview with Apache License 2.0 | 5 votes |
/** * 判断线性布局的recyclerView是否到达顶部 * * @param view */ private boolean isAtTop(View view) { boolean isTop = false; if (view instanceof RecyclerView) { if (((RecyclerView) view).getLayoutManager() instanceof ILayoutManager) { if (((RecyclerView) view).getLayoutManager() instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) ((RecyclerView) view).getLayoutManager(); if (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) { isTop = true; } } if (((RecyclerView) view).getLayoutManager() instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = (GridLayoutManager) ((RecyclerView) view).getLayoutManager(); if (gridLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) { isTop = true; } } if (((RecyclerView) view).getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) ((RecyclerView) view).getLayoutManager(); int[] positions = new int[staggeredGridLayoutManager.getSpanCount()]; staggeredGridLayoutManager.findFirstVisibleItemPositions(positions); if (findMin(positions) == 0) { isTop = true; } } } else { throw new IllegalArgumentException("RecyclerView layoutManager must implement ILayoutManager"); } } return isTop; }
Example 6
Source File: OnReachBottomListener.java From Melophile with Apache License 2.0 | 5 votes |
private int fetchFirstVisibleItemPosition() { if (layoutManager instanceof LinearLayoutManager) { return LinearLayoutManager.class.cast(layoutManager).findFirstVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager manager = StaggeredGridLayoutManager.class.cast(layoutManager); int[] result = manager.findFirstVisibleItemPositions(null); if (result != null && result.length > 0) { return result[0]; } } return -1; }
Example 7
Source File: Utils.java From ht-refreshrecyclerview with MIT License | 5 votes |
/** * 获取第一个可见/完全可见item的位置索引 * @param layoutManager RecyclerView.LayoutManager对象 * @param isCompletelyVisible 是否完全可见 * @return item的索引 */ public static int getFirstItemPosition(@NonNull RecyclerView.LayoutManager layoutManager, boolean isCompletelyVisible) { int firstVisibleItemPosition = -1; if (layoutManager instanceof LinearLayoutManager) { //GridLayoutManager继承LinearLayoutManager LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; firstVisibleItemPosition = isCompletelyVisible ? linearLayoutManager.findFirstCompletelyVisibleItemPosition() : linearLayoutManager.findFirstVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] firstVisibleItemPositions = isCompletelyVisible ? staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null) : staggeredGridLayoutManager.findFirstVisibleItemPositions(null); if (firstVisibleItemPositions != null && firstVisibleItemPositions.length > 0) firstVisibleItemPosition = firstVisibleItemPositions[0]; } return firstVisibleItemPosition; }
Example 8
Source File: LoadMoreRecyclerListener.java From RecyclerViewManager with MIT License | 5 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); hasCompleted = false; isLoadingMoreEnabled = dy > 0; RecyclerView.LayoutManager mLayoutManager = recyclerView.getLayoutManager(); //初始化firstVisibleItemPosition和lastVisibleItemPosition if (null != mLayoutManager) { if (mLayoutManager instanceof LinearLayoutManager) { firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager) .findFirstVisibleItemPosition(); lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager) .findLastVisibleItemPosition(); } else if (mLayoutManager instanceof GridLayoutManager) { firstVisibleItemPosition = ((GridLayoutManager) mLayoutManager) .findFirstVisibleItemPosition(); lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager) .findLastVisibleItemPosition(); } else if (mLayoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager mStaggeredGridLayoutManager = (StaggeredGridLayoutManager) mLayoutManager; if (null == mPositions) { mPositions = new int[mStaggeredGridLayoutManager.getSpanCount()]; } mStaggeredGridLayoutManager.findFirstVisibleItemPositions(mPositions); mStaggeredGridLayoutManager.findLastVisibleItemPositions(mPositions); firstVisibleItemPosition = getFirst(mPositions); lastVisibleItemPosition = getLast(mPositions); } else { throw new IllegalArgumentException( "The layoutManager must be one of LinearLayoutManager, " + "GridLayoutManager or StaggeredGridLayoutManager"); } } }
Example 9
Source File: VerticalRecyclerView.java From YCAudioPlayer with Apache License 2.0 | 4 votes |
@Override public boolean dispatchTouchEvent(MotionEvent ev) { LayoutManager layoutManager = getLayoutManager(); if (layoutManager != null) { if (layoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); } else if (layoutManager instanceof LinearLayoutManager) { lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; firstPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); staggeredGridLayoutManager.findFirstVisibleItemPositions(firstPositions); lastVisibleItemPosition = findMax(lastPositions); firstVisibleItemPosition = findMin(firstPositions); } } else { throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = ev.getX(); downY = ev.getY(); //如果滑动到了最底部,就允许继续向上滑动加载下一页,否者不允许 getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: float dx = ev.getX() - downX; float dy = ev.getY() - downY; boolean allowParentTouchEvent; if (Math.abs(dy) > Math.abs(dx)) { if (dy > 0) { //位于顶部时下拉,让父View消费事件 allowParentTouchEvent = isTop = firstVisibleItemPosition == 0 && getChildAt(0).getTop() >= 0; } else { //位于底部时上拉,让父View消费事件 int visibleItemCount = layoutManager.getChildCount(); int totalItemCount = layoutManager.getItemCount(); allowParentTouchEvent = isBottom = visibleItemCount > 0 && (lastVisibleItemPosition) >= totalItemCount - 1 && getChildAt(getChildCount() - 1).getBottom() <= getHeight(); } } else { //水平方向滑动 allowParentTouchEvent = true; } getParent().requestDisallowInterceptTouchEvent(!allowParentTouchEvent); } return super.dispatchTouchEvent(ev); }
Example 10
Source File: VerticalRecyclerView.java From YCShopDetailLayout with Apache License 2.0 | 4 votes |
@Override public boolean dispatchTouchEvent(MotionEvent ev) { RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager != null) { if (layoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); } else if (layoutManager instanceof LinearLayoutManager) { lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; firstPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); staggeredGridLayoutManager.findFirstVisibleItemPositions(firstPositions); lastVisibleItemPosition = findMax(lastPositions); firstVisibleItemPosition = findMin(firstPositions); } } else { throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = ev.getX(); downY = ev.getY(); //如果滑动到了最底部,就允许继续向上滑动加载下一页,否者不允许 getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: float dx = ev.getX() - downX; float dy = ev.getY() - downY; boolean allowParentTouchEvent; if (Math.abs(dy) > Math.abs(dx)) { if (dy > 0) { //位于顶部时下拉,让父View消费事件 allowParentTouchEvent = isTop = firstVisibleItemPosition == 0 && getChildAt(0).getTop() >= 0; } else { //位于底部时上拉,让父View消费事件 int visibleItemCount = layoutManager.getChildCount(); int totalItemCount = layoutManager.getItemCount(); allowParentTouchEvent = isBottom = visibleItemCount > 0 && (lastVisibleItemPosition) >= totalItemCount - 1 && getChildAt(getChildCount() - 1).getBottom() <= getHeight(); } } else { //水平方向滑动 allowParentTouchEvent = true; } getParent().requestDisallowInterceptTouchEvent(!allowParentTouchEvent); break; default: break; } return super.dispatchTouchEvent(ev); }
Example 11
Source File: WXRecyclerViewOnScrollListener.java From ucar-weex-core with Apache License 2.0 | 4 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if(listener == null){ return; } IOnLoadMoreListener iOnLoadMoreListener = listener.get(); if(iOnLoadMoreListener!=null) { iOnLoadMoreListener.onBeforeScroll(dx, dy); if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; mLastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition(); int firstVisible = linearLayoutManager.findFirstVisibleItemPosition(); iOnLoadMoreListener.notifyAppearStateChange(firstVisible , mLastVisibleItemPosition , dx , dy); } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.GRID; GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; mLastVisibleItemPosition = gridLayoutManager.findLastVisibleItemPosition(); iOnLoadMoreListener.notifyAppearStateChange(gridLayoutManager.findFirstVisibleItemPosition() , mLastVisibleItemPosition , dx , dy); } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int newSpanCount = staggeredGridLayoutManager.getSpanCount(); if (mLastPositions == null || newSpanCount != mLastPositions.length) { mLastPositions = new int[newSpanCount]; } if (mFirstPositions == null || newSpanCount != mFirstPositions.length) { mFirstPositions = new int[newSpanCount]; } //avoid crash of support-v7 original bug try{ staggeredGridLayoutManager.findFirstVisibleItemPositions(mFirstPositions); mFirstVisibleItemPosition = findMin(mFirstPositions); staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions); mLastVisibleItemPosition = findMax(mLastPositions); iOnLoadMoreListener.notifyAppearStateChange( mFirstVisibleItemPosition , mLastVisibleItemPosition , dx , dy); }catch(Exception e){ e.printStackTrace(); WXLogUtils.e(e.toString()); } } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } } }
Example 12
Source File: LoadMoreScrollListener.java From LazyRecyclerAdapter with MIT License | 4 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManagerType == null) { if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.GRID; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; } else { throw new RuntimeException("Unsupported"); } } switch (layoutManagerType) { case LINEAR: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager) .findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager) .findFirstVisibleItemPosition(); break; case GRID: lastVisibleItemPosition = ((GridLayoutManager) layoutManager) .findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager) .findFirstVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } if (firstPositions == null) { firstPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); staggeredGridLayoutManager.findFirstVisibleItemPositions(firstPositions); lastVisibleItemPosition = findMax(lastPositions); firstVisibleItemPosition = findMin(firstPositions); break; } onScrolled(firstVisibleItemPosition); }