CollapsingToolbarLayout disable draw down expansion

不打扰是莪最后的温柔 提交于 2019-12-12 16:07:57

问题


I have a CollapsingToolbar that I have conditionally disabled. When the user loads the view under that condition, it just looks like a normal ToolBar object.

The only weird thing is that if they drag down, such as in a pull to refresh style action, the CollapsingToolbar expands, despite my wishes and code to the contrary!

Here is what I have, and the commented out code reflects what I have also tried

    appBar.setExpanded(false);
    appBar.setActivated(false);
    /*CollapsingToolbarLayout.LayoutParams p = (CollapsingToolbarLayout.LayoutParams)toolbar.getLayoutParams();
    p.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PIN);
    toolbar.setLayoutParams(p);

    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
    appBarLayoutParams.setBehavior(null);
    appBar.setLayoutParams(appBarLayoutParams);*/
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBar.getLayoutParams();
    lp.height = (int) getResources().getDimension(R.dimen.app_bar_height);

I want to disable the drag down expansion, and I didn't see a way to do it. This activity contains a recyclerview and that is what users primarily interact with.


回答1:


I was closely with method. Expand layout behavior controls AppBarLayout. So you need update expand methods of that widget. Based on AppBarLayout Doc.

AppBarLayout appBarLayout;

//....

public void setExpandToolbar (boolean isEpand) {
    if (isExpand) {
       appBarLayout.setExpanded(true,true);
    } 
    else {
       appBarLayout.setExpanded(false,true);
    }
}

Let me know, about this issue. Because you can control expand via other solution. For example, by custom layout params.

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



回答2:


To prevent scrolling of RecyclerView or NestedScrollView from expanding or collapsing the CollapsingToolbarLayout.

// scrollView can be RecyclerView or NestedScrollView
ViewCompat.setNestedScrollingEnabled(scrollView, false)

https://code.luasoftware.com/tutorials/android/how-to-disable-or-lock-collapsingtoolbarlayout-collapse-or-expand/



来源:https://stackoverflow.com/questions/38469579/collapsingtoolbarlayout-disable-draw-down-expansion

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