我在我的三个标签片段中添加了一个全屏地图片段。除了我在标签之间切换时遇到的问题,一切都很有效。
每次我从带有地图的片段切换到其他片段之一时,我会得到一个0.2秒的黑屏。对于使用地图片段滚动活动的问题,似乎是一个类似的问题。
我已尝试在其他地方建议的地方添加透明视图,但它没有帮助。
这是我的xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ParkMapFragment">
<fragment
android:id="@+id/fullParkMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
这是我的代码:
public class ParkMapFragment extends Fragment {
public ParkMapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onStart(){
super.onStart();
getActivity().setTitle("Map");
}
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fullParkMap);
if (mapFragment != null) {
getFragmentManager().beginTransaction().remove(mapFragment).commit();
}
}
}
有人有任何建议吗?
答案 0 :(得分:5)
检查视图是否为null,如果不是,则将其从父视图中删除。这样,你就不会看到黑屏。
尝试以下代码。
片段:
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExhibhitionMap2 extends Fragment implements OnMapClickListener,OnMarkerClickListener{
View v;
private GoogleMap mMap;
FragmentManager fragmentManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// android = inflater.inflate(R.layout.activity_exhibhition_map, container, false);
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
}
try {
v = inflater.inflate(R.layout.activity_exhibhition_map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
setUpMapIfNeeded();
return v;
}
/***** Sets up the map if it is possible to do so *****/
public void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map2)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setOnMapClickListener(this);
mMap.setOnMarkerClickListener((OnMarkerClickListener) this);
MarkerOptions marker = new MarkerOptions().position(new LatLng(36.011513,-115.174853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title("P-1");
MarkerOptions marker01 = new MarkerOptions().position(new LatLng(36.111513,-115.204853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title("P-2");
MarkerOptions marker02 = new MarkerOptions().position(new LatLng(36.051513,-115.154853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title("P-3");
MarkerOptions marker03 = new MarkerOptions().position(new LatLng(36.057513,-115.344853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title("P-4");
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(36.081513,-115.224853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
.title("F-1");
MarkerOptions marker2 = new MarkerOptions().position(new LatLng(36.051513,-115.334853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
.title("F-2");
MarkerOptions marker3 = new MarkerOptions().position(new LatLng(36.101513,-115.124853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
.title("F-1");
MarkerOptions marker4 = new MarkerOptions().position(new LatLng(36.031513,-115.154853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
.title("F-2");
MarkerOptions marker5 = new MarkerOptions().position(new LatLng(36.081513,-115.194853))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
.title("F-2");
mMap.addMarker(marker);
mMap.addMarker(marker01);
mMap.addMarker(marker02);
mMap.addMarker(marker03);
mMap.addMarker(marker1);
mMap.addMarker(marker2);
mMap.addMarker(marker3);
mMap.addMarker(marker4);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(36.051513,-115.174853), 10.0f));
}
}
}
@Override
public boolean onMarkerClick(Marker marker) {
/*Toast.makeText(getActivity(),
"" + marker.getTitle(), Toast.LENGTH_LONG)
.show();*/
marker.showInfoWindow();
return false;
}
@Override
public void onMapClick(LatLng arg0) {
// TODO Auto-generated method stub
}
public void onDestroy() {
super.onDestroy();
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.istride.mmps.ContactActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tableLayout1"
android:visibility="gone"
android:layout_margin="10dp" >
<TextView
android:id="@+id/tv_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Bharati Vidyapeeth University's"
android:textColor="@color/black_text_color"
android:textSize="@dimen/Large_Size" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_below="@+id/tv_Title"
android:text="Department of Information Technology and Management "
android:textColor="@color/black_text_color"
android:textSize="@dimen/small_size" />
<View
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="@+id/tv"
android:layout_marginTop="10dp"
android:background="@color/blue_background" />
<RelativeLayout
android:id="@+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view2"
android:layout_marginTop="10dp"
android:background="@color/gray_home">
<TextView
android:id="@+id/textVi1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_add"
android:layout_alignParentTop="true"
android:background="@color/blue_background"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="13dp"
android:paddingTop="2dp"
android:gravity="center_vertical"
android:text="ADDRESS "
android:textColor="@color/white_text_color"
android:textSize="@dimen/Medium_size"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/textVi1"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="2nd Floor, Architecture Building, Bharati Vidyapeeth Campus, Dhankawadi, Pune-411043"
android:textColor="@color/black_text_color"
android:textSize="@dimen/Medium_size" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout_call"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relative"
android:layout_marginTop="10dp"
android:background="@color/gray_home">
<TextView
android:id="@+id/textView1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv_school_office_num"
android:layout_alignParentTop="true"
android:background="@color/blue_background"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="13dp"
android:paddingTop="2dp"
android:text="PHONE "
android:gravity="center_vertical"
android:textColor="@color/white_text_color"
android:textSize="@dimen/Medium_size"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_school_office_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_toRightOf="@+id/textView1"
android:layout_toLeftOf="@+id/imageView2"
android:textColor="@color/black_text_color"
android:paddingTop="2dp"
android:textSize="@dimen/Medium_size"
android:paddingBottom="2dp"
android:text="+91 989898989 asasa asas asasas ass sdsd asas asas ass assas asas ass " />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:src="@drawable/ic_call" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout_Email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout_call"
android:layout_marginTop="10dp"
android:background="@color/gray_home">
<TextView
android:id="@+id/tex1"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentTop="true"
android:background="@color/blue_background"
android:gravity="center_vertical"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="13dp"
android:paddingTop="2dp"
android:text="EMAIL "
android:textColor="@color/white_text_color"
android:textSize="@dimen/Medium_size"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_school_office_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/tex1"
android:textColor="@color/black_text_color"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textSize="@dimen/Medium_size"
android:text="principal.mmei@eternalmewar.in" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:src="@drawable/ic_email" />
</RelativeLayout>
</RelativeLayout>
<fragment
android:id="@+id/map2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:background="@color/gray_color"
android:paddingBottom="5dp"
android:layout_above="@+id/footer"
android:textSize="@dimen/small_size" />
<RelativeLayout
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:id="@+id/blue"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#0000FF"
android:gravity="center"
android:layout_margin="10dp"
android:textColor="@color/black_text_color"
android:textSize="@dimen/small_size"
android:textStyle="bold" />
<TextView
android:id="@+id/parking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/blue"
android:layout_alignTop="@+id/blue"
android:layout_alignBottom="@+id/blue"
android:gravity="center_vertical"
android:text="Parking"
android:textColor="@color/black_text_color"
android:textSize="@dimen/Medium_size"
android:textStyle="bold" />
<TextView
android:id="@+id/cyc"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#00FFFF"
android:gravity="center"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/parking"
android:textColor="@color/black_text_color"
android:textSize="@dimen/small_size"
android:textStyle="bold" />
<TextView
android:id="@+id/footer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/cyc"
android:layout_alignTop="@+id/cyc"
android:layout_alignBottom="@+id/cyc"
android:gravity="center_vertical"
android:text="Food"
android:textColor="@color/black_text_color"
android:textSize="@dimen/Medium_size"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
答案 1 :(得分:2)
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fullParkMap);
if (mapFragment != null) {
getFragmentManager().beginTransaction().remove(mapFragment).commit();
}
}
此处您正在删除片段而不替换它。我会用.replace代替一个新片段。
这样的事情:
getFragmentManager().beginTransaction().replace(R.id.your_placeholder, new CustomFragment(), "cstmFragID").commit();
答案 2 :(得分:2)
我建议您使用 MapView 而不是 SupportFragment 来避免嵌套片段有关详细信息,请查看以下帖子
Should I use MapView or MapFragment
现在你的ParkFragment看起来像
<强> MapFragment.java 强>
public class MapFragment extends Fragment implements OnMapReadyCallback {
private MapView mMapView;
private GoogleMap mGoogleMap;
public MapFragment() {
// Required empty public constructor
}
public static MapFragment newInstance() {
MapFragment fragment = new MapFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = (MapView) rootView.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
return rootView;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
public void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
}
<强> fragment_map.xml 强>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 3 :(得分:1)
这是known issue。与此同时,您可以通过在地图顶部放置另一个视图来使用与其容器相同的背景颜色来解决问题。这使得地图一旦准备好就会顺利进入。
SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true));
答案 4 :(得分:1)
由于你总是替换片段,所以map总是被创建然后解决方法就是只加载你的地图片段一次,其余时间只显示/隐藏你的片段(好像你正在显示标签片段)。
答案 5 :(得分:1)
问题可能是因为它正在重新创建导致黑屏的Fragment
。如果您使用的是ViewPager
,那么您可以使用
// Note that you must set adapter before this line otherwise it can cause NullPointerException
// Or add null check on getAdapter if required
mViewPager.setOffscreenPageLimit(mViewPager.getAdapter().getCount());
它会强制ViewPager
不再创建碎片。当然,它需要更多内存,但我相信使用较少的标签会更有效,所以我通常这样做是为了避免重新创建片段。
其次我建议从onDestroy
中删除这些行:
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fullParkMap);
if (mapFragment != null) {
getFragmentManager().beginTransaction().remove(mapFragment).commit();
}
通过更新onCreateView
来解决此问题:
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_map, container, false);
}
return mView;
}
每次都不会使布局膨胀,因此您不会面临重复ID,标记为null或父ID与com.google.android.gms.maps.MapFragment 问题的其他片段。
但请确保您在 attachToRoot (inflater.inflate的最后一个参数)中传递 false ,否则您可能会遇到像这样的例外已指定的孩子有父母
最后,当在片段中使用 MapFragment 时,或者换句话说,当使用嵌套片段时,我建议使用 getChildFragmentManager()而不是< strong> getFragmentManager()以避免意外问题。
因为documentation说:
getFragmentManager:返回FragmentManager以与与此片段的活动相关联的片段进行交互。请注意,在将片段放入FragmentTransaction直到将其提交并附加到其活动之间的时间内,这将在getActivity()之前略微为空。如果此Fragment是另一个Fragment的子级,则此处返回的FragmentManager将是父级的getChildFragmentManager()。
getChildFragmentManager:返回一个私有的FragmentManager,用于放置和管理此片段内的片段。
答案 6 :(得分:1)
您是否尝试隐藏并显示片段(而不是删除/添加片段)?
private FragmentA fragmentA;
private FragmentB fragmentB;
private FragmentC fragmentC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
fragmentA = FragmentA.newInstance("foo");
fragmentB = FragmentB.newInstance("bar");
fragmentC = FragmentC.newInstance("baz");
}
}
在您的情况下,您可以将其更改为ParkMapFragment等。
protected void displayFragmentA() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (fragmentA.isAdded()) { // if the fragment is already in container
ft.show(fragmentA);
} else { // fragment needs to be added to frame container
ft.add(R.id.flContainer, fragmentA, "A");
}
// Hide fragment B
if (fragmentB.isAdded()) { ft.hide(fragmentB); }
// Hide fragment C
if (fragmentC.isAdded()) { ft.hide(fragmentC); }
// Commit changes
ft.commit();
}
来源:https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments
答案 7 :(得分:1)
在createView中尝试:
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
}
答案 8 :(得分:0)
您应该通过这种方式转换片段:
Fragment fragment = new YourFragmentToShow();
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, TAG).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
使用TRANSIT_FRAGMENT_FADE片段应该简单地淡入或淡出;也就是说,除了由于某种原因出现或消失之外,没有与之相关的强大导航。
当你用另一个片段用map替换片段时使用它,在其他情况下我推荐常用,没有(.setTransition ...):
Fragment fragment = new YourFragmentToShow();
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, TAG).commit();