Search code examples
androidandroid-recyclerviewandroid-appbarlayoutonscrolllistener

Detect the top of RecyclerView?


I have a RecyclerView, first its scrolled down, now when we scroll up, it reach top most point. I want to detect that .Is there any way? I have AppBar whose height is greater than device width. so it cause an fling on scroll. I thought by getting top reached listener manually expand AppBar to avoid fling.

    if (layoutManager.firstCompletelyVisibleItemPosition() == 0) {
    //this is the top of the RecyclerView 
    }

this code i already used but my issue is that i can't scroll up RecyclerView after putting this code. Because my RecyclerView uses GridLayoutManager with 3 columns.


Solution

  • If you have a LinearLayoutManager set on your RecyclerView you can use it to find the first visible item:

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerview.setLayoutManager(layoutManager);
    
    if (layoutManager.firstCompletelyVisibleItemPosition() == 0) {
        //this is the top of the RecyclerView
        //Do Something
    }