Android: 在CollapsingToolbarLayout中使用MapView

6
我目前正在尝试将一个地图视图(或包含地图的片段)放置在CollapsingToolbarLayout中。我希望当RecyclerView滚动时,它能产生视差效果。不幸的是,它根本没有显示出来!甚至连灰色的网格都没有!不过,折叠动画是可以工作的。我已经到处搜索了,但我只找到了关于ImageView而没有其他组件的信息。所以,这里是我的问题:
  1. CollapsingToolbarLayout中除了ImageView还能放其他东西吗?(文档提到了子视图,所以我认为是可能的)
  2. 如果可以,那么片段也可以吗?
  3. 那我到底做错了什么?

这是我的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>

当我获取地图后,onMapReady 没有被触发。
    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
            }
        });

    }

你是否正确初始化地图(即在活动的相同点调用onCreate()、onResume()、onPause())? - natario
1
你尝试使用片段代替 MapView 了吗? - romtsn
@m 嗯...我感觉很蠢...我没有调用那些函数,现在我调用了它们,它们运行得很好!非常感谢 :) - Kazuya
我正在使用 Fragment 而不是 MapView…地图可以滚动,但当我滚动时,黑色背景仍然存在…我不知道为什么?你能帮我吗? - H Raval
嗨。我尝试使用一个片段,但从未成功过。为什么不使用MapView?它运行良好,并且在使用方式上没有太大变化。 - Kazuya
2个回答

3
You can use MapView also to display google map. 

在Coordinator布局中,我们可以使用折叠式工具栏和地图视图。它可以正常工作。

需要正确提及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);
}
}

mapView.getMapAsync(this) 这行代码会调用 onMapReady 方法,因此你的代码将会是一个无限循环。 - Farid

1

这对我来说很好用

使用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"/>

活动

    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会覆盖地图滚动

解决方案:

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);

这个问题在 如何正确使用Collapsingtoolbarlayout中的地图碎片 中得到了解决

注意:或许底部抽屉是最适合您的需求的

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接