How to reset the Toolbar position controlled by the CoordinatorLayout?

后端 未结 8 1788
情话喂你
情话喂你 2020-12-04 16:19

The app I\'m working on consists of a Navigation Drawer which is implemented in an Activity. The activity layout is as follows:



        
8条回答
  •  死守一世寂寞
    2020-12-04 17:14

    To reset the scroll state, just get the AppBarLayout.Behavior object

    CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator);
    AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();       
    

    and call onNestedPreScroll method manually:

    int[] consumed = new int[2];
    behavior.onNestedPreScroll(coordinator, appbar, null, 0, -1000, consumed);
    

    If you would like to reset smoothly with an animation, you can try calling onNestedFling instead:

    behavior.onNestedFling(coordinator, appbar, null, 0, -1000, true);
    

提交回复
热议问题