How to reset the Toolbar position controlled by the CoordinatorLayout?

后端 未结 8 1704
情话喂你
情话喂你 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 16:56

    I'm using this codes before fragment changes.

    scrollingElement.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
    scrollingElement.dispatchNestedPreScroll(0, -Integer.MAX_VALUE, null, null);
    scrollingElement.stopNestedScroll();
    
    0 讨论(0)
  • 2020-12-04 17:01

    @razzledazzle The AppBarLayout stores onOffsetChangedListeners as WeakReferences, which means they are garbage collected when needed, for instance when you do a intense fling. See solution here:

    https://code.google.com/p/android/issues/detail?id=176328

    0 讨论(0)
  • 2020-12-04 17:07

    Update your support lib to v23 then you can use:

    appBarLayout.setExpanded(true/false);
    

    public void setExpanded (boolean expanded)

    Sets whether this AppBarLayout is expanded or not, animating if it has already been laid out.

    As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout.

    expanded true if the layout should be fully expanded, false if it should be fully collapsed

    0 讨论(0)
  • 2020-12-04 17:08
    // Get the reference for appbar from layout
    AppBarLayout appbar = rootView.findViewById(R.id.appbar);
    //boolean expanded, boolean animate
    appbar.setExpanded(true, true);
    
    0 讨论(0)
  • 2020-12-04 17:09

    Replace or Wrap the FrameLayout inside the android.support.design.widget.CoordinatorLayout with a android.support.v4.widget.NestedScrollView and that will do the job automatically without having to any other hacks....so it would look like this:

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v4.widget.NestedScrollView
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <include
            android:id="@+id/appbar"
            layout="@layout/appbar" />
    
    </android.support.design.widget.CoordinatorLayout>
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题