Need to Disable Expand on Collapsingtoolbarlayout for Certain Fragments

Need to disable expand on CollapsingToolbarLayout for certain fragments

Now, in v23 of support library you can easily control your appbar visibility.

Just get a reference to your AppBarLayout and hide/show it depending on the fragment you want to load:

private AppBarLayout appBarLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
[...]
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
[...]
}

public void switchToFragment(Fragment fragment, String tag, boolean expandToolbar){
FragmentManager fragmentManager = getSupportFragmentManager();

Fragment currentFragment = fragmentManager.findFragmentByTag(currentFragmentTag);

if(currentFragment == null || !TextUtils.equals(tag, currentFragmentTag) ){
currentFragmentTag = tag;
fragmentManager
.beginTransaction()
.replace(R.id.flContent, fragment, currentFragmentTag)
.commit();

if(expandToolbar){
appBarLayout.setExpanded(true,true);
}else{
appBarLayout.setExpanded(false,true);
}
}
}

P.S. don't forget to add the required dependencies in your build.gradle:

dependencies {  
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
}

EDIT:
If you also want to lock your toolbar in certain fragments (apart from collapsing) you have to resort to workarounds as this feature is not provided by CollapsingToolbarLayout until now (v23.2.1 of support design). Here you can find my proposed workaround.

Prevent collapsingToolbar from expanding for certain fragments

I was able to resolve this problem using the following (as shown here by @DanielPersson):

collapsingToolbarLayout.setTitleEnabled(false);  
toolbar.setTitle(title);

How to disable expand from AppBarLayout in fragment that has a NestedScrollView?

I've faced with this problem not so far, and resloved it by changing AppBarLayout height when I need it be collapsable or expandable. So, firstly, you need to define the next two items in your default dimens.xml file:

<dimen name="toolbar_height">56dp</dimen>
<dimen name="toolbar_expanded_height">256dp</dimen> // this can be whatever you need

And then the complete method to prevent AppBarLayout and Toolbar from expanding/collapsing is:

public void disableCollapse() {
appbar.setActivated(false);
//you will need to hide also all content inside CollapsingToolbarLayout
//plus you will need to hide title of it
backdrop.setVisibility(View.GONE);
shadow.setVisibility(View.GONE);
collapsingToolbar.setTitleEnabled(false);

AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
p.setScrollFlags(0);
collapsingToolbar.setLayoutParams(p);
collapsingToolbar.setActivated(false);

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
lp.height = getResources().getDimensionPixelSize(R.dimen.toolbar_height);
appbar.requestLayout();

//you also have to setTitle for toolbar
toolbar.setTitle(title); // or getSupportActionBar().setTitle(title);
}

You may also need to add status bar height to toolbar's height, if you use fitsSystemWindows=true for example, so then you need to change

lp.height = getResources().getDimensionPixelSize(R.dimen.toolbar_height);

to

lp.height = getResources().getDimensionPixelSize(R.dimen.toolbar_height) + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getStatusBarHeight() : 0);

And getStatusBarHeight() method implementation is:

protected int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

And, finally, if you want to make your AppBarLayout and Toolbar collapsable/expandable again, you have to use next method:

public void enableCollapse() {
appbar.setActivated(true);
collapsingToolbar.setActivated(true);

//you will need to show now all content inside CollapsingToolbarLayout
//plus you will need to show title of it
backdrop.setVisibility(View.VISIBLE);
shadow.setVisibility(View.VISIBLE);
collapsingToolbar.setTitleEnabled(true);

AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
p.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbar.setLayoutParams(p);

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
lp.height = getResources().getDimensionPixelSize(R.dimen.toolbar_expanded_height);
appbar.requestLayout();

//you also have to setTitle for toolbar
toolbar.setTitle(title); // or getSupportActionBar().setTitle(title);
}

Hope that helps!

AppBarLayout CollapsingToolbar how to disable expand on EditText click?

I have find the answer. I do not know is the best solution, but it works nice. If someone has better solution, feel free to write it.

So, what i done is i put the whole AppBarLayout params to 0 when i click EditText or when EditText get focus.
So, when EditText is clicked, AppBarLayout is still there but, now his height is 0 and it is not visible.

This do the trick:

 //Collaps AppBarLayout    public void collapsAppBarLayout(){        CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();        params.height = 0; // setting new param height to 0        //setting params to appbarLayout (now AppBarLayout is 0)        appBarLayout.setLayoutParams(params);        appBarLayout.setExpanded(false);    }

How to disable CollapsingToolbar's collapse when scroll has not content?

get your appbarlayout reference and set the setScrollFlags.

AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
p.setScrollFlags(0);
toolbar.setLayoutParams(p);

How to lock CollapsingToolbarLayout from Android support library

Well, I managed to solve it myself. The trick is to disable nested scrolling behaviour with ViewCompat.setNestedScrollingEnabled(recyclerView, expanded);

As I am using one fragment in the activity as a content view and putting it on the backstack I simply check when backstack has changed and which fragment is visibile. Note that I NestedScrollView in every fragment to trigger collapsible toolbar. This is my code:

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
NestedScrollView nestedScrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
int size = getSupportFragmentManager().getBackStackEntryCount();
if (size >= 1 && nestedScrollView != null) {
if (getSupportFragmentManager().getBackStackEntryAt(size - 1).getName().equals("SpotDetailsFragment")) {
Log.d(LOG_TAG, "Enabling collapsible toolbar.");
ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
} else {
Log.d(LOG_TAG, "Disabling collapsible toolbar.");
ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
}
}
}
});

This thread helped me a lot, where another possible solution is presented:
Need to disable expand on CollapsingToolbarLayout for certain fragments



Related Topics



Leave a reply



Submit