Prevent CollapsingToolbarLayout collapse if not needed

前端 未结 6 1914
情歌与酒
情歌与酒 2020-12-05 02:30

Using:

compile \'com.android.support:design:23.0.0\'
compile \'com.android.support:appcompat-v7:23.0.0\'
compile \'com.android.support:cardview-v7:23.0.0\'
c         


        
相关标签:
6条回答
  • 2020-12-05 03:03
      AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) activityUserGroupProfleBinding.collapsingToolbarLayout.getLayoutParams();
            if (logic) {
                params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
            } else {
                params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
            }
            activityUserGroupProfleBinding.collapsingToolbarLayout.setLayoutParams(params);
    
    0 讨论(0)
  • 2020-12-05 03:04

    You can use below code for this:

       public static void stopScroll() {
        AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
        toolbarLayoutParams.setScrollFlags(0);
        collapsing_toolbar.setLayoutParams(toolbarLayoutParams);
    
        CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
        appBarLayoutParams.setBehavior(null);
        appbar.setLayoutParams(appBarLayoutParams);
    }
    
    public static void startScroll() {
        AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) collapsing_toolbar.getLayoutParams();
        toolbarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        collapsing_toolbar.setLayoutParams(toolbarLayoutParams);
    
        CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
        appBarLayoutParams.setBehavior(new AppBarLayout.Behavior());
        appbar.setLayoutParams(appBarLayoutParams);
    }
    
    0 讨论(0)
  • 2020-12-05 03:07

    Here is my working code, to initially collapes the bar:

    _appbar.setExpanded(false);

       AppBarLayout _appbar = (AppBarLayout) findViewById(R.id.appbar);
        _appbar.setExpanded(false);
    

    here is the layout xml

     <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">
    
    
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    
                app:layout_collapseMode="pin" />
    
    
    
    
    
        </android.support.design.widget.CollapsingToolbarLayout>
    
    </android.support.design.widget.AppBarLayout>
    

    the reference is: AppBarLayout.setExpanded(boolean)

    0 讨论(0)
  • 2020-12-05 03:13

    To implement such behaviour in Cheesesquare example just modify android:layout_height param of the NestedScrollView to wrap_content. It will prevent scrolling by content if it is small enough to fit on the screen.

    And to prevent scrolling by CollapsingToolbarLayout you should programmatically set layout_scrollFlags parameter to the AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP value.

    Here described how you can do this.

    0 讨论(0)
  • 2020-12-05 03:13

    A data-binding solution inspired by @Vishal's answer

        <com.google.android.material.appbar.AppBarLayout>
            <com.google.android.material.appbar.CollapsingToolbarLayout
                app:enableCollapsingScroll="@{listItems.size > 0}"
                ... />
        </com.google.android.material.appbar.AppBarLayout>
    
        @BindingAdapter("app:enableCollapsingScroll")
        fun setCollapsingToolbarLayoutScrollEnabled(collapsingToolbarLayout: CollapsingToolbarLayout, enabled: Boolean?) {
            val lp = collapsingToolbarLayout.layoutParams as AppBarLayout.LayoutParams
            if (enabled.orFalse()) {
                lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
            } else {
                lp.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP
            }
            collapsingToolbarLayout.layoutParams = lp
        }
    
    0 讨论(0)
  • 2020-12-05 03:16

    In xml I have used property

    app:layout_scrollFlags="snap" in <android.support.design.widget.CollapsingToolbarLayout

    and following in the activity

     toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     toolbar.setTitle(null);
     toolbar.setCollapsible(false);
    

    It is working now.

    0 讨论(0)
提交回复
热议问题