20

I have a bottom sheet with a NestedScrollView inside (see below). When I press on a FAB button, I want to make some parts in this NestedScrollView invisible. But when I change some linearlayouts visibilities to GONE, bottomsheet fly aways from the top. See here:

enter image description here

You can get the whole code from https://github.com/Tanrikut/BottomSheetExample

My change visibility method:

private void changeVisibility() {
    subtitleLayout.setVisibility(View.GONE);

    coordinateLayout.setVisibility(View.GONE);
    timeLayout.setVisibility(View.GONE);

}

My NestedScrollView xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:behavior_peekHeight="120dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:id="@+id/bottom_sheet_main">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:background="@android:color/white"
            android:animateLayoutChanges="true"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="10dp"
                android:paddingStart="10dp"
                android:paddingTop="@dimen/activity_horizontal_margin">

                <TextView
                    style="@style/TextAppearance.AppCompat.Headline"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Dandelion Chocolate"
                    android:id="@+id/title" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/activity_horizontal_margin"
                    android:layout_marginTop="16dp"
                    android:orientation="horizontal"
                    android:id="@+id/subtitleLayout">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/subtitle"
                        android:text="Subtitle" />

                </LinearLayout>


            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="@dimen/activity_horizontal_margin"
                android:id="@+id/coordinateLayout">

                <ImageButton
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:alpha="0.36"
                    android:src="@drawable/ic_room_24dp"
                    android:background="@null" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginStart="@dimen/activity_horizontal_margin"
                    android:text="740, Valencia St, San Francisco, CA"
                    android:textColor="@android:color/primary_text_light"
                    android:id="@+id/bottom_sheet_coordinate" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="@dimen/activity_horizontal_margin"
                android:id="@+id/timeLayout">

                <ImageButton
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:alpha="0.36"
                    android:src="@drawable/ic_query_builder_24dp"
                    android:background="@null" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginStart="@dimen/activity_horizontal_margin"
                    android:text="Wed, 10 AM - 9 PM"
                    android:textColor="@android:color/primary_text_light"
                    android:id="@+id/bottom_sheet_time" />

            </LinearLayout>


        </LinearLayout>

    </FrameLayout>
</android.support.v4.widget.NestedScrollView>
Tanrikut
  • 286
  • 2
  • 11

6 Answers6

48

I ran into this, took a while to figure out what was the cause.

It's because you're using android:animateLayoutChanges, which surfaces a bug in either BottomSheetBehavior or CoordinatorLayout.

Remove it and the BottomSheet will stop animating on its own when it shouldn't. Not a fix, but a workaround at least.

--

Update:

Turns out that if you enable "animateLayoutChanges" programmatically by setting the LayoutTransition instance to use, you can set a flag on it that will prevent it from messing with views that are ancestors of the one you're using android:animateLayoutChanges on (aka: your BottomSheet container):

LayoutTransition transition = new LayoutTransition();
transition.setAnimateParentHierarchy(false);
yourLinearLayoutThatNeedsLayoutAnimation.setLayoutTransition(transition);
brAzzi64
  • 905
  • 8
  • 12
7

Removing the following line from my root layout solved this problem:

android:animateLayoutChanges="true"
Cícero Moura
  • 1,391
  • 1
  • 17
  • 30
1

As a workaround, you need to remove the android:animateLayoutChanges="true" from the parent coordinator layout

0

Try to make the parent of bottomSheet an independent CoordinatorLayout without having any other child Views. For example:

<RelativeLayout
    android:id="@+id/some_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Some_View
    .../>


    <Some_Other_view>
        .
        .
        .</Some_Other_view>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <include
            android:id="@+id/included_layout"
            layout="@layout/bottom_sheet" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
inkedTechie
  • 664
  • 5
  • 13
0

Change View.GONE to View.INVISIBLE. Since the View.GONE has no size, the bottom sheet can't compute the height of the child being updated.

double-beep
  • 3,889
  • 12
  • 24
  • 35
-1

The BottomSheetBehavior has its own behavior by which you can get respective results. The following are the behavior of the bottomsheet.

STATE_DRAGGING, STATE_SETTLING, STATE_EXPANDED, STATE_COLLAPSED, STATE_HIDDEN.

Don't use visibility of any layouts.

Use this behavior in your code like:

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int behaviorState = bottomSheetBehavior.getState();
        if (behaviorState == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        } else {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }
});
Bruno Bieri
  • 7,754
  • 10
  • 55
  • 79
Dhruv
  • 1,794
  • 3
  • 19
  • 37
  • i am not trying to change visibility of bottom sheet. I am trying to change what is visible inside that bottom sheet. – Tanrikut Jul 13 '16 at 06:07
  • This could also be written as: `behavior.setState(behavior.getState() == STATE_EXPANDED ? STATE_COLLAPSED : STATE_EXPANDED);` where as behavior is your bottomSheetBehavior. – Bruno Bieri Dec 16 '19 at 13:01