Push out/pull in ActionBar when ListView is scroll

2019-08-02 17:30发布

问题:

The latest Google Newsstand App features an ActionBar and ViewPager tab bar which slowly eases out the top of the screen when the list below it is scrolled down. Importantly, it eases out at the same speed the list is scrolled. When the list is scrolled upwards the ActionBar eases back onto the screen, again at the same speed the list is scrolled.

I am not referring to the ActionBar hide() and show() methods, instead I want to know how to hide and show the ActionBar at the same rate the list below it is scrolled.

How is this achieved? All attempts at finding a documented solution have proven unsuccessful so any help would be appreciated.

回答1:

Short Answer

Create a Fragment subclass that serves as an ActionBar replacement. Use an OnScrollListener to update the fragment's view's translationY after scroll events.

Long Answer

After digging around the ActionBar source code, it seems that there is no public method for accessing the ActionBar container view. But where there's a will, there's a way.

You can grab a reference to the Action Bar's view using the following:

int resId = getResources().getIdentifier("action_bar_container", "id", "android");
View actionBarContainer = findViewById(resId);

Disclaimer: action_bar_container is a private ID, so it's subject to Google's whims. Use at your own risk.

Once you have the Action Bar's view, you can animate it however you'd like.

I hacked together a semi-functional example if you're interested.

Hope this helps!