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.

Answer

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);
}
  • 1
Reply Report

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.

  • 0
Reply Report
      • 1
    • Yes, I don't want the layout to take space. I want to hide it. The problem was that I don't have enough items to make the scroll.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions