Java Code Examples for android.support.v7.widget.StaggeredGridLayoutManager#findFirstCompletelyVisibleItemPositions()
The following examples show how to use
android.support.v7.widget.StaggeredGridLayoutManager#findFirstCompletelyVisibleItemPositions() .
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: OnRecyclerViewScrollListener.java From HeaderFooterRecyclerView with Apache License 2.0 | 6 votes |
/** * 计算第一个元素的位置 */ private int calculateFirstVisibleItemPos(RecyclerView.LayoutManager layoutManager, int firstVisibleItemPosition) { switch (mLayoutManagerType) { case LINEAR: mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case GRID: mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (mLastPositions == null) { mLastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } mLastPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions); mLastVisibleItemPosition = findMax(mLastPositions); staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(mLastPositions); firstVisibleItemPosition = findMax(mLastPositions); break; } return firstVisibleItemPosition; }
Example 2
Source File: FamiliarRecyclerView.java From AndroidBase with Apache License 2.0 | 6 votes |
public int getFirstVisiblePosition() { LayoutManager layoutManager = getLayoutManager(); if (null == layoutManager) return 0; int ret = -1; switch (mLayoutManagerType) { case LAYOUT_MANAGER_TYPE_LINEAR: ret = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount(); break; case LAYOUT_MANAGER_TYPE_GRID: ret = ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount(); break; case LAYOUT_MANAGER_TYPE_STAGGERED_GRID: StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] firstVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()]; tempStaggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(firstVisibleItemPositions); ret = firstVisibleItemPositions[0] - getHeaderViewsCount(); break; } return ret < 0 ? 0 : ret; }
Example 3
Source File: OnRcvScrollListener.java From CommonAdapter with Apache License 2.0 | 6 votes |
/** * 计算第一个元素的位置 */ private int calculateFirstVisibleItemPos(int type, RecyclerView.LayoutManager layoutManager, int firstVisibleItemPosition) { switch (type) { case TYPE_LINEAR: mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case TYPE_GRID: mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case TYPE_STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (mLastPositions == null) { mLastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } mLastPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions); mLastVisibleItemPosition = findMax(mLastPositions); staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(mLastPositions); firstVisibleItemPosition = findMax(mLastPositions); break; } return firstVisibleItemPosition; }
Example 4
Source File: OnRecyclerViewScrollListener.java From ExRecyclerView with Apache License 2.0 | 6 votes |
/** * 计算第一个元素的位置 */ private int calculateFirstVisibleItemPos(RecyclerView.LayoutManager layoutManager, int firstVisibleItemPosition) { switch (mLayoutManagerType) { case LINEAR: mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case GRID: mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (mLastPositions == null) { mLastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } mLastPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions); mLastVisibleItemPosition = findMax(mLastPositions); staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(mLastPositions); firstVisibleItemPosition = findMax(mLastPositions); break; } return firstVisibleItemPosition; }
Example 5
Source File: PtrDefaultHandler.java From JD-Test with Apache License 2.0 | 5 votes |
public static boolean canChildScrollUp(View view) { if (android.os.Build.VERSION.SDK_INT < 14) { if (view instanceof AbsListView) { final AbsListView absListView = (AbsListView) view; return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0) .getTop() < absListView.getPaddingTop()); } else if(view instanceof RecyclerView){ RecyclerView recyclerView = (RecyclerView)view; RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if(layoutManager instanceof LinearLayoutManager){ int position = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition(); return position != 0; }else{ if(layoutManager instanceof StaggeredGridLayoutManager ){ StaggeredGridLayoutManager stagger = (StaggeredGridLayoutManager) layoutManager; int[] positions = stagger.findFirstCompletelyVisibleItemPositions(null); return positions[0] != 0; }else{ throw new RuntimeException("can not support this LayoutManager "); } } }else{ return view.getScrollY() > 0; } } else { return view.canScrollVertically(-1); } }
Example 6
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 7
Source File: BounceTouchListener.java From Bounce with Apache License 2.0 | 5 votes |
private boolean hasHitTop() { if (mMainView instanceof ScrollView) { ScrollView scrollView = (ScrollView) mMainView; return scrollView.getScrollY() == 0; } else if (mMainView instanceof ListView) { ListView listView = (ListView) mMainView; if (listView.getAdapter() != null) { if (listView.getAdapter().getCount() > 0) { return listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() >= 0; } } } else if (mMainView instanceof RecyclerView) { RecyclerView recyclerView = (RecyclerView) mMainView; if (recyclerView.getAdapter() != null && recyclerView.getLayoutManager() != null) { RecyclerView.Adapter adapter = recyclerView.getAdapter(); if (adapter.getItemCount() > 0) { RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; return linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0; } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] checks = staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null); for (int check : checks) { if (check == 0) return true; } } } } } return false; }
Example 8
Source File: LRecyclerView.java From LRecyclerView with Apache License 2.0 | 4 votes |
@Override public void onScrolled(int dx, int dy) { super.onScrolled(dx, dy); int firstVisibleItemPosition = 0; RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManagerType == null) { if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LayoutManagerType.LinearLayout; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LayoutManagerType.GridLayout; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LayoutManagerType.StaggeredGridLayout; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } } switch (layoutManagerType) { case LinearLayout: firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case StaggeredGridLayout: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(lastPositions); firstVisibleItemPosition = findMax(lastPositions); break; default: break; } // 根据类型来计算出第一个可见的item的位置,由此判断是否触发到底部的监听器 // 计算并判断当前是向上滑动还是向下滑动 calculateScrollUpOrDown(firstVisibleItemPosition, dy); // 移动距离超过一定的范围,我们监听就没有啥实际的意义了 mScrolledXDistance += dx; mScrolledYDistance += dy; mScrolledXDistance = (mScrolledXDistance < 0) ? 0 : mScrolledXDistance; mScrolledYDistance = (mScrolledYDistance < 0) ? 0 : mScrolledYDistance; if (mIsScrollDown && (dy == 0)) { mScrolledYDistance = 0; } //Be careful in here if (null != mLScrollListener) { mLScrollListener.onScrolled(mScrolledXDistance, mScrolledYDistance); } if (mLoadMoreListener != null && mLoadMoreEnabled) { int visibleItemCount = layoutManager.getChildCount(); int totalItemCount = layoutManager.getItemCount(); if (visibleItemCount > 0 && lastVisibleItemPosition >= totalItemCount - 1 && (isCritical ? totalItemCount >= visibleItemCount : totalItemCount > visibleItemCount) && !isNoMore && !mRefreshing) { mFootView.setVisibility(View.VISIBLE); if (!mLoadingData) { mLoadingData = true; mLoadMoreFooter.onLoading(); mLoadMoreListener.onLoadMore(); } } } if (isOnTop() && dy > 0 && mRefreshHeader.getType() == IRefreshHeader.TYPE_HEADER_MATERIAL && !mRefreshing && (appbarState == AppBarStateChangeListener.State.EXPANDED)) { mRefreshHeader.onMove(dy, mScrolledYDistance); } }
Example 9
Source File: LuRecyclerView.java From LRecyclerView with Apache License 2.0 | 4 votes |
@Override public void onScrolled(int dx, int dy) { super.onScrolled(dx, dy); int firstVisibleItemPosition = 0; RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManagerType == null) { if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LayoutManagerType.LinearLayout; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LayoutManagerType.GridLayout; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LayoutManagerType.StaggeredGridLayout; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } } switch (layoutManagerType) { case LinearLayout: firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case StaggeredGridLayout: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(lastPositions); firstVisibleItemPosition = findMax(lastPositions); break; default: break; } // 根据类型来计算出第一个可见的item的位置,由此判断是否触发到底部的监听器 // 计算并判断当前是向上滑动还是向下滑动 calculateScrollUpOrDown(firstVisibleItemPosition, dy); // 移动距离超过一定的范围,我们监听就没有啥实际的意义了 mScrolledXDistance += dx; mScrolledYDistance += dy; mScrolledXDistance = (mScrolledXDistance < 0) ? 0 : mScrolledXDistance; mScrolledYDistance = (mScrolledYDistance < 0) ? 0 : mScrolledYDistance; if (mIsScrollDown && (dy == 0)) { mScrolledYDistance = 0; } //Be careful in here if (null != mLScrollListener) { mLScrollListener.onScrolled(mScrolledXDistance, mScrolledYDistance); } //如果想要滑动到底部自动加载更多,mIsManualLoadMore必须为false if (mIsManualLoadMore) { if (!isNoMore) { Log.e("lzx","onScrooo set visible"); mLoadingData = true; mLoadMoreFooter.setOnClickLoadMoreListener(mLoadMoreListener); } } else { if (mLoadMoreListener != null && mLoadMoreEnabled) { int visibleItemCount = layoutManager.getChildCount(); int totalItemCount = layoutManager.getItemCount(); if (visibleItemCount > 0 && lastVisibleItemPosition >= totalItemCount - 1 && (isCritical ? totalItemCount >= visibleItemCount : totalItemCount > visibleItemCount) && !isNoMore && !mRefreshing) { mFootView.setVisibility(View.VISIBLE); if (!mLoadingData) { mLoadingData = true; mLoadMoreFooter.onLoading(); mLoadMoreListener.onLoadMore(); } } } } }