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

The following examples show how to use androidx.recyclerview.widget.GridLayoutManager#findLastVisibleItemPosition() . 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: 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 2
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 3
Source File: LazyFetchingOnScrollListener.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
public void checkForNewElements(RecyclerView recyclerView) {
    int currentOffset = mLazyFetchingActivity.getCurrentOffset();
    int maxElementsToFetchTotal = mLazyFetchingActivity.getMaxElementsToFetch();

    // If the task has already been run, make a new task as a task can only be run once.
    if (getElementsTask.getStatus() == AsyncTask.Status.FINISHED) {
        getElementsTask = new GetVisualElementsTask<>(mLazyFetchingActivity);
    }

    // Only bother to check if we need to fetch more game objects if we are not already in the process of doing so.
    if (getElementsTask.getStatus() != AsyncTask.Status.RUNNING && currentOffset < maxElementsToFetchTotal) {
        GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
        RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
        if (mAdapter == null) {
            return;
        }

        int lastViewPosition = lm.findLastVisibleItemPosition();
        int spanCount = lm.getSpanCount();
        int itemCount = mAdapter.getItemCount();
        final double FETCH_WHEN_BELOW_FIVE = 5;
        final double NUMBER_OF_ROWS = Math.ceil(itemCount / (spanCount * 1.0)); // Round UP to the nearest Integer
        final double LAST_ROW_VISIBLE = Math.ceil(lastViewPosition / (spanCount * 1.0)); // Round UP to the nearest Integer

        // If the Second to last or the last row is visible, then fetch more game objects.
        if (LAST_ROW_VISIBLE >= NUMBER_OF_ROWS - FETCH_WHEN_BELOW_FIVE) {
            getElementsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            mLazyFetchingActivity.startProgress();
        }
    }
}
 
Example 4
Source File: EventCaptureFormFragment.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkLastItem() {
    GridLayoutManager layoutManager = (GridLayoutManager) binding.formRecycler.getLayoutManager();
    int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
    boolean shouldShowFab =
            lastVisiblePosition == dataEntryAdapter.getItemCount() - 1 ||
                    dataEntryAdapter.getItemViewType(lastVisiblePosition) == 17;
    animateFabButton(shouldShowFab);
}
 
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: RecyclerPreloadView.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);
    if (onRecyclerViewPreloadListener != null) {
        if (isEnabledLoadMore) {
            LayoutManager layoutManager = getLayoutManager();
            if (layoutManager == null) {
                throw new RuntimeException("LayoutManager is null,Please check it!");
            }
            Adapter adapter = getAdapter();
            if (adapter == null) {
                throw new RuntimeException("Adapter is null,Please check it!");
            }
            boolean isReachBottom = false;
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                int rowCount = adapter.getItemCount() / gridLayoutManager.getSpanCount();
                int lastVisibleRowPosition = gridLayoutManager.findLastVisibleItemPosition() / gridLayoutManager.getSpanCount();
                isReachBottom = (lastVisibleRowPosition >= rowCount - reachBottomRow);
            }

            if (!isReachBottom) {
                isInTheBottom = false;
            } else if (!isInTheBottom) {
                onRecyclerViewPreloadListener.onRecyclerViewPreloadMore();
                if (dy > 0) {
                    isInTheBottom = true;
                }
            } else {
                // 属于首次进入屏幕未滑动且内容未超过一屏,用于确保分页数设置过小导致内容不足二次上拉加载...
                if (dy == 0) {
                    isInTheBottom = false;
                }
            }
        }
    }
}
 
Example 7
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 8
Source File: LazyFetchingOnScrollListener.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 5 votes vote down vote up
public void checkForNewElements(RecyclerView recyclerView) {
	int currentOffset = mLazyFetchingActivity.getCurrentOffset();
	int maxElementsToFetchTotal = mLazyFetchingActivity.getMaxElementsToFetch();

	// If the task has already been run, make a new task as a task can only be run once.
	if(getElementsTask.getStatus() == AsyncTask.Status.FINISHED) {
		getElementsTask = new GetVisualElementsTask<>();
	}

	// Only bother to check if we need to fetch more game objects if we are not already in the process of doing so.
	if(getElementsTask.getStatus() != AsyncTask.Status.RUNNING && currentOffset < maxElementsToFetchTotal) {
		GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
		RecyclerView.Adapter mAdapter = recyclerView.getAdapter();
		if (mAdapter == null ){
			return;
		}

		int lastViewPosition = lm.findLastVisibleItemPosition();
		int spanCount = lm.getSpanCount();
		int itemCount = mAdapter.getItemCount();
		final double FETCH_WHEN_BELOW_FIVE = 5;
		final double NUMBER_OF_ROWS = Math.ceil(itemCount / (spanCount * 1.0)); // Round UP to the nearest Integer
		final double LAST_ROW_VISIBLE = Math.ceil(lastViewPosition/(spanCount * 1.0)); // Round UP to the nearest Integer

		// If the Second to last or the last row is visible, then fetch more game objects.
		if(LAST_ROW_VISIBLE >= NUMBER_OF_ROWS - FETCH_WHEN_BELOW_FIVE) {
			getElementsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mLazyFetchingActivity);
			mLazyFetchingActivity.startProgress();
		}
	}
}
 
Example 9
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;
        }
    }