I am trying to implement the same behavior, here is the main feature of the code that shows and hides the toolbar (placed in any class containing your RecyclerView):
int toolbarMarginOffset = 0 private int dp(int inPixels){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, inPixels, getApplicationContext().getResources().getDisplayMetrics()); } public RecyclerView.OnScrollListener onScrollListenerToolbarHide = new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); toolbarMarginOffset += dy; if(toolbarMarginOffset>dp(48)){ toolbarMarginOffset = dp(48); } if(toolbarMarginOffset<0){ toolbarMarginOffset = 0; } ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)toolbar.getLayoutParams(); params.topMargin = -1*toolbarMarginOffset; toolbar.setLayoutParams(params); } };
I turned on the dp function to convert from pixels to dp, but obviously set it depending on the height of your toolbar. (replace dp (48) with your toolbar height)
Wherever you set up RecyclerView, enable this:
yourListView.setOnScrollListener(onScrollListenerToolbarHide);
However, there are a couple more additional issues if you are also using SwipeRefreshLayout.
I had to set the marginTop of the first element in the adapter for the RecyclerView to the height of the toolbar plus the original offset. (A bit about the hack I know). The reason for this is because I found that if I changed my code above by enabling marginTop in recyclerView when scrolling, it was inconvenient. So, how I overcame it. So, basically tweak your layout so that the toolbar floats on top of the RecyclerView (crop it). Something like this (in the onBindViewHolder of your RecyclerView custom adapter):
if(position==0){ ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)holder.card.getLayoutParams();
And finally, since there is a large bias, the RecyclerViews update circle will be cropped, so you will need to compensate for it (back to the onCreate of your class containing RecyclerView):
swipeLayout.setProgressViewOffset(true,dp(48),dp(96));
Hope this helps someone. His first detailed answer, so I hope I was detailed enough.