BottomSheet随着能见度的变化飞走了

Tan*_*kut 13 android android-nestedscrollview bottom-sheet

我有一个带有NestedScrollView的底页(见下文).当我按下FAB按钮时,我想让这个NestedScrollView中的某些部分不可见.但是当我将一些线性布局的可见性改为GONE时,底片会从顶部飞走.看这里:

在此输入图像描述

您可以从https://github.com/Tanrikut/BottomSheetExample获取整个代码

我的变更可见性方法:

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

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

}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

brA*_*i64 29

我遇到了这个问题,花了一段时间才弄明白是什么原因.

这是因为您正在使用android:animateLayoutChanges,它会在BottomSheetBehavior或CoordinatorLayout中出现错误.

删除它,BottomSheet将不会自动停止动画.不是解决方案,但至少是一种解决方法.

-

更新:

事实证明,如果通过设置要使用的LayoutTransition实例以编程方式启用"animateLayoutChanges",则可以在其上设置一个标志,以防止它与您正在使用android:animateLayoutChanges on(也称为:你的BottomSheet容器):

LayoutTransition transition = new LayoutTransition();
transition.setAnimateParentHierarchy(false);
yourLinearLayoutThatNeedsLayoutAnimation.setLayoutTransition(transition);
Run Code Online (Sandbox Code Playgroud)

  • 2020年仍然是一个问题 (4认同)

Cíc*_*ura 7

从我的根布局中删除以下行解决了这个问题:

android:animateLayoutChanges="true"
Run Code Online (Sandbox Code Playgroud)


Dhr*_*ruv -1

BottomSheetBehavior有自己的行为,您可以通过它获得各自的结果。以下是底片的行为。

STATE_DRAGGING,,,,, .STATE_SETTLINGSTATE_EXPANDED​​STATE_COLLAPSEDSTATE_HIDDEN

不要使用任何布局的可见性。

在您的代码中使用此行为,例如:

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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)