I have ImageView and TabLayout(4 Tabs) inside CollapsingToolbarLayout, Now i want to collapse Appbar when clicking on Tabs(2,3,4) and for first tab it should work normally(as per scrolling). Is there a way to expand and collapse Appbar programmatically?
however i have seen solution, appBarLayout.setExpanded(false) collapses Appbar but again it is able to drag down. i want to prevent AppBar Expansion until Tab 1 is clicked?
CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout.
Since the v23 of Support (proceeding to AndroidX), there is setExpanded method in the AppBarLayout. Use mAppBarLayout.setExpanded (true) to expand Toolbar and use mAppBarLayout.setExpanded (false) to collapse Toolbar.
I've written a small extension to AppBarLayout. It allows for expanding and collapsing of the CollapsibleToolbarLayout both with and without animation. It seems to be doing it quite right. Feel free to try it out.
Since the v23 of Support (proceeding to AndroidX), there is setExpanded method in the AppBarLayout. Use mAppBarLayout.setExpanded (true) to expand Toolbar and use mAppBarLayout.setExpanded (false) to collapse Toolbar. If you want to change CollapsingToolbarLayout height programmatically then just use mAppBarLayout.setLayoutParams (params);
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 changeCollapsingToolbarLayout
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~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With