How to avoid blocking of scrolling itself when using setNestedScrollingEnabled(false)?

后端 未结 8 1835
盖世英雄少女心
盖世英雄少女心 2020-12-30 01:07

Background

We have quite a complex layout that has CollapsingToolbarLayout in it, together with a RecyclerView at the bottom.

In certain cases, we tempora

8条回答
  •  天命终不由人
    2020-12-30 02:06

    Actually, you might be looking at the problem in the wrong way.

    The only thing you need is to set the Toolbar flags accordingly. You don't really anything else so I would say that your layout should be simplified to:

    
    
        
    
            
    
        
    
        
    
        
    
            

    Then when you wish to disable the collapsing just set your toolbar flags:

    // To disable collapsing
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
    toolbar.setLayoutParams(params);
    

    And to enable

    // To enable collapsing
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL|AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
    toolbar.setLayoutParams(params);
    

    Hold a reference to the layout params if you are changing instead of getting it all the time.

    If you need to have the CollapsingToolbarLayout get from and set the LayoutParams to that View instead, update the flags the same way but now adding the appBarLayout.setExpanded(true/false)

    Note: Using the setScrollFlags clears all previous flags, so be careful and set all required flags when using this method.

提交回复
热议问题