CollapsingToolbarLayout setting scroll flag ENTER_ALWAYS_COLLAPSED programmatically ignored

Viewed 78

I'm setting collapingToolbarLayout scroll flags programmatically & it works fine the firs time...

-First fragment: show an imageView in collapsingToolbar (flags: SCROLL_FLAG_SCROLL,SCROLL_FLAG_ENTER_ALWAYS, SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) WORKS FINE. then with a button go to second fragment.

-Second Fragment: remove the imageView (Height = 0dp) and change collapsingToolbar (flags: SCROLL_FLAG_SCROLL,SCROLL_FLAG_ENTER_ALWAYS) WORKS FINE. then onBackPressed go back to first fragment.

-First fragment: the flag "SCROLL_FLAG_ENTER_ALWAYS" don't work anymore, the collapsingToolbar always scroll to the bottom if i scroll down enough.

I'm making the changes in onDestinationChanged (NavController).

What i tried: -Setting the flags (SCROLL_FLAG_SCROLL,SCROLL_FLAG_ENTER_ALWAYS, SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) in XML and just change the imageView visibility state Visible/Gone, same problem.

Some code:

public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {

appBarLayout.setExpanded(true);

        if (destination.getId() == R.id.firstFragment) {
                toolbarChanges();
                collapsingToolbarChanges();
        } else if (destination.getId() == R.id.secondFragment) {
                toolbarChangesReset();
                collapsingToolbarChangesReset();
        }
}

private void toolbarChanges() {
        ViewGroup.LayoutParams toolbarImgParams = imgToolbarBackground.getLayoutParams();
        final float scale = getResources().getDisplayMetrics().density;
        int height = (int) (120 * scale + 0.5f);
        toolbarImgParams.height = height;
        imgToolbarBackground.setLayoutParams(toolbarImgParams);
    }

private void toolbarChangesReset() {
        ViewGroup.LayoutParams toolbarImgParams = imgToolbarBackground.getLayoutParams();
        toolbarImgParams.height = 0;
        imgToolbarBackground.setLayoutParams(toolbarImgParams);
    }

private void collapsingToolbarChanges() {
        AppBarLayout.LayoutParams collapsingToolbarPrams = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
        collapsingToolbarPrams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL |
                AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
                AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
        collapsingToolbar.setLayoutParams(collapsingToolbarPrams);
    }

    private void collapsingToolbarChangesReset() {
        AppBarLayout.LayoutParams collapsingToolbarPrams = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
        collapsingToolbarPrams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL |
                AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        collapsingToolbar.setLayoutParams(collapsingToolbarPrams);
    }
0 Answers
Related