Changing Toolbar and CollapsingToolbarLayout scroll flags programmatically

房东的猫 提交于 2020-04-09 08:42:51

问题


I have a single Activity android app with lots of fragments. When I'm showing a list screen I want to use the Toolbar with the, app:layout_scrollFlags="scroll|enterAlways" property. And in the detail fragments I want to use the CollapsingToolbarLayout with an image in it. Since it's a single Activity app, I have only one Toolbar. Is it possible to modify my layout programmatically to suit both cases?


回答1:


Yes. Let's say you are going from the CollapsingToolbarLayout fragment to the Toolbar one.

  1. You collapse your AppBarLayout using AppBarLayout.setExpanded(false);

  2. You change the scroll flags to fit your needs.

    AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    p.setScrollFlags(...);
    toolbar.setLayoutParams(p);
    

    Same goes for the CollapsingToolbarLayout if necessary. I guess it should be something like:

    collapsingToolbarParams.setScrollFlags(0); //no flags for ctl
    toolbarParams.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS); //new flags for toolbar
    



回答2:


I found it working

public void disableToolBarScrolling() {
    CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(0);
}

public void enableToolBarScrolling() {
    CollapsingToolbarLayout toolbar = findViewById(R.id.collap_toolbar);
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(SCROLL_FLAG_SCROLL | SCROLL_FLAG_ENTER_ALWAYS);
}


来源:https://stackoverflow.com/questions/32418625/changing-toolbar-and-collapsingtoolbarlayout-scroll-flags-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!