Prevent CollapsingToolbarLayout collapse if not ne

2019-01-17 11:21发布

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'
compile 'com.android.support:recyclerview-v7:23.0.0'

With the project Cheesesquare updated.

Into the detail of cheese, I remove 2 cards (to have only one). Is there a way to prevent the collapsing of the toolbar that show a blank space?

enter image description here

4条回答
小情绪 Triste *
2楼-- · 2019-01-17 11:32

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-17 11:34

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)

查看更多
爷、活的狠高调
4楼-- · 2019-01-17 11:49

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);
}
查看更多
看我几分像从前
5楼-- · 2019-01-17 11:53

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.

查看更多
登录 后发表回答