Android:折叠工具栏布局中的地图视图
我目前正在尝试将Android:折叠工具栏布局中的地图视图,android,android-mapview,mapfragment,android-collapsingtoolbarlayout,Android,Android Mapview,Mapfragment,Android Collapsingtoolbarlayout,我目前正在尝试将地图视图(或包含地图的片段)放入折叠工具栏布局。我想在RecyclerView滚动时对其产生视差效果。不幸的是,它根本没有出现!连灰色的格子都没有!不过,正在折叠的动画仍在工作。我到处都在搜索,但我能找到的只是关于ImageView,没有其他组件。下面是我的问题: 甚至可以在折叠工具栏布局中放置图像视图以外的任何内容吗?(文档讨论的是子视图,因此我认为这是可能的) 如果是,是否也允许使用片段 那我做错了什么 以下是我的xml: 这对我来说很好 使用片段而不是MapView &l
地图视图
(或包含地图的片段)放入折叠工具栏布局
。我想在RecyclerView
滚动时对其产生视差效果。不幸的是,它根本没有出现!连灰色的格子都没有!不过,正在折叠的动画仍在工作。我到处都在搜索,但我能找到的只是关于ImageView
,没有其他组件。下面是我的问题:
折叠工具栏布局中放置图像视图
以外的任何内容吗?(文档讨论的是子视图,因此我认为这是可能的)
<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"/>
API密钥位于google_maps_API.xml中
我的父布局是CoordinatorLayout(我想也是你的,但问题上没有出现)
请注意,使用此方法可折叠工具栏布局滚动阴影地图滚动
解决方案:
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);
这个问题在这里得到了解决
注意:也许底部床单最适合您的需要
一些例子:
在协调器布局中,我们可以将折叠工具栏与mapview一起使用。工作正常
需要正确提及谷歌地图键
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>
是否正确初始化映射(即,在活动中的相同点调用onCreate()、onResume()、onPause())?是否尝试使用片段而不是
MapView
?@m vai Ok。。。我觉得自己很愚蠢。。。我没有调用这些函数,现在我调用了,它工作得很好!非常感谢:)我正在使用fragment而不是mapview…地图正在滚动,但当我滚动时黑色背景仍然存在…我不知道为什么?你能帮我吗?嗨。我试图使用一个片段,但始终没有成功。为什么不使用地图视图?它工作正常,在如何使用它方面没有太大变化。mapView.getMapAsync(此)
行内部将调用onMapReady
,因此您的代码将是一个无止境的循环。
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
}
}
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);
You can use MapView also to display google map.
<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>
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);
}
}