Java Code Examples for android.support.v7.widget.StaggeredGridLayoutManager#getSpanCount()
The following examples show how to use
android.support.v7.widget.StaggeredGridLayoutManager#getSpanCount() .
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: StaggeredSpaceDecoration.java From ItemDecorationDemo with Apache License 2.0 | 6 votes |
@Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { RecyclerView.LayoutManager originalManager = parent.getLayoutManager(); if (originalManager == null || !(originalManager instanceof StaggeredGridLayoutManager)) { return; } StaggeredGridLayoutManager manager = (StaggeredGridLayoutManager) originalManager; mSpanCount = manager.getSpanCount(); mItemCount = parent.getAdapter().getItemCount(); if (manager.getOrientation() == StaggeredGridLayoutManager.VERTICAL) { handleVertical(outRect, view, parent); } else { handleHorizontal(outRect, view, parent); } }
Example 2
Source File: RecyclerViewScrollListener.java From Android-RecyclerViewHelper with Apache License 2.0 | 6 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); } else if (layoutManager instanceof LinearLayoutManager) { lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); } else { throw new RuntimeException("Unsupported LayoutManager used. " + "Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } }
Example 3
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 4
Source File: FamiliarDefaultItemDecoration.java From AndroidBase with Apache License 2.0 | 5 votes |
private void initLayoutManagerType() { this.mLayoutManagerType = mRecyclerViewListener.getCurLayoutManagerType(); RecyclerView.LayoutManager layoutManager = mRecyclerViewListener.getCurLayoutManager(); switch (mLayoutManagerType) { case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR: LinearLayoutManager curLinearLayoutManager = (LinearLayoutManager)layoutManager; if (curLinearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) { mOrientation = OrientationHelper.HORIZONTAL; } else { mOrientation = OrientationHelper.VERTICAL; } break; case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID: GridLayoutManager curGridLayoutManager = (GridLayoutManager)layoutManager; mGridSpanCount = curGridLayoutManager.getSpanCount(); if (curGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) { mOrientation = OrientationHelper.HORIZONTAL; } else { mOrientation = OrientationHelper.VERTICAL; } break; case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID: StaggeredGridLayoutManager curStaggeredGridLayoutManager = (StaggeredGridLayoutManager)layoutManager; mGridSpanCount = curStaggeredGridLayoutManager.getSpanCount(); if (curStaggeredGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) { mOrientation = OrientationHelper.HORIZONTAL; } else { mOrientation = OrientationHelper.VERTICAL; } break; default: this.mLayoutManagerType = FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR; } initDivisible(); }
Example 5
Source File: EasyRefreshLayout.java From EasyRefreshLayout with Apache License 2.0 | 5 votes |
/** * 最后一个的位置 */ private int getLastVisiBleItem() { RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); int lastVisibleItemPosition = -1; int layoutManagerType = 0; if (layoutManager instanceof GridLayoutManager) { layoutManagerType = 1; } else if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = 0; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = 2; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } switch (layoutManagerType) { case 0: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case 1: lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case 2: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); break; } return lastVisibleItemPosition; }
Example 6
Source File: EndlessRecyclerOnScrollListener.java From HeaderAndFooterRecyclerView with Apache License 2.0 | 5 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 GridLayoutManager) { layoutManagerType = LayoutManagerType.GridLayout; } else if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LayoutManagerType.LinearLayout; } 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: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: 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); break; } }
Example 7
Source File: BasePullUpRecyclerAdapter.java From CoreModule with Apache License 2.0 | 5 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LINEAR; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = GRID; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = STAGGERED_GRID; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are " + "LinearLayoutManager, GridLayoutManager and " + "StaggeredGridLayoutManager"); } switch (layoutManagerType) { case LINEAR: lastVisibleItem = ((LinearLayoutManager) layoutManager) .findLastVisibleItemPosition(); break; case GRID: lastVisibleItem = ((GridLayoutManager) layoutManager) .findLastVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItem = findMax(lastPositions); break; } }
Example 8
Source File: DRecyclerViewScrollListener.java From Pas with Apache License 2.0 | 5 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 = 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: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: 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); break; } }
Example 9
Source File: BasePullUpRecyclerAdapter.java From RecyclerViewDemo with Apache License 2.0 | 5 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LINEAR; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = GRID; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = STAGGERED_GRID; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are " + "LinearLayoutManager, GridLayoutManager and " + "StaggeredGridLayoutManager"); } switch (layoutManagerType) { case LINEAR: lastVisibleItem = ((LinearLayoutManager) layoutManager) .findLastVisibleItemPosition(); break; case GRID: lastVisibleItem = ((GridLayoutManager) layoutManager) .findLastVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItem = findMax(lastPositions); break; } }
Example 10
Source File: RecyclerViewOnScroll.java From GankGirl with GNU Lesser General Public License v2.1 | 5 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 = 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: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: 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); break; } }
Example 11
Source File: EndlessRecyclerOnScrollListener.java From HeaderFooterRecyclerView with Apache License 2.0 | 5 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 = 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: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GridLayout: 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); break; } }
Example 12
Source File: QuickReturnRecyclerView.java From droidddle with Apache License 2.0 | 4 votes |
public void checkLoadingMore() { boolean firstVisible = false; LayoutManager manager = getLayoutManager(); int visibleItemCount = manager.getChildCount(); int totalItemCount = manager.getItemCount(); int firstVisibleItems = 0; if (manager instanceof LinearLayoutManager) { firstVisibleItems = ((LinearLayoutManager) manager).findFirstVisibleItemPosition(); } else if (manager instanceof GridLayoutManager) { firstVisibleItems = ((GridLayoutManager) manager).findFirstVisibleItemPosition(); } else if (manager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager sg = ((StaggeredGridLayoutManager) manager); int[] items = new int[sg.getSpanCount()]; items = ((StaggeredGridLayoutManager) manager).findFirstVisibleItemPositions(items); firstVisibleItems = items[0]; } // boolean topOfFirstItemVisible = manager.getChildAt(0).getTop() >= mFirstTop; // enabling or disabling the refresh layout if (firstVisibleItems == 0) { View child = manager.getChildAt(0); if (child != null) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); firstVisible = manager.getChildAt(0).getTop() >= (getPaddingTop() + lp.topMargin); } } // firstVisible = firstVisibleItems == 0 && topOfFirstItemVisible; mMoreListener.isFirstItemFullVisible(firstVisible); if (!mIsLoadingMore && mHasMoreData) { if ((visibleItemCount + firstVisibleItems) >= totalItemCount - 1) { if (!Utils.hasInternet(getContext())) { return; } mIsLoadingMore = true; mMoreListener.onLoadingMore(); } } }
Example 13
Source File: WXRecyclerViewOnScrollListener.java From weex 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(); // int lastVisibleItemPosition = -1; 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 LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } } switch (layoutManagerType) { case LINEAR: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager) .findLastVisibleItemPosition(); listener.get().notifyAppearStateChange(((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition() ,lastVisibleItemPosition ,dx ,dy); break; case GRID: lastVisibleItemPosition = ((GridLayoutManager) layoutManager) .findLastVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); break; } }
Example 14
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(); } } } } }
Example 15
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 16
Source File: RecyclerViewScrollListener.java From Social with Apache License 2.0 | 4 votes |
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { Log.d(TAG, "onScrolled: dy = " + dy); /** * 获取布局参数 */ RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); //如果为null,第一次运行,确定布局类型 if (mLayoutManagerType == null) { if (layoutManager instanceof LinearLayoutManager) { mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT; } else if (layoutManager instanceof GridLayoutManager) { mLayoutManagerType = LayoutManagerType.GRID_LAYOUT; } else if (layoutManager instanceof StaggeredGridLayoutManager) { mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT; } else { throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager"); } } //对于不太能够的布局参数,不同的方法获取到当前显示的最后一个条目数 switch (mLayoutManagerType) { case LINEAR_LAYOUT: mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GRID_LAYOUT: mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); int firstItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition(); Log.d(TAG, "onScrolled: firstPosition = " + firstItemPosition); break; case STAGGERED_GRID_LAYOUT: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (mLastPostions == null) { mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions); mLastVisibleItemPosition = findMax(mLastPostions); break; default: break; } }
Example 17
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 18
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 19
Source File: EndlessRecyclerViewScrollListener.java From scallop with MIT License | 4 votes |
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) { this.mLayoutManager = layoutManager; visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); }
Example 20
Source File: FamiliarRecyclerView.java From AndroidBase with Apache License 2.0 | 4 votes |
public int getLastVisiblePosition() { LayoutManager layoutManager = getLayoutManager(); if (null == layoutManager) return -1; int curItemCount = (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0); int ret = -1; switch (mLayoutManagerType) { case LAYOUT_MANAGER_TYPE_LINEAR: ret = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } break; case LAYOUT_MANAGER_TYPE_GRID: ret = ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } break; case LAYOUT_MANAGER_TYPE_STAGGERED_GRID: StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] lastVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()]; tempStaggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(lastVisibleItemPositions); if (lastVisibleItemPositions.length > 0) { int maxPos = lastVisibleItemPositions[0]; for (int curPos : lastVisibleItemPositions) { if (curPos > maxPos) maxPos = curPos; } ret = maxPos - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } } break; } return ret < 0 ? (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0) : ret; }