【问题标题】:Layout view stuck after snackbar appears with coordinator view小吃栏与协调器视图一起出现后布局视图卡住
【发布时间】:2016-05-16 22:53:26
【问题描述】:

我已经成功地使用围绕片段中 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>

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

【问题讨论】:

  • 您应该将“app:layout_behavior="com.adamshort.canieatthis.FloatingActionButtonBehaviour" 添加到浮动操作按钮...而不是 MapView
  • @GuilhermeP 这是一种模仿浮动按钮发生的自定义行为。它的全部意义在于将自定义行为应用于其他视图,这只是我给它的名称,因为它将复制 FAB。
  • 对不起...你对...算了...

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


【解决方案1】:

拜托,我下面的回答是试图帮助你...如果它对你有帮助,请告诉我...然后,如果它没用,我删除...

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

【讨论】:

  • 不一样,不幸的是问题。
【解决方案2】:

Google 旨在防止干扰无障碍操作。 https://code.google.com/p/android/issues/detail?id=209953

【讨论】:

    【解决方案3】:

    遇到了完全相同的问题。

    为了解决这个问题,我更改了 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);
    }
    

    【讨论】:

    • child.setTranslationY(0) 是您需要在 onDependentViewRemoved 中将位置重置为其原始位置的操作。 setTranslationY 相对于其顶部位置 (view.getTop()) 移动视图,因此 setTranslationY(0) 将其移回顶部位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多