Android:CollapsingToolbarLayout中的MapView [英] Android : MapView in a CollapsingToolbarLayout
问题描述
我目前正在尝试将Map View
(或包含地图的片段)放入CollapsingToolbarLayout
内.我想在RecyclerView
滚动时对其产生视差效果.不幸的是,它根本没有出现!甚至没有灰色网格!折叠动画正在运行.我到处搜索,但是我只能找到关于ImageView
的信息,而没有其他组件.所以这是我的问题:
I'm currently trying to put a Map View
(or a fragment containing the map)inside a CollapsingToolbarLayout
. I would want to have the parallax effect on it when the RecyclerView
scrolls. Unfortunately, it isn't showing up at all! Not even the grey grid! The collapsing animation is working though. I've search everywhere, but all I was able to find is about ImageView
and no other component. So here are my questions :
- 是否有可能在
CollapsingToolbarLayout
中放置除ImageView
以外的任何内容? (文档讨论的是子视图,因此我认为这是可能的) - 如果是,还允许使用片段吗?
- 那我在做什么错了??
- Is it even possible to put anything else than an
ImageView
in aCollapsingToolbarLayout
? (The documentation is talking about child views so I thought it was possible) - If so, is a fragment allowed as well?
- Then what am I doing wrong??
这是我的xml:
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>
还有我拿回地图的那一部分.
onMapReady
永远不会 触发.
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
}
});
}
推荐答案
You can use MapView also to display google map.
在Coordinator布局中,我们可以将可折叠的工具栏与mapview一起使用.工作正常.
Within Coordinator layout we can use collapsing toolbar with mapview . Its working fine.
需要正确提及google map键.
Need to mention google map key correctly.
activity_main.xml:
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:
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);
}
}
这篇关于Android:CollapsingToolbarLayout中的MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!