Android : CollapsingToolbarLayout 中的 MapView

Android : MapView in a CollapsingToolbarLayout

我目前正在尝试将 Map View(或包含地图的片段)放入 CollapsingToolbarLayout。当 RecyclerView 滚动时,我想对其产生视差效果。不幸的是,它根本没有出现!甚至没有灰色网格!折叠动画虽然有效。我到处搜索,但我能找到的只有 ImageView 而没有其他组件。所以这是我的问题:

  1. 是否可以在 CollapsingToolbarLayout 中放置 ImageView 之外的任何其他内容? (文档在谈论子视图,所以我认为这是可能的)
  2. 如果可以,是否也允许使用片段?
  3. 那我做错了什么??

Here is my xml :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:map="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/main_activity_toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                android:background="?attr/colorPrimary"
                android:layout_alignParentTop="true"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            </android.support.v7.widget.Toolbar>

            <com.google.android.gms.maps.MapView
                android:id="@+id/main_activity_mapview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:apiKey="AIzaSyA-kEuKT39QK8eG7iYWriFgsvkrZZz6zNo"
                android:clickable="true"
                app:layout_collapseMode="parallax" />

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/main_rv_list_places"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

And the part where I get the map back. onMapReady is never triggered.

    if (mMap == null) {

        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                MapsInitializer.initialize(MainActivity.this);
                //...
                // other stuff here
            }
        });

    }

这对我来说很好

使用 Fragment 代替 MapView

 <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="com.example.axel.googlemapsdemo.MapsActivity"/>

Activity

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

        private GoogleMap mMap;
        RecyclerView mRecyclerView;
        TestAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            setUpRecyclerView();
        }


        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
   `        // Do stuff
        }
}

API 密钥在 google_maps_api.xml.

我的父布局是 CoordinatorLayout(我想也是你的,但它没有出现在问题上)

注意用这种方法CollapsingToolbarLayout scroll overcast Map scroll

解决方案:

AppBarLayout mAppBarLayout = (AppBarLayout)findViewById(R.id.appBarLayout);
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
        behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
            @Override
            public boolean canDrag(AppBarLayout appBarLayout) {
                return false;
            }
        });
        params.setBehavior(behavior);

这个问题在这里解决

注意:也许底部 Sheet 最适合您的需求

一些例子: http://www.truiton.com/2016/07/android-bottom-sheet-example/ https://github.com/miguelhincapie/CustomBottomSheetBehavior

You can use MapView also to display google map. 

在 Coordinator 布局中,我们可以将折叠工具栏与 mapview 一起使用。它工作正常。

需要正确提及 google 映射键。

activity_main.xml:

<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">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_lay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false"
            app:statusBarScrim="@null"
            android:fitsSystemWindows="true">
                <com.google.android.gms.maps.MapView
                    android:id="@+id/map_view"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:layout_below="@+id/main_lay"
                    android:apiKey="@string/google_maps_key"
                    android:clickable="true"
                    android:enabled="true"
                    android:focusable="true"
                    app:layout_collapseMode="parallax"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/post_rcycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:visibility="visible"
        android:clipToPadding="false"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="parallax"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>

MainActivity.Java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener, GoogleMap.OnMapLoadedCallback  {
    private MapView mapView;
    private GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = (MapView) findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    mapView.getMapAsync(this);
}
}