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

The following examples show how to use androidx.recyclerview.widget.StaggeredGridLayoutManager#findLastCompletelyVisibleItemPositions() . 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 last completely visible grid position.
 *
 * @return the last completely visible position or RecyclerView.NO_POSITION if any error occurs.
 * @see #getLastVisiblePosition()
 */
public int getLastCompletelyVisiblePosition() {
    LayoutManager lm = getLayoutManager();
    if (lm instanceof LinearLayoutManager) {
        LinearLayoutManager llm = (LinearLayoutManager) lm;
        return llm.findLastCompletelyVisibleItemPosition();
    } else {
        StaggeredGridLayoutManager sglm = (StaggeredGridLayoutManager) lm;
        if (sglm == null) {
            return NO_POSITION;
        }
        int[] lastVisibleItemPositions = sglm.findLastCompletelyVisibleItemPositions(null);
        Arrays.sort(lastVisibleItemPositions);

        return lastVisibleItemPositions[lastVisibleItemPositions.length - 1];
    }
}
 
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>last completely</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>last fully</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findLastVisibleItemPosition()
 * @since 5.0.0-b8
 */
@Override
public int findLastCompletelyVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findLastCompletelyVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findLastCompletelyVisibleItemPositions(null)[i];
            if (nextPosition > position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
    }
}
 
Example 3
Source File: FamiliarRecyclerView.java    From FamiliarRecyclerView with MIT License 5 votes vote down vote up
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;
}