Simple question, but I can't find an answer. How can I collapse or expand the CollapsingToolbarLayout
programmatically?
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Simple question, but I can't find an answer. How can I collapse or expand the CollapsingToolbarLayout
programmatically?
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Using Support Library v23, you can call appBarLayout.setExpanded(true/false)
.
Further reading: AppBarLayout.setExpanded(boolean)
AppBarLayout
. There's also a nested CoordinatorLayout
to deal with. –
Dispensatory I use this code for collapsing toolbar. Still cannot find a way to expand it.
public void collapseToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
}
}
Edit 1: The same function with negative velocityY but the toolbar is not expanded 100% and false for last param should work
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.onNestedFling(rootLayout, appbarLayout, null, 0, -10000, false);
}
}
Edit 2: This code do the trick for me
public void expandToolbar(){
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbarLayout.getLayoutParams();
behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null) {
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(rootLayout, appbarLayout, null, 0, 1, new int[2]);
}
}
Will try to implement Behavior by myself.
onNestedFling
to false
. –
Piccaninny onNestedFling
before set the fragment. After set the fragment, I could call the method successfully. –
Embosom com.android.support:design:27.0.0
–
Sememe You can define how much it expands or collapses with your custom animator.
Just use the setTopAndBottomOffset(int)
.
Here is an example:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
ValueAnimator valueAnimator = ValueAnimator.ofInt();
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
behavior.setTopAndBottomOffset((Integer) animation.getAnimatedValue());
appBar.requestLayout();
}
});
valueAnimator.setIntValues(0, -900);
valueAnimator.setDuration(400);
valueAnimator.start();
}
( -900 )
to set valueAnimator.setIntValues
The beter solution is that to use - ( appBar.getTotalScrollRange() )
–
Antidisestablishmentarianism 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.
Just use it instead of your AppBarLayout
, and you can call methods responsible for expanding or collapsing of the CollapsingToolbarLayout
.
It's working exactly as expected in my project, but you might need to tweak the fling/scroll values in the perform...
methods (especially in performExpandingWithAnimation()
) to fit perfectly with your CollapsibleToolbarLayout
.
setExpanded
method in the AppBarLayout
. –
Solvolysis 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)
;
Expand:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*200; // HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(true);
Collapse:
CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
params.height = 3*80; // HEIGHT
mAppBarLayout.setLayoutParams(params);
mAppBarLayout.setExpanded(false);
for the ones who wants to work with onNestedPreScroll and get error like me. i get NullPointerException in onCreate with out this line
CoordinatorLayout coordinator =(CoordinatorLayout)findViewById(R.id.tab_maincontent);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
//below line
params.setBehavior(new AppBarLayout.Behavior() {});
and doesn't work properly with this. but i work around this problem with
in onCreate :
scrollToolbarOnDelay();
and...
public void scrollToolbarOnDelay() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.tab_appbar);
CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.tab_maincontent);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if(behavior!=null)
behavior.onNestedPreScroll(coordinator, appBarLayout, null, 0, 100, new int[]{0, 0});
else
scrollToolbarOnDelay()
}
}, 100);
}
Try this...
Expand
appBarLayout.setExpanded(true, true);
To recall
appBarLayout.setExpanded(false, true);
To expand/collapse AppBarLayout programmatically:
fun expandAppBarLayout(expand: Boolean, isAnimationEnabled: Boolean){
appBarLayout.setExpanded(expand, isAnimationEnabled);
}
This may help to expand or collapse :
appBarLayout.setActivated(true);
appBarLayout.setExpanded(true, true);
i have using this
private fun collapseAppbar() {
scrollView.postDelayed(Runnable {
scrollView?.smoothScrollTo(50, 50)
}, 400)
}
Make sure your parent is a coordinator layout, otherwise setExpanded will not work.
© 2022 - 2024 — McMap. All rights reserved.
CollapsingToolbarLayout
cannot expand or collapse itself what's worst, other comments are making it seemed as if it's an issue that has to do with the version, In reality what you need to handle is its parent, which is funny becauseCollapsingToolbarLayout
CAN disable its own collapse/expansion, but for some reason it cannot trigger them. – Multiply