android - BottomSheet - 为什么 FAB 按钮在 View 上?当我再次关注 map 时如何隐藏 BottoSheet?

标签 android google-maps google-maps-markers floating-action-button bottom-sheet

我有两个问题。我的想法是:

当我点击 googlemap 的标记时, Bottom Sheet 会显示标记的详细信息。

我现在有两个问题。

  1. 当点击一个标记时,bottomsheet View 向上滑动并覆盖一半的屏幕(如我所愿)覆盖 map ,但 2 个 FAB 按钮仍保留在 bottomsheet 上方(我希望它们位于 bottomSheet 下方)。
  2. 当显示 bottomSheet 时,我希望如果我单击 map ,bottomSheet 应该消失(它没有)。

这是我的代码:

ma​​in_layout_activity.xml:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ankic.it.nasone.NasoneActivity" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_gravity="bottom|end|right"
        app:backgroundTint="@color/white"
        android:layout_margin="@dimen/fab_margin"
        app:fabSize="normal"

        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="top|end"
        android:layout_marginBottom="85dp"
        android:layout_marginEnd="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        app:layout_anchor="@id/floatingButton"
        app:layout_anchorGravity="top"
        app:backgroundTint="@color/white"
        />


    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:clipToPadding="true"
        android:background="@android:color/holo_orange_light"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        >

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is a test dialog fragment"
                android:textColor="@android:color/white"
                android:textSize="16sp"
                android:padding="16dp"
                android:background="@android:color/holo_purple"/>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/immagine_lista"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

MyActivity.java

这里是 fragment :

public class MyActivity extends AppCompatActivity
{

    protected FloatingActionButton floatingButton;
    private FloatingActionButton fabAdd;
    private BottomSheetBehavior mBottomSheetBehavior;

    ............



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ........

        this.floatingButton = (FloatingActionButton) findViewById(R.id.floatingButton);
        this.fabAdd=(FloatingActionButton)findViewById(R.id.fabAdd);
        this.cl=(CoordinatorLayout)findViewById(R.id.coordinatorLayoutNasoni) ;


        View bottomSheet = findViewById( R.id.bottom_sheet );
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

    }



    @Override
    protected void onResume() {
        super.onResume();


    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

                return false;
            }
        });
    }

}

我该怎么办?

最佳答案

您必须转到您的 mainactivity.java 类并将其添加到您的 onmarkerclick 方法中。它为 bottomsheet 的不同状态设置 float 操作按钮的行为,并记住为两个 float 操作按钮执行此操作..它在这里为其中一个实现 `

     // set callback for changes
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                // this part hides the button immediately and waits bottom sheet
                // to collapse to show
                if (BottomSheetBehavior.STATE_EXPANDED== newState) {
                    floatingButton.animate().scaleX(0).scaleY(0).setDuration(0).start();
                    floatingButton.setVisibility(View.GONE);
                } else if (BottomSheetBehavior.STATE_COLLAPSED == newState) {
                    floatingButton.animate().scaleX(1).scaleY(1).setDuration(0).start();
                    floatingButton.setVisibility(View.VISIBLE);
                } else if (BottomSheetBehavior.STATE_HIDDEN == newState) {
                    floatingButton.animate().scaleX(1).scaleY(1).setDuration(0).start();
                    floatingButton.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        });
//        Log.d(this.getClass().getSimpleName(), marker.getTitle());
        return true;
    }

关于android - BottomSheet - 为什么 FAB 按钮在 View 上?当我再次关注 map 时如何隐藏 BottoSheet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545086/

相关文章:

javascript - 当用户点击时更改 Google map 标记图标

android - 添加测试设备不工作

android - Google API key 不起作用

android - 有什么方法可以对所有级别的 api 进行安全的相机 Intent 吗?

google-maps - 用于卫星图像的 Google 地球的替代品

android - Google direction API 给出了错误的距离和持续时间

javascript - 防止标记在缩小时缩放

php - 为什么我的 Google 标记没有显示?

php - Android - 后台线程更新 PHP session

android - 如何在 Android 的 ImageView 上制作卷边?