Programmatically Collapse or Expand Collapsingtoolbarlayout

Programmatically collapse or expand CollapsingToolbarLayout

Using Support Library v23, you can call appBarLayout.setExpanded(true/false).

Further reading: AppBarLayout.setExpanded(boolean)

Android: Programmatically Collapse and expand the CollapsingToolbarLayout

Use mAppBarLayout.setExpanded(true) to expand Toolbar and use mAppBarLayout.setExpanded(false) to collapse Toolbar.

If you want to prevent CollapsingToolbarLayout expansion until
Tab 1 is clicked then you should use mAppBarLayout.setLayoutParams(params) programmatically to change
CollapsingToolbarLayout height.

Collapse: Use when Tabs(2,3,4) clicked

CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // COLLAPSED_HEIGHT

mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);

Expand: Use when Tab 1 clicked

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // EXPANDED_HEIGHT

mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);

Hope this will help you~

CollapsingToolbarLayout expand programmatically animation duration

Note: this answer is based on android design library v25.0.0.

You can call the private method animateOffsetTo of the AppBarLayout.Behavior of your NestedScrollView with reflection. This method has a velocity parameter that has an impact on the animation duration.

private void expandAppBarLayoutWithVelocity(AppBarLayout.Behavior behavior, CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, float velocity) {
try {
//With reflection, we can call the private method of Behavior that expands the AppBarLayout with specified velocity
Method animateOffsetTo = AppBarLayout.Behavior.getClass().getDeclaredMethod("animateOffsetTo", CoordinatorLayout.class, AppBarLayout.class, int.class, float.class);
animateOffsetTo.setAccessible(true);
animateOffsetTo.invoke(behavior, coordinatorLayout, appBarLayout, 0, velocity);
} catch (Exception e) {
e.printStackTrace();
//If the reflection fails, we fall back to the public method setExpanded that expands the AppBarLayout with a fixed velocity
Log.e(TAG, "Failed to get animateOffsetTo method from AppBarLayout.Behavior through reflection. Falling back to setExpanded.");
appBarLayout.setExpanded(true, true);
}
}

To get the Behavior, you need to fetch it from the LayoutParams of your AppBarLayout.

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

How to lock expanding/collapsing action programmatically?

I assume your list is using a RecyclerView.

You can turn off nested scrolling on your RecyclerView which will disable the toolbar collapse:

recyclerView.setNestedScrollingEnabled(false);

You can lock the toolbar by clearing the scroll flags:

CollapsingToolbarLayout toolbar = findViewById(R.id.collapsingToolbar);  // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0); // clear all scroll flags

CollapsingToolbarLayout expand only when at the top

I ended up moving the orange bar out of the CollapsingToolbarLayout and setting an OnOffsetChangedListener that changes the translationY of the top bar on the AppBarLayout.

Setting the OnOffsetChangedListener:

app_bar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appbar, offset ->

topbar.translationY = Math.min(image.height.toFloat(), - offset.toFloat())

})

Layout:

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="@+id/topbar"
app:elevation="8dp"
android:elevation="8dp"
android:background="#ff8000"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Top bar"
android:textColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways" />

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

<TextView
android:layout_width="match_parent"
android:layout_height="145dp"
android:background="#444"
android:gravity="center"
android:text="ImageView"
android:textColor="@android:color/white"
android:textSize="20sp"
app:layout_collapseMode="parallax" />

</android.support.design.widget.CollapsingToolbarLayout>

<TextView
android:id="@+id/bottombar"
android:layout_width="match_parent"
android:layout_height="50dp"
app:elevation="8dp"
android:elevation="8dp"
android:background="#ddd"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Bottom bar"
android:textColor="@android:color/black"
app:layout_scrollFlags="enterAlways" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<include layout="@layout/content_scrolling"/>

</android.support.v4.widget.NestedScrollView>



Related Topics



Leave a reply



Submit