简体   繁体   English

快餐栏出现后布局视图卡在协调员视图中

[英]Layout view stuck after snackbar appears with coordinator view

I've managed to get a snackbar show successfully using a coordinator layout wrapped around a MapView in a fragment. 我已经成功地使用在片段中围绕MapView的协调器布局成功地获得了一个小吃吧。 The map view successfully slides up to allow room for the snackbar, however, once the snackbar has gone, the view stays were it is and leaves a white bar where the snackbar would appear. 地图视图成功滑动以便为小吃店留出空间,但是,一旦小吃店消失,视图就会停留,并留下一个白色的小酒吧,小吃吧会出现。 How can I fix this? 我怎样才能解决这个问题? I'm currently applying the following code as a behavior on the map view: 我目前正在应用以下代码作为地图视图上的行为:

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
        child.setTranslationY(translationY);
        return true;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        // we only want to trigger the change
        // only when the changes is from a snackbar
        return dependency instanceof Snackbar.SnackbarLayout;
    }

and my fragments view is this: 我的片段视图是这样的:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/places_coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.adamshort.canieatthis.FloatingActionButtonBehaviour"/>

</android.support.design.widget.CoordinatorLayout>

I've tried searching around, but either I'm searching for the wrong thing or I no one's had a similar issue. 我试过四处寻找,但要么我正在寻找错误的东西,要么我没有人遇到类似的问题。 I've seen the behavior code I have posted and working successfully in various tutorials and videos, but it doesn't behave well with the map view. 我已经看过我发布的行为代码并在各种教程和视频中成功运行,但它在地图视图中表现不佳。 Any thoughts? 有什么想法吗?

Please, my answer below is an attempt to help you... Let me know if it helps you or not... Then, I delete if it is useless... 请问,我的回答是试图帮助你...让我知道它是否对你有帮助...然后,我删除它是否无用...

Maybe, you need to define and anchor to your MapView. 也许,您需要定义并锚定到MapView。 In my case, I had to created a dummy view in the bottom and set it as archor to the view which will move with the SnackBar 在我的情况下,我不得不在底部创建一个虚拟视图,并将其设置为视图的archor,它将与SnackBar一起移动

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/places_coordinator_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.Space
        android:id="@+id/anchor"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/transparent"
        android:layout_gravity="bottom" />

        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_anchor="@id/anchor"
            app:layout_behavior="com.adamshort.canieatthis.FloatingActionButtonBehaviour"/>

</android.support.design.widget.CoordinatorLayout>

This is intended by Google to prevent interfering with accessibility operation. 这是谷歌打算防止干扰无障碍操作。 https://code.google.com/p/android/issues/detail?id=209953 https://code.google.com/p/android/issues/detail?id=209953

Had exactly the same problem. 有完全相同的问题。

To fix this I changed the onDependentViewChanged() method to store the initial Y position of the view this behaviour is attached to. 为了解决这个问题,我更改了onDependentViewChanged()方法来存储此行为附加到的视图的初始Y位置。 Then on removal of the snackbar, which calls the onDependentViewRemoved() method, reset the position of that view to the stored Y position. 然后在删除调用onDependentViewRemoved()方法的快餐栏时,将该视图的位置重置为存储的Y位置。

private static float initialPositionY;

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    initialPositionY = child.getY();
    float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
    child.setTranslationY(translationY);
    return true;
}

@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
    super.onDependentViewRemoved(parent, child, dependency);
    child.setTranslationY(initialPositionY);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在使用协调器布局后显示快餐栏时,我的其他视图没有上移? - why my other view doesn't move up when the snackbar show after using coordinator layout? 将静态视图添加到协调器布局 - Adding static view to coordinator layout Coordinator 布局内的回收站视图 - Recycler view inside Coordinator layout 使用带有回收器视图的协调器布局 - Using coordinator layout with recycler view 协调器布局中的嵌套滚动视图不起作用? - Nested Scroll View in Coordinator Layout not working? 协调器布局内的回收器视图未以编程方式滚动 - Recycler view inside coordinator layout is not scrolling programmatically 如何将带有片段的协调器布局用作“滚动视图” - How to use coordinator layout with fragment as “scrolling view” 在协调器布局中自定义视图的BringToFront - BringToFront of the custom view inside a coordinator layout Android:Recycler视图是否可以使用协调器布局? - Android:Does the Recycler view works with Coordinator layout? 协调器布局 - 使用回收器视图折叠内容交互 - Coordinator layout - collapsing content interaction with recycler view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM