Java Code Examples for androidx.recyclerview.widget.LinearLayoutManager#findLastCompletelyVisibleItemPosition()

The following examples show how to use androidx.recyclerview.widget.LinearLayoutManager#findLastCompletelyVisibleItemPosition() . 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: HistoryView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onMore(View view, VisitInfo item) {
    mBinding.historyList.requestFocusFromTouch();

    int rowPosition = mHistoryAdapter.getItemPosition(item.getVisitTime());
    RecyclerView.ViewHolder row = mBinding.historyList.findViewHolderForLayoutPosition(rowPosition);
    boolean isLastVisibleItem = false;
    if (mBinding.historyList.getLayoutManager() instanceof LinearLayoutManager) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.historyList.getLayoutManager();
        int lastItem = mHistoryAdapter.getItemCount();
        if ((rowPosition == layoutManager.findLastVisibleItemPosition() || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition() ||
                rowPosition == layoutManager.findLastVisibleItemPosition() - 1 || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition() - 1)
                && (rowPosition != lastItem && rowPosition > 2)) {
            isLastVisibleItem = true;
        }
    }

    if (row != null) {
        mBinding.getCallback().onShowContextMenu(
                row.itemView,
                item,
                isLastVisibleItem);
    }
}
 
Example 2
Source File: DownloadsView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onMore(@NonNull View view, @NonNull Download item) {
    mBinding.downloadsList.requestFocusFromTouch();

    int rowPosition = mDownloadsAdapter.getItemPosition(item.getId());
    RecyclerView.ViewHolder row = mBinding.downloadsList.findViewHolderForLayoutPosition(rowPosition);
    boolean isLastVisibleItem = false;
    if (mBinding.downloadsList.getLayoutManager() instanceof LinearLayoutManager) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.downloadsList.getLayoutManager();
        int lastItem = mDownloadsAdapter.getItemCount();
        if ((rowPosition == layoutManager.findLastVisibleItemPosition() || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition() ||
                rowPosition == layoutManager.findLastVisibleItemPosition() - 1 || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition() - 1)
                && ((rowPosition == (lastItem - 1)) && rowPosition > 2)) {
            isLastVisibleItem = true;
        }
    }

    if (row != null) {
        mBinding.getCallback().onShowContextMenu(
                row.itemView,
                item,
                isLastVisibleItem);
    }
}
 
Example 3
Source File: BookmarksView.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onMore(@NonNull View view, @NonNull Bookmark item) {
    mBinding.bookmarksList.requestFocusFromTouch();

    int rowPosition = mBookmarkAdapter.getItemPosition(item.getGuid());
    RecyclerView.ViewHolder row = mBinding.bookmarksList.findViewHolderForLayoutPosition(rowPosition);
    boolean isLastVisibleItem = false;
    if (mBinding.bookmarksList.getLayoutManager() instanceof LinearLayoutManager) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mBinding.bookmarksList.getLayoutManager();
        int lastItem = mBookmarkAdapter.getItemCount();
        if ((rowPosition == layoutManager.findLastVisibleItemPosition() || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition() ||
                rowPosition == layoutManager.findLastVisibleItemPosition()-1 || rowPosition == layoutManager.findLastCompletelyVisibleItemPosition()-1)
                && rowPosition != lastItem) {
            isLastVisibleItem = true;
        }
    }

    mBinding.getCallback().onShowContextMenu(
            row.itemView,
            item,
            isLastVisibleItem);
}
 
Example 4
Source File: CardsAdapter.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 6 votes vote down vote up
@UiThread
public void notifyWinningCard(int winnerCardId) {
    for (int i = 0; i < cards.size(); i++) {
        CardsGroup group = cards.get(i);
        if (group.hasCard(winnerCardId)) {
            RecyclerView list = listener != null ? listener.getCardsRecyclerView() : null;
            if (list != null && list.getLayoutManager() instanceof LinearLayoutManager) { // Scroll only if item is not visible
                LinearLayoutManager llm = (LinearLayoutManager) list.getLayoutManager();
                int start = llm.findFirstCompletelyVisibleItemPosition();
                int end = llm.findLastCompletelyVisibleItemPosition();
                if (start == -1 || end == -1 || i >= end || i <= start)
                    list.getLayoutManager().smoothScrollToPosition(list, null, i);
            }

            group.setWinner();
            notifyItemChanged(i);
            break;
        }
    }
}
 
Example 5
Source File: OnScrollListener.java    From RecyclerViewHelper with Apache License 2.0 6 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
    // When not scroll
    if (newState == RecyclerView.SCROLL_STATE_IDLE && manager != null) {
        // Get the last fully displayed item position.
        int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
        int itemCount = manager.getItemCount();

        // Scroll to the last item, and it's scroll up.
        if (lastItemPosition == (itemCount - 1) && mIsScrollUp) {
            // Load More
            onLoadMore();
        }
    }
}
 
Example 6
Source File: OnRecyclerLoadMoreListener.java    From android with MIT License 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
        mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

        mItemCount = mLayoutManager.getItemCount();
        mLastCompletely = mLayoutManager.findLastCompletelyVisibleItemPosition();
    } else {
        return;
    }

    if (mLastLoad != mItemCount && mItemCount == mLastCompletely + 1) {
        LogUtils.debug(String.format("****************load more****************"));
        LogUtils.debug(String.format("mItemCount:%d \t mLastCompletely:%d", mItemCount, mLastCompletely));
        LogUtils.debug(String.format("****************load more****************"));

        mLastLoad = mItemCount;
        onLoadMore();
    }
}
 
Example 7
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 8
Source File: ListSearchView.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private void updateOverscrollMode() {
    LinearLayoutManager lm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    if (lm.findFirstCompletelyVisibleItemPosition() == 0 &&
            lm.findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() - 1) {
        mRecyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
    } else {
        mRecyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_ALWAYS);
    }
}
 
Example 9
Source File: ChatMessagesFragment.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private void checkForUnreadMessages() {
    if (mUnreadCtr.getVisibility() == View.GONE)
        return;
    LinearLayoutManager llm = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    int firstPos = llm.findFirstCompletelyVisibleItemPosition();
    int lastPos = llm.findLastCompletelyVisibleItemPosition();
    long firstId = firstPos != RecyclerView.NO_POSITION ? mAdapter.getItemId(firstPos) : -1;
    long lastId = lastPos != RecyclerView.NO_POSITION ? mAdapter.getItemId(lastPos) : -1;
    boolean found = false;
    if (mUnreadCheckedFirst == -1 && firstId != -1) {
        mUnreadCheckedFirst = firstId;
        mUnreadCheckedLast = firstId;
        found = checkItemForUnread(
                mAdapter.getMessage(mAdapter.getItemPosition(firstId)), mUnreadCheckFor);
    }
    while (firstId != -1 && firstId < mUnreadCheckedFirst) {
        found |= checkItemForUnread(mAdapter.getMessage(
                mAdapter.getItemPosition(mUnreadCheckedFirst)), mUnreadCheckFor);
        if (found)
            break;
        --mUnreadCheckedFirst;
    }
    while (lastId != -1 && lastId > mUnreadCheckedLast) {
        found |= checkItemForUnread(mAdapter.getMessage(
                mAdapter.getItemPosition(mUnreadCheckedLast)), mUnreadCheckFor);
        if (found)
            break;
        ++mUnreadCheckedLast;
    }
    if (found) {
        ChannelNotificationManager mgr = mConnection.getNotificationManager()
                .getChannelManager(mChannelName, true);
        mgr.clearUnreadMessages();
        mUnreadCtr.setVisibility(View.GONE);
        mUnreadCheckedFirst = -1;
        mUnreadCheckedLast = -1;
    }
}
 
Example 10
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 5 votes vote down vote up
private boolean isAtEdgeOfList(LinearLayoutManager lm) {
    if ((!lm.getReverseLayout() && gravity == Gravity.START)
            || (lm.getReverseLayout() && gravity == Gravity.END)
            || (!lm.getReverseLayout() && gravity == Gravity.TOP)
            || (lm.getReverseLayout() && gravity == Gravity.BOTTOM)) {
        return lm.findLastCompletelyVisibleItemPosition() == lm.getItemCount() - 1;
    } else if (gravity == Gravity.CENTER) {
        return lm.findFirstCompletelyVisibleItemPosition() == 0
                || lm.findLastCompletelyVisibleItemPosition() == lm.getItemCount() - 1;
    } else {
        return lm.findFirstCompletelyVisibleItemPosition() == 0;
    }
}
 
Example 11
Source File: ScrollPositionListener.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (llm != null) {
        int firstVisibleItemPosition = llm.findFirstVisibleItemPosition();
        int lastCompletelyVisibleItemPosition = llm.findLastCompletelyVisibleItemPosition();
        onPositionChangeListener.accept(Math.max(firstVisibleItemPosition, lastCompletelyVisibleItemPosition));
    }
}
 
Example 12
Source File: ArticleListsFragment.java    From android-app with GNU General Public License v3.0 5 votes vote down vote up
public void scroll(boolean up) {
    ArticleListFragment currentFragment = getCurrentFragment();

    if (currentFragment != null && currentFragment.recyclerViewLayoutManager != null) {
        LinearLayoutManager listLayout = currentFragment.recyclerViewLayoutManager;

        int numberOfVisibleItems =
                listLayout.findLastCompletelyVisibleItemPosition() -
                        listLayout.findFirstCompletelyVisibleItemPosition() + 1;

        int oldPositionOnTop = listLayout.findFirstCompletelyVisibleItemPosition();

        // scroll so that as many new articles are visible than possible with one overlap
        int newPositionOnTop;
        if (up) {
            newPositionOnTop = oldPositionOnTop - numberOfVisibleItems + 1;
        } else {
            newPositionOnTop = oldPositionOnTop + numberOfVisibleItems - 1;
        }

        if (newPositionOnTop >= listLayout.getItemCount()) {
            newPositionOnTop = listLayout.getItemCount() - numberOfVisibleItems - 1;
        } else if (newPositionOnTop < 0) {
            newPositionOnTop = 0;
        }

        Log.v(TAG, " scrolling to position: " + newPositionOnTop);

        listLayout.scrollToPositionWithOffset(newPositionOnTop, 0);
    }
}
 
Example 13
Source File: BaseQuickAdapter.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
private boolean isFullScreen(LinearLayoutManager llm) {
    return (llm.findLastCompletelyVisibleItemPosition() + 1) != getItemCount() ||
            llm.findFirstCompletelyVisibleItemPosition() != 0;
}