Boris Bankov
Boris Bankov

Reputation: 23

Android onScrolled not called when View is set to GONE

I am trying to hide a RelativeLayout when I scroll up and show it when I scroll down. onScroll works fine and is invoked every time until View is set to GONE.

final RelativeLayout placeHeaderMain = findViewById(R.id.place_header_main);

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (dy > 0) {
            // Scrolling up
            placeHeaderMain.setVisibility(View.GONE);

        } else {
            // Scrolling down
           placeHeaderMain.setVisibility(View.VISIBLE);
        }
    }
});

I want my listener to continue working after setting the View to Gone in order to make it Visible when scrolled down.

Thanks in advance.

Upvotes: 2

Views: 404

Answers (2)

ardiien
ardiien

Reputation: 804

Are there enough items to be scrolled?

That code above won't be triggered if dy == 0. It could be not enough items to make the scroll and it will return dy equal to 0, father more it won't to call onScroll(...)

What dy do you have when RelativeLayout has hidden? Try to check that method below:

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
}

Upvotes: 1

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

Try to set the view to INVISIBLE and not to GONE.
when you set any view to View.GONE he is invisible and it doesn't take any space inside your layout , but when you set a view to View.INVISIBLE like before he will be invisible but unlike View.GONE your view still takes up space inside the layout.

Upvotes: 0

Related Questions