Reverse AppBarLayout.ScrollingViewBehavior

Viewed 449

I have a ToolBar and Recyclerview wrapped in a Coordinatorlayout. The RecyclerView is populated with messages from our api and are stacked from the bottom. (Like a chat application)

 //Set the layout manager
 LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
 layoutManager.setStackFromEnd(true);
 layoutManager.setReverseLayout(true);
 binding.listMessages.setLayoutManager(layoutManager);

I'd like to define a custom ReverseScrollBehavior by overriding the AppBarLayout.ScrollingViewBehavior to inverse it's behavior. So when I scroll up, the toolbar hides and when I scroll back down (to the bottom) the toolbar shows.

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/list_messages"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    app:stackFromEnd="true"
    app:reverseLayout="true"
    app:layout_behavior="com.rootsoft.kot.utils.ReverseScrollBehavior" />

I'd like to use the ScrollingViewBehavior for this rather then overriding the onTouch

UPDATE

What I'm trying to achieve is to get the inverse of the current appbar_scrolling_view_behavior. Imagine having a chat application where the newest messages are starting from the bottom. When you scroll up, I want to hide the toolbar. The current situation with the AppBarLayout is when you scroll up, the toolbar is shown and when you scroll back down, it hides. My scrollflags are: app:layout_scrollFlags="scroll|enterAlways|snap"

1 Answers

You need to create a method that will get the height of the screen and scroll events (up and down.) You can also toggle the visibility in the method. This means you will set visibility to visible in your XML and do something like:

    ChatButton Button;

    ChatButtton.setVisibility = View.GONE 
    ChatButton.setVisibility = View.VISIBLE

to handle the toggle of the visibility based on the scroll events

Related