简体   繁体   中英

Is it possible to hide the Toolbar and disable the scroll when swiping to specific page?

I would like to be able to hide the Toolbar on swipe to a specific tab and then lock it. Expanding it and then locking would also work for me although i prefer the first.

I have tried doing stuff like in the code below, but it gives rise to some snappy behavior because the page is scrolled of the screen originally. As soon as i set the scroll flags to 0, the whole page snaps back up and then locks the screen with the toolbar expanded which makes sense because with scroll flags set to zero, the page should not be able to scroll off the screen at all so it just snaps back up.

The page I am tending to is a chat page and in order to have a static text input bar at the bottom I really need to disable the scrolling for that page while making it possible for the others.

Can you guys think of some way to accomplish this?

Activity:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group);
    appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // add all the needed fragments for the tabs inside a Vector
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOffscreenPageLimit(4);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    viewPager.clearOnPageChangeListeners();
    viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListenerGroups(tabLayout, this, fab, appBarLayout));

    viewPager.setCurrentItem(CURRENT_TAB);
}

public void disableToolbarScrolling()
{
    logger.d("disabling scrolling toolbar");
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(0);  // clear all scroll flags
    toolbar.setLayoutParams(params);
}

public void enableToolbarScrolling()
{
    logger.d("enabling scrolling toolbar");
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);  // clear all scroll flags
    toolbar.setLayoutParams(params);
}

And then using a PageChangeListener to handle the event:

public TabLayoutOnPageChangeListenerGroups(TabLayout tabLayout, Activity activity, FloatingActionsMenu fab, AppBarLayout appBarLayout)
{
    ...
    this.appBarLayout = appBarLayout;
    ...
}

private void enableScrolling()
{
    ((GroupActivity) mActivity).enableToolbarScrolling();
    toolbarState = toolbarState_enabled;
}

private void disableScrolling()
{
    ((GroupActivity) mActivity).disableToolbarScrolling();
    toolbarState = toolbarState_disabled;
}

@Override
public void onPageSelected(int position)
{
    if(position == 0)
    {
        logger.d("pos 0");
        disableScrolling();
        appBarLayout.setExpanded(false);
    }
    else
    {
        logger.d("pos != 0");
        enableScrolling();
    }
}

EDIT 1:

I have tried setting toolbar visibility to both GONE or INVISIBLE. But they only make the toolbar white, giving a white bar at the top of the screen. Still allowing for the scrolling behaviour.

add this line to your code in onCreate setSupportActionBar(toolbar); and then add toolbar.setVisibility(View.GONE);

要隐藏工具栏,您可以尝试:

getSupportActionBar().hide();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM