BottomSheet随着能见度的变化而飞走 [英] BottomSheet fly away with visibility change

查看:95
本文介绍了BottomSheet随着能见度的变化而飞走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内部带有NestedScrollView的底部工作表(请参见下文).当我按下FAB按钮时,我想使此NestedScrollView中的某些部分不可见.但是,当我将一些linearlayouts可见性更改为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>

解决方案

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

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

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

-

更新:

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

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

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:

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>

解决方案

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

这篇关于BottomSheet随着能见度的变化而飞走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆