3

我已成功使用包裹在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> 

我试着摸索,但无论我在寻找对于错误的事情或我没有人有类似的问题。我已经看到了我发布并在各种教程和视频中成功运行的行为代码,但它在地图视图中表现不佳。有什么想法吗?

+0

您应该添加“应用程序:layout_behavior =” com.adamshort.canieatthis.FloatingActionButtonBehaviour”你的漂浮动作按钮......而不是MapView的 – W0rmH0le

+0

@GuilhermeP这是一个自定义行为模仿所发生的浮动按钮。它的全部要点是将自定义行为应用到其他视图,这就是我给它的名称,因为它会复制FAB。 –

+0

对不起...你对...忘记它... – W0rmH0le

回答

0

请,我的回答如下是试图帮助你......让我知道,如果它可以帮助你或不...然后,我删除,如果它是无用的...

也许,你需要定义并定位到您的MapView。就我而言,我不得不在底部创建一个虚拟视图,并设置为archor到将与小吃吧

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

不好,不幸的是,问题不幸。 –

0

有完全相同的问题。

为了解决这个问题,我改变了onDependentViewChanged()方法来存储这个行为所附加的视图的初始Y位置。然后,在移除调用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); 
} 
+0

child.setTranslationY(0)是你在onDependentViewRemoved中需要做的事情,以将位置重置到其原始位置。setTranslationY将视图相对于其顶部位置(view.getTop())移动,因此setTranslationY(0)将其移回到最高的位置。 –