Ohad
Ohad

Reputation: 1480

Change layout_scrollFlags programmatically in CollapsingToolbarLayout

I have the following CollapsingToolbarLayout tag in my xml:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsingToolbarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
    app:layout_scrollFlags="exitUntilCollapsed">

I would like to change the scrollFlags value programmatically (run time) - specifically, toggle the scrollable flag. Is it possible?

Upvotes: 18

Views: 13127

Answers (2)

OneKe
OneKe

Reputation: 137

    getSupportActionBar().setHideOnContentScrollEnabled(true);

Upvotes: 1

DmitryArc
DmitryArc

Reputation: 4837

CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); // list other flags here by |
collapsingToolbar.setLayoutParams(params);

One thing worth mentioning - setScrollFlags overrides all previous flags so we should be careful not to lose already existing flag

Upvotes: 43

Related Questions