2

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.

2 Answers2

1

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);
}
ardiien
  • 767
  • 6
  • 26
  • Yes, you are right. The problem is that I don't have enough items to scroll through. I guess I have to hide the header layout only after I have enough items to scroll through after the View is Gone. – Boris Bankov Feb 05 '19 at 13:28
  • Yep, happy codding :) – ardiien Feb 05 '19 at 13:39
  • You may mark my answer with an arrow (accept), to notify people that suggested answer is solved the problem. – ardiien Feb 05 '19 at 15:16
0

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • 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. – Boris Bankov Feb 05 '19 at 13:34
  • You can try to look at [this post](https://stackoverflow.com/questions/14931510/android-gesture-detection-swipe-up-down-on-particular-view) to detect when your user swiped up/down – Tamir Abutbul Feb 05 '19 at 13:39