关于android:BottomSheet随着可见度的变化而飞走

BottomSheet fly away with visibility change

我有一个内部带有NestedScrollView的底部工作表(请参见下文)。当我按下FAB按钮时,我想使此NestedScrollView中的某些部分不可见。但是,当我将一些linearlayouts可见性更改为GONE时,底页就会从顶部飞走。参见此处:

enter

1
2
3
4
5
6
7
private void changeVisibility() {
    subtitleLayout.setVisibility(View.GONE);

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

}

我的NestedScrollView xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?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>


我遇到了这个问题,花了一段时间才找出原因。

这是因为您使用的是android:animateLayoutChanges,它会在BottomSheetBehavior或CoordinatorLayout中显示一个错误。

将其删除,BottomSheet会在不应该的情况下自行停止设置动画。不是修复程序,但至少是解决方法。

-

更新:

事实证明,如果通过设置要使用的LayoutTransition实例以编程方式启用" animateLayoutChanges",则可以在其上设置一个标志,以防止其与正在使用android:animateLayoutChanges的视图的祖先的视图混淆(又名:您的BottomSheet容器):

1
2
3
LayoutTransition transition = new LayoutTransition();
transition.setAnimateParentHierarchy(false);
yourLinearLayoutThatNeedsLayoutAnimation.setLayoutTransition(transition);


从我的根目录布局中删除以下行即可解决此问题:

1
android:animateLayoutChanges="true"

作为一种解决方法,您需要从父协调器布局中删除android:animateLayoutChanges="true"


View.GONE更改为View.INVISIBLE。由于View.GONE没有大小,因此底页无法计算要更新的子项的高度。


尝试使bottomSheet的父级成为独立的CoordinatorLayout,而没有任何其他子级Views。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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>

BottomSheetBehavior具有其自己的行为,通过它可以获取各自的结果。以下是底页的行为。

STATE_DRAGGINGSTATE_SETTLINGSTATE_EXPANDEDSTATE_COLLAPSEDSTATE_HIDDEN

请勿使用任何布局的可见性。

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

1
2
3
4
5
6
7
8
9
10
11
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);
        }
    }
});


也许这可以帮助您!我无法发表评论,因此将其发布为答案

这里

对于幻灯片布局,与底页相同,但很好。