【问题标题】:BottomSheet fly away with visibility changeBottomSheet 随着能见度的变化而飞走
【发布时间】:2016-07-12 11:38:37
【问题描述】:

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

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

我的更改可见性方法:

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

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

}

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

【问题讨论】:

  • 你在哪里调用 changeVisibility() 方法?
  • 在 fab 按钮的 clicklistener 中

标签: android bottom-sheet android-nestedscrollview


【解决方案1】:

我遇到了这个,花了一段时间才弄清楚是什么原因。

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

删除它,BottomSheet 将在不应该的时候自行停止动画。不是修复,但至少是一种解决方法。

--

更新:

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

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

【讨论】:

  • 这正是解决问题的方法! - 谢谢!当前支持库 v25.0.1 中仍然存在此问题
  • 你是救世主,花了相当多的时间调查这个问题。
  • 2020 年仍然是个问题
【解决方案2】:

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

android:animateLayoutChanges="true"

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        View.GONE 更改为View.INVISIBLE。由于View.GONE 没有大小,底部工作表无法计算正在更新的孩子的高度。

        【讨论】:

          【解决方案6】:

          BottomSheetBehavior 有自己的行为,您可以通过它获得相应的结果。下面是bottomsheet的行为。

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

          【讨论】:

          • 我不想改变底页的可见性。我正在尝试更改该底部表中可见的内容。
          • 这也可以写成:behavior.setState(behavior.getState() == STATE_EXPANDED ? STATE_COLLAPSED : STATE_EXPANDED); where as behavior 是你的bottomSheetBehavior。
          猜你喜欢
          • 2019-08-27
          • 1970-01-01
          • 1970-01-01
          • 2021-11-28
          • 2014-06-25
          • 2020-02-14
          • 1970-01-01
          • 2018-09-15
          • 1970-01-01
          相关资源
          最近更新 更多