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

I have two problems:

1) I was able to prevent the "expand" function when I don't have a NestedScrollView inside my fragment, but when I do this, it continues to expand, even using:

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

Is there a way to prevent the toolbar from expanding when I have a NestedScrollView inside a fragment?

2) Even if I don’t have a NestedScrollView, I can still expand it when I touch my finger on the toolbar and scroll down and up. Extension and crash is still working.

How can I turn off the collapse action and expand the toolbar when I touch my finger in it and scroll down and up?

Thank.

Edit1 (Additional Information):

This is the code of my fragment inside FrameLayout.

<android.support.v4.widget.NestedScrollView>

    <LinearLayout>

        <TextView />

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

    </LinearLayout>

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

:

<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>

2: : , NestedScrollView ?

+4
3

, AppBarLayout height, , . , -, dimens.xml :

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

AppBarLayout Toolbar /:

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);
}

, , fitsSystemWindows=true, ,

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

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

getStatusBarHeight():

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

, , / AppBarLayout Toolbar, :

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);
}

, !

+9

AppBarLayout, DragCallback:

AppBarLayout layout = ...;
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) layout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
    @Override
    public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
        return false;
    }
});

AppBarLayout - NestedScrollView, NestedScrollView:

NestedScrollView nestedScrollView = ...;
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) nestedScrollView.getLayoutParams();
params.setBehavior(null);

a >

, - AppBarLayout:

AppBarLayout layout = ...;
View child = layout.getChildAt(0);
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) child.getLayoutParams();
params.setScrollFlags(0);
+4

in your specific fragment the onViewCreated add method

 private void disableCollapsing() {
    AppBarLayout layout = ((HomeActivity) getActivity()).appBarLayout;
    View child = layout.getChildAt(0);
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) child.getLayoutParams();
    params.setScrollFlags(0);
}

and reActive falls apart when destroyed

private void enableCollapsing() {
    AppBarLayout layout = ((HomeActivity) getActivity()).appBarLayout;
    View child = layout.getChildAt(0);
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) child.getLayoutParams();
    params.setScrollFlags(1);
}
0
source

Source: https://habr.com/ru/post/1660968/


All Articles