How to detect when RecyclerView is scrolled to most top position
Asked Answered
M

3

6

I have RecyclerView above that i have an AppBarLayout whose height is larger than 255 px. When user scrolls RecyclerView, AppBarLayout has an fling issue. To avoid that i decided to expand AppBarLayout manually. My RecyclerView made of GridLayoutManager with span of 3. I used below code to listen RecyclerView top reach

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            int firstVisiblePosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
            if (firstVisiblePosition == 0) {
                appBarLayout.setExpanded(true, true);
            }
        }
    }
});
Mogul answered 17/1, 2017 at 6:30 Comment(0)
N
5
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            int firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition();

            if(firstVisibleItem == 0){
                // your code
            }
        }
    });
Nguyen answered 17/1, 2017 at 7:11 Comment(4)
i want to get top reached on scroll upMogul
nope i have an appbar above recyclerview, when scrolled down appbar manually expanded and when scrolled up app bar collapsed manuallyMogul
place your code in onScrolled() instead of onScrollStateChanged()Nguyen
@HafizMuneeb my list size is greater than the screen size , in this cast the method always gets called , how can i detect recyclerview scrolled to top most item in this case ?Ware
A
0

This answer will help you. Yours truly

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {

            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            totalItemCount = layoutManager.getItemCount();
            visibleItemCount = layoutManager.getChildCount();
            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
            if ((visibleItemCount + pastVisiblesItems) >= totalItemCount ) {

                Log.d(TAG, "scroll down");



            }
            else{
                Log.d(TAG, "scroll up");
            }
        }
    });
Aimless answered 13/12, 2019 at 7:36 Comment(0)
N
0

I found a brilliant solution here. You could implement it inside your RecyclerView.OnScrollListener like this:

binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)

        if (!recyclerView.canScrollVertically(-1)) {
            Log.d(TAG, "THE TOP")
        } else {
            Log.d(TAG, "NOT TOP")
        }
    }
})
Nephograph answered 10/10 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.