Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two problems:

1) I was able to prevent the "expand" feature when i have no NestedScrollView inside my fragment, but when i do, it keeps expanding, even using:

appBarLayout.setExpanded(false);
appBarLayout.setActivated(false);

There's any way to prevent the expanding of toolbar when i have a NestedScrollView inside a fragment?

2) Even if i don't have a NestedScrollView, i'm still able to expand it when i touch my finger in the toolbar, and scroll down and up. The "expand & collapse" still works.

How can i disable the action of collapse and expand the toolbar when i touch my finger in it and scroll down and up?

Thanks.

Edit1 (More info):

This is the code of my fragment, inside of the FrameLayout.

<android.support.v4.widget.NestedScrollView>

    <LinearLayout>

        <TextView />

        <android.support.v7.widget.RecyclerView />

    </LinearLayout>

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

And this is my activity structure:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.CoordinatorLayout>

        <android.support.design.widget.AppBarLayout>

            <android.support.design.widget.CollapsingToolbarLayout>

                <android.support.v7.widget.Toolbar />

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

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

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    ...

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

Edit 2: Th only question left now is: There's any way to prevent the expanding of toolbar when i have a NestedScrollView inside a fragment?

like image 420
Otuyh Avatar asked Dec 03 '22 14:12

Otuyh


1 Answers

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!

like image 162
romtsn Avatar answered Dec 06 '22 12:12

romtsn