Java Code Examples for androidx.recyclerview.widget.StaggeredGridLayoutManager#findFirstVisibleItemPositions()

The following examples show how to use androidx.recyclerview.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: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the first visible grid position.
 *
 * @return the first visible position or RecyclerView.NO_POSITION if any error occurs.
 * @see #getFirstCompletelyVisiblePosition()
 */
public int getFirstVisiblePosition() {
    LayoutManager lm = getLayoutManager();
    if (lm instanceof LinearLayoutManager) {
        LinearLayoutManager llm = (LinearLayoutManager) lm;

        return llm.findFirstVisibleItemPosition();
    } else {
        StaggeredGridLayoutManager sglm = (StaggeredGridLayoutManager) lm;
        if (sglm == null) {
            return NO_POSITION;
        }
        int[] firstVisibleItemPositions = sglm.findFirstVisibleItemPositions(null);
        Arrays.sort(firstVisibleItemPositions);

        return firstVisibleItemPositions[0];
    }
}
 
Example 2
Source File: FlexibleLayoutManager.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to find the adapter position of the <b>first partially</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>first partially</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findFirstCompletelyVisibleItemPosition()
 * @since 5.0.0-rc1
 */
@Override
public int findFirstVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findFirstVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findFirstVisibleItemPositions(null)[i];
            if (nextPosition < position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
    }
}
 
Example 3
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public static boolean canAutoRefresh(View view) {
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return false;
        }
        int firstVisiblePosition = -1;
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
            if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            firstVisiblePosition = linearManager.findFirstVisibleItemPosition();
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
            if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            int[] firstPositions = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findFirstVisibleItemPositions(firstPositions);
            for (int value : firstPositions) {
                if (value == 0) {
                    firstVisiblePosition = 0;
                    break;
                }
            }
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        return adapter != null && firstVisiblePosition == 0;
    }
    return false;
}
 
Example 4
Source File: LayoutManagerUtil.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
private static int caseStaggeredGrid(RecyclerView.LayoutManager layoutManager, boolean findFirst) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    int[] positions = new int[staggeredGridLayoutManager.getSpanCount()];

    if (findFirst) {
        staggeredGridLayoutManager.findFirstVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[0];
    } else {
        staggeredGridLayoutManager.findLastVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[positions.length - 1];
    }
}
 
Example 5
Source File: BackToTopUtils.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static int getFirstVisibleItemPosition(StaggeredGridLayoutManager manager) {
    return manager.findFirstVisibleItemPositions(null)[0];
}
 
Example 6
Source File: UltimateRecyclerView.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private void scroll_load_more_detection(RecyclerView recyclerView) {

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

        if (layoutManagerType == null) {
            if (layoutManager instanceof GridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
            } else if (layoutManager instanceof LinearLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
            } else {
                throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
            }
        }

        mTotalItemCount = layoutManager.getItemCount();
        mVisibleItemCount = layoutManager.getChildCount();

        switch (layoutManagerType) {
            case LINEAR:
                mFirstVisibleItem = mRecyclerViewHelper.findFirstVisibleItemPosition();
                lastVisibleItemPosition = mRecyclerViewHelper.findLastVisibleItemPosition();
                break;
            case GRID:
                if (layoutManager instanceof GridLayoutManager) {
                    GridLayoutManager ly = (GridLayoutManager) layoutManager;
                    lastVisibleItemPosition = ly.findLastVisibleItemPosition();
                    mFirstVisibleItem = ly.findFirstVisibleItemPosition();
                }
                break;
            case STAGGERED_GRID:
                if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager sy = (StaggeredGridLayoutManager) layoutManager;

                    if (mlastPositionsStaggeredGridLayout == null)
                        mlastPositionsStaggeredGridLayout = new int[sy.getSpanCount()];

                    sy.findLastVisibleItemPositions(mlastPositionsStaggeredGridLayout);
                    lastVisibleItemPosition = findMax(mlastPositionsStaggeredGridLayout);

                    sy.findFirstVisibleItemPositions(mlastPositionsStaggeredGridLayout);
                    mFirstVisibleItem = findMin(mlastPositionsStaggeredGridLayout);
                }
                break;
        }

        if (automaticLoadMoreEnabled) {

            if (mTotalItemCount > previousTotal) {
                automaticLoadMoreEnabled = false;
                previousTotal = mTotalItemCount;
            }
        }

        boolean bottomEdgeHit = (mTotalItemCount - mVisibleItemCount) <= mFirstVisibleItem;

        if (bottomEdgeHit) {
            if (mIsLoadMoreWidgetEnabled) {
                /**auto activate load more**/
                if (!automaticLoadMoreEnabled) {
                    onLoadMoreListener.loadMore(mRecyclerView.getAdapter().getItemCount(), lastVisibleItemPosition);
                    automaticLoadMoreEnabled = true;
                }
            }
            mAdapter.internalExecuteLoadingView();
            previousTotal = mTotalItemCount;
        }
    }