Java Code Examples for androidx.recyclerview.widget.GridLayoutManager#findFirstVisibleItemPosition()

The following examples show how to use androidx.recyclerview.widget.GridLayoutManager#findFirstVisibleItemPosition() . 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: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 6 votes vote down vote up
/**
 * 获取recyclerView滑动距离
 */
private int getScrollYDistance() {
    if (!(recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
        return 0;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    int position = gridLayoutManager.findFirstVisibleItemPosition();
    if (position < 0) {
        position = 0;
    }
    View firstVisibleChildView = gridLayoutManager.findViewByPosition(position);
    if (firstVisibleChildView == null) {
        return 0;
    }

    int itemHeight = firstVisibleChildView.getHeight() + PViewSizeUtils.dp(recyclerView.getContext(), 2);
    return (position / getSpanCount()) * itemHeight - firstVisibleChildView.getTop();
}
 
Example 2
Source File: AlbumMediaAdapter.java    From Matisse with Apache License 2.0 6 votes vote down vote up
public void refreshSelection() {
    GridLayoutManager layoutManager = (GridLayoutManager) mRecyclerView.getLayoutManager();
    int first = layoutManager.findFirstVisibleItemPosition();
    int last = layoutManager.findLastVisibleItemPosition();
    if (first == -1 || last == -1) {
        return;
    }
    Cursor cursor = getCursor();
    for (int i = first; i <= last; i++) {
        RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(first);
        if (holder instanceof MediaViewHolder) {
            if (cursor.moveToPosition(i)) {
                setCheckStatus(Item.valueOf(cursor), ((MediaViewHolder) holder).mMediaGrid);
            }
        }
    }
}
 
Example 3
Source File: MainActivity.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the user started this activity by pressing the back button on another main activity.
 * If so it runs the show animation for the activity's icon, text and visual elements.
 */
public void checkIsBackFromMainActivity() {
    if (isTransitioned) {
        GridLayoutManager manager = (GridLayoutManager) mRecyclerView.getLayoutManager();
        final int DELAY_BETWEEN = 50;
        int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
        int lastVisibleItemPosition = manager.findLastVisibleItemPosition();

        int startPositionCol = AnimationService.getColumnPosFromIndex(firstVisibleItemPosition, mRecyclerView);
        int startPositionRow = AnimationService.getRowPosFromIndex(firstVisibleItemPosition, mRecyclerView);

        // Show the Activity Icon and Text
        AnimationService.startAlphaRevealAnimation(mCircleIconWrapper);
        AnimationService.startAlphaRevealAnimation(mTitleView);

        // Fake fill the RecyclerViews with children again
        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
            final View mView = mRecyclerView.getChildAt(i - firstVisibleItemPosition);

            int positionColumnDistance = Math.abs(AnimationService.getColumnPosFromIndex(i, mRecyclerView) - startPositionCol);
            int positionRowDistance = Math.abs(AnimationService.getRowPosFromIndex(i, mRecyclerView) - startPositionRow);
            int delay = (positionColumnDistance + positionRowDistance) * DELAY_BETWEEN;

            //int delay = (i - firstVisibleItemPosition) * DELAY_BETWEEN;
            if (mView != null) {
                AnimationService.startAlphaRevealAnimation(delay, mView, mAdapter instanceof StreamsAdapter);
            }
        }
        isTransitioned = false;
    }
}
 
Example 4
Source File: RecyclerViewTouchHelper.java    From YImagePicker with Apache License 2.0 5 votes vote down vote up
private int getItemHeight() {
    if (!(recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
        return 0;
    }
    GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
    int position = gridLayoutManager.findFirstVisibleItemPosition();
    if (position < 0) {
        position = 0;
    }
    View firstVisibleChildView = gridLayoutManager.findViewByPosition(position);
    if (firstVisibleChildView == null) {
        return 0;
    }
    return firstVisibleChildView.getHeight();
}
 
Example 5
Source File: RecyclerPreloadView.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(int newState) {
    super.onScrollStateChanged(newState);
    if (newState == SCROLL_STATE_IDLE || newState == SCROLL_STATE_DRAGGING) {
        LayoutManager layoutManager = getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager linearManager = (GridLayoutManager) layoutManager;
            mFirstVisiblePosition = linearManager.findFirstVisibleItemPosition();
            mLastVisiblePosition = linearManager.findLastVisibleItemPosition();
        }
    }
}
 
Example 6
Source File: MainActivity.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the user started this activity by pressing the back button on another main activity.
 * If so it runs the show animation for the activity's icon, text and visual elements.
 */
public void checkIsBackFromMainActivity() {
    if (isTransitioned) {
        GridLayoutManager manager = (GridLayoutManager) mRecyclerView.getLayoutManager();
        final int DELAY_BETWEEN = 50;
        int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
        int lastVisibleItemPosition = manager.findLastVisibleItemPosition();

        int startPositionCol = AnimationService.getColumnPosFromIndex(firstVisibleItemPosition, mRecyclerView);
        int startPositionRow = AnimationService.getRowPosFromIndex(firstVisibleItemPosition, mRecyclerView);

        // Show the Activity Icon and Text
        AnimationService.startAlphaRevealAnimation(mCircleIconWrapper);
        AnimationService.startAlphaRevealAnimation(mTitleView);

        // Fake fill the RecyclerViews with children again
        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) {
            final View mView = mRecyclerView.getChildAt(i - firstVisibleItemPosition);

            int positionColumnDistance = Math.abs(AnimationService.getColumnPosFromIndex(i, mRecyclerView) - startPositionCol);
            int positionRowDistance = Math.abs(AnimationService.getRowPosFromIndex(i, mRecyclerView) - startPositionRow);
            int delay = (positionColumnDistance + positionRowDistance) * DELAY_BETWEEN;

            //int delay = (i - firstVisibleItemPosition) * DELAY_BETWEEN;
            if (mView != null) {
                AnimationService.startAlphaRevealAnimation(delay, mView, mAdapter instanceof StreamsAdapter);
            }
        }
        isTransitioned = false;
    }
}
 
Example 7
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;
        }
    }
 
Example 8
Source File: AlScrollListener.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();

    visibleItemCount = recyclerView.getChildCount();
    if (manager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
        firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();
        totalItemCount = gridLayoutManager.getItemCount();
    } else if (manager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) manager;
        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
        totalItemCount = linearLayoutManager.getItemCount();
    }


    if (infiniteScrollingEnabled) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }

        if ((totalItemCount - visibleItemCount) == 0) {
            return;
        }


        if (!loading && (totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold)) {
            // End has been reached
            // do something
            onLoadMore();
            loading = true;
        }
    }

    if (firstVisibleItem == 0) {
        if (!controlsVisible) {
            onScrollUp();
            controlsVisible = true;
        }

        return;
    }

    if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
        onScrollDown();
        controlsVisible = false;
        scrolledDistance = 0;
    } else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
        onScrollUp();
        controlsVisible = true;
        scrolledDistance = 0;
    }

    if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) {
        scrolledDistance += dy;
    }
}