12

Ok, so hiding the action bar is something doable. But, how can we hide the (newly introduced) toolbar in our activity?

I am making an app with an activity having theme as theme.apcompat.light.noactionbar(to hide the action bar) , I have placed a toolbar with slidingtablayout below it. And below it is my listview.

What I want is to hide the toolbar when I scroll the listview up. But the slidingtablayout should remain there. And while in middle of the listview if I scroll down, the toolbar should again be visible.

zavione
  • 919
  • 1
  • 7
  • 14

3 Answers3

8

Updated (8/24/2015):

Please see my latest answer here using the Design Support Library:

Hiding the ActionBar on RecyclerView/ListView onScroll

Updated (6/2/2015):

ListView + ToolBar - without Libraries:

Please see https://stackoverflow.com/a/26547550/950427 and https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java.

mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= 16) {
            mToolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            mToolbar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
        mToolbar.animate().translationY(-mToolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
    }
});

From: http://pastebin.com/yeMX3VYP

ListView + ActionBar - without Libraries:

https://stackoverflow.com/a/17767691/950427

I recently wanted the same functionality and this works perfectly for me:

As the user scrolls upward, the ActionBar will be hidden in order to give the user the entire screen to work work with.

As the user scrolls downward and lets go, the ActionBar will return.

getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

listView.setOnScrollListener(new OnScrollListener() {
    int mLastFirstVisibleItem = 0;

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {   }           

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {    
        if (view.getId() == listView.getId()) {
            final int currentFirstVisibleItem = listView.getFirstVisiblePosition();

            if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                // getSherlockActivity().getSupportActionBar().hide();
                getSupportActionBar().hide();
            } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                // getSherlockActivity().getSupportActionBar().show();
                getSupportActionBar().show();
            }

            mLastFirstVisibleItem = currentFirstVisibleItem;
        }               
    }
});
Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
5

Use the following code:

ViewCompat.setNestedScrollingEnabled(listView, true);
DareDeviL
  • 327
  • 3
  • 16
2

for recyclerView i managed it to make it like this:

public class FragmentsOnScrollListener extends RecyclerView.OnScrollListener {

    private final MainActivity mainActivity;
    private final View bothToolbarLayouts;
    private int approxToolbarHeight;

    public FragmentsOnScrollListener(MainActivity activity) {
        mainActivity = activity;
        bothToolbarLayouts = mainActivity.getBothToolbarLayouts();
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        approxToolbarHeight += dy;
        bothToolbarLayouts.animate().translationY(-approxToolbarHeight).setInterpolator(new AccelerateInterpolator()).start();
    }
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
cV2
  • 5,229
  • 3
  • 43
  • 53