Rajeev Shetty
Rajeev Shetty

Reputation: 1784

RecyclerView LinearLayoutManager's findLastCompletelyVisibleItemPosition() vs findLastVisibleItemPosition() methods

findLastCompletelyVisibleItemPosition : Returns the adapter position of the last fully visible view. This position does not include adapter changes that were dispatched after the last layout pass.

findLastVisibleItemPosition: Returns the adapter position of the last visible view. This position does not include adapter changes that were dispatched after the last layout pass.

I have 16 items . So when scrolled till end (when last item is fully visible) both method gives result as 16. But when i scroll till end but last item is half visible findLastCompletelyVisibleItemPosition shows 14 and findLastVisibleItemPosition shows 15.

Can someone explain me why it is displaying 14 ? and what is the exact difference between these two function.

@Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        RecyclerView.LayoutManager linearLayoutManager = recyclerView.getLayoutManager();

        if(linearLayoutManager != null && linearLayoutManager instanceof LinearLayoutManager) {
            int position = ((LinearLayoutManager) linearLayoutManager).findLastCompletelyVisibleItemPosition();
            int position1 = ((LinearLayoutManager) linearLayoutManager).findLastVisibleItemPosition();


            Log.d(TAG, "position: " + position);
            Log.d(TAG, "position1: " + position1);
        }
    }

Upvotes: 1

Views: 1452

Answers (1)

Ricardo
Ricardo

Reputation: 9696

If you have 16 items it is impossible to return 16 as the visible position since your last position is the 15th.

Secondly it looks pretty straight forward to understand what each method does from their naming. If the last position is the 15th and you can see half of it, findLastCompletelyVisibleItemPosition will return 14 and findLastVisibleItemPosition will return 15.

Upvotes: 2

Related Questions