在标记点击上从谷歌地图底部滑动视图

gno*_*lle 2 android google-maps floating-action-button

我想实现以下目标: GMaps在模型中滑动

单击标记时,"信息"视图应从底部滑入.黑色圆圈是一个浮动动作按钮,应该通过信息窗口上升到顶部,其中心位于信息窗口的上边界.

当我点击信息窗口的任何地方时,它应该向下滑动并消失.

你能给我一些起点吗?我已经阅读过关于对象动画师和CoordinatorLayout的内容,但我不知道从哪里开始.

Olg*_*zek 6

您可以通过将底部元素添加到其父布局MapFragment然后设置自定义来自己实现它OnMarkerClickListener.

要使底页工作,您应该使用CoordinatorLayoutfor的父级MapFragment.要创建底部元素,应设置app:layout_behavior="android.support.design.widget.BottomSheetBehavior为布局元素,例如a NestedScrollView.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<include layout="@layout/content_main" />

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


    <TextView android:id="@+id/detail_name" android:layout_width="0dp"
        android:layout_height="wrap_content" android:layout_margin="25dp"
        android:layout_weight="3" android:gravity="center_vertical"
        android:textAppearance="?android:textAppearanceLarge" />

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

地图片段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

  <fragment
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/map"
  android:name="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后在您的活动类中,您可以在以下位置设置底部工作表的初始行为onCreate():

private BottomSheetBehavior bottomSheetBehavior;
private View bottomSheet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bottomSheet = findViewById(R.id.bottom_sheet);
    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    bottomSheetBehavior.setPeekHeight(200);
    bottomSheetBehavior.setHideable(true);
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
    }
Run Code Online (Sandbox Code Playgroud)

然后在onMapReady()以下位置修改地图行为:

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            updateBottomSheetContent(marker);
            return true;
        }
    });
    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }
    });
}

private void updateBottomSheetContent(Marker marker) {
    TextView name = (TextView) bottomSheet.findViewById(R.id.detail_name);
    name.setText(marker.getTitle());
  bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
Run Code Online (Sandbox Code Playgroud)

当然,您也可以使用存储在标记中的一些数据来查询数据库或ContentProvider进行适当的记录,然后使用返回的数据在底部工作表中设置更多文本,图像等.