There is a method in which the header appears or disappears depending on the scrolling. But since my list changes periodically (number changes), if there are 2-3 items in the list, I don’t want the header to disappear, but alas, it’s impossible to complete it. Who can prompts what efficient? Method:

public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); firstVisiblePosition = recyclerLayoutManager.findFirstVisibleItemPosition(); if (lastScrollState == RecyclerView.SCROLL_STATE_DRAGGING && scrollDirection == ScrollDirection.SCROLL_DOWN) { hideHeader(); } else if (lastScrollState == RecyclerView.SCROLL_STATE_DRAGGING && scrollDirection == ScrollDirection.SCROLL_UP && firstVisiblePosition < 10) { showHeader(); } lastScrollState = newState; } 

    1 answer 1

    I would advise you to initially define your recyclerView . If I understand correctly, from the count of elements in recyclerView you display or hide the header .

    Accordingly, initially try to calculate the following values ​​in your hideHeader method: the height of the list item, the height of the view and the number of items in the list.

    Depending on these parameters, you can already play with the conditions and display the elements you need.

    You can get the following values ​​approximately as follows:

      int itemHeight = recyclerView.getChildAt(0).getMeasuredHeight(); int fragmentHeight = getView().getMeasuredHeight(); int count = adapter.getItemCount(); 

    Well, the condition itself based on the values ​​obtained will be approximately as follows for hideHeader:

     fragmentHeight > count * itemHeight 

    UPD:

    In order not to knock out, try to add the following condition in your if and else:

     && adapter != null 

    And I think in your project the header does not immediately hide itself. firstVisiblePosition is the logical notation for the first element. Try something like:

     adapter.getItemCount() > 10 

    Well, in general, because First of all, you will use your application on the tablets as well, I would advise you to calculate how many elements you have on the screen of the widest resolution of the tablet elements and set this figure, because I think that the header will probably jump.

    • Still knocks out, but catch the error does not work. Once at a time + header is not immediately hidden, only with a certain amount of scrolling. - Inkognito