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

时间:2016-05-16 22:53:26

标签: android android-fragments android-mapview android-snackbar

我成功地使用围绕片段中的MapView的协调器布局成功地获得了一个小吃吧。地图视图成功滑动以便为小吃店留出空间,但是,一旦小吃店消失,视图就会停留,并留下一个白色的小酒吧,小吃吧会出现。我怎样才能解决这个问题?我目前正在应用以下代码作为地图视图上的行为:

    @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;
    }

我的片段视图是这样的:

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

我试过四处寻找,但要么我正在寻找错误的东西,要么我没有人遇到类似的问题。我已经看过我发布的行为代码并在各种教程和视频中成功运行,但它在地图视图中表现不佳。有什么想法吗?

3 个答案:

答案 0 :(得分:0)

请,下面我的回答是试图帮助你...让我知道它是否对你有帮助...然后,如果没用就删除我...

也许,您需要定义并锚定到MapView。在我的情况下,我必须在底部创建一个虚拟视图,并将其设置为视图的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>

答案 1 :(得分:0)

这是Google打算防止干扰无障碍操作的。 https://code.google.com/p/android/issues/detail?id=209953

答案 2 :(得分:0)

有完全相同的问题。

要解决此问题,我更改了onDependentViewChanged()方法以存储此行为附加到的视图的初始Y位置。然后在删除调用onDependentViewRemoved()方法的snackbar时,将该视图的位置重置为存储的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);
}