How to expand and collapse a layout above recyclerView when scrolling?

Viewed 185

I have an app which have a recycler with product and anothe layout above with categories I want to expand and collapse the categories when the recycler scroll like this video

enter image description here

I made a class that listens to scroll:

public abstract class MyRecyclerScroll extends RecyclerView.OnScrollListener {

    final float MINIMUM = 200;
    int scrollDist = 0;
    boolean isVisible = true;
    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (isVisible && scrollDist > MINIMUM) {
            hide();
            scrollDist = 0;
            isVisible = false;
        }
        else if (!isVisible && scrollDist < -MINIMUM) {
            show();
            scrollDist = 0;
            isVisible = true;
        }

        if ((isVisible && dy > 0) || (!isVisible && dy < 0)) {
            scrollDist += dy;
        }
    }

    public abstract void show();
    public abstract void hide();
}

Then I used it on recycler.setOnScrollListener like this:

rec.setOnScrollListener(new MyRecyclerScroll() {
    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }
});
0 Answers
Related