2016-09-05 3 views
-1

Titel kann ein wenig kompliziert sein, aber das Problem ist buchstäblich wie beschrieben. Ich versuche Google Map Fragment in einem Fragmentstapel zu verwenden. Wenn die Anwendung gestartet wird, wird dem Benutzer eine Schaltfläche angezeigt, und wenn der Benutzer die Schaltfläche drückt, wird die aktuelle Ansicht durch das Kartenfragment ersetzt. Momentan kann ich die Fragmente ersetzen, aber wenn das Kartenfragment angezeigt wird, hat die Ansicht immer noch die Schaltfläche des vorherigen Fragments. Außerdem versuche ich Fragmente in einer der Registerkarten meiner App zu ersetzen. Hier ist mein Code;Google Map-Fragment zeigt das Element des vorherigen Fragments

Kartenfragment;

public class MapFragment extends Fragment { 

    MapView mMapView; 
    private GoogleMap googleMap; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_maps, container, false); 

     mMapView = (MapView) rootView.findViewById(R.id.mapView); 
     mMapView.onCreate(savedInstanceState); 

     mMapView.onResume(); // needed to get the map to display immediately 

     try { 
      MapsInitializer.initialize(getActivity().getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mMapView.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap mMap) { 
       googleMap = mMap; 
       try { 
        googleMap.setMyLocationEnabled(true); 
       } catch (SecurityException e) { 
        e.printStackTrace(); 
       } 
       // For dropping a marker at a point on the Map 
       LatLng sydney = new LatLng(-34, 151); 
       googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description")); 

       // For zooming automatically to the location of the marker 
       CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build(); 
       googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
      } 
     }); 

     return rootView; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mMapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mMapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mMapView.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 
} 

Ausgangsfragment;

public class HomeFragment extends RootFragment { 

    public HomeFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     // Obtain the shared Tracker instance. 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.home_tab, container, false); 

     Button go = (Button)v.findViewById(R.id.btn_go); 
     go.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Intent intent = new Intent(getActivity(), MapFragment.class); 
       //startActivity(intent); 
       enterNextFragment(); 
      } 
     }); 
     return v; 
    } 

    @Override 
    public void onResume() { 

     super.onResume(); 
    } 
    private void enterNextFragment() { 
     // Pushing MapView Fragment 
     Fragment fragment = Fragment.instantiate(this.getContext(), MapFragment.class.getName()); 
     FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.replace(R.id.fragment_mainLayout, fragment); 
     ft.commit(); 
    } 

} 

Kartenlayout;

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

Ausgangsfragment;

<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=".HomeFragment" 
android:background="@android:color/white" 
android:id="@+id/fragment_mainLayout"> 

<!-- TODO: Update blank fragment layout --> 


<android.support.v7.widget.AppCompatButton 
    android:id="@+id/btn_go" 
    android:layout_width="fill_parent" 
    android:layout_marginRight="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="170dp" 
    android:text="GO"/> 

</FrameLayout> 
+0

Haben Sie meine Antwort? – ziLk

+0

Ich löste den Fall, indem ich die Karte nicht als Fragment verwendete. Ich habe eine andere Ansicht erstellt und sie von einer Aktivität aus aufgerufen. Wenn der Benutzer die Schaltfläche "Los" drückt, wird eine andere Aktivität gestartet, die die aktuelle Ansicht nicht ersetzt. –

Antwort

0

Oft werden Sie ein Fragment wollen mit anderen zu kommunizieren, zum Beispiel den Inhalt auf ein Benutzerereignis basiert zu ändern. Alle Die Kommunikation von Fragment zu Fragment erfolgt über die zugehörige Aktivität . Zwei Fragmente sollten niemals direkt kommunizieren.

Ihre Fragmente sollten nicht direkt gemäß the documentation kommunizieren.

Wenn Sie unter Berücksichtigung der Dokumentation mit Ihren Fragmenten kommunizieren konnten. Dann können Sie diese einfache Methode verwenden, um ein Fragment zu ändern und wiederzuverwenden.

In Ihrem Host-Aktivität:

private void changeFragment(Fragment frag, boolean saveInBackstack) { 
    String backStateName = ((Object) frag).getClass().getName(); 

    try { 
     FragmentManager manager = getSupportFragmentManager(); 

     if (manager.findFragmentByTag(backStateName) == null) { 
      //fragment not in back stack, create it. 
      FragmentTransaction transaction = manager.beginTransaction(); 
      transaction.replace(R.id.container, frag, backStateName); 

      if (saveInBackstack) { 
       Log.d(TAG, "Change Fragment: addToBackTack " + backStateName); 
       transaction.addToBackStack(backStateName); 
      } else { 
       Log.d(TAG, "Change Fragment: NO addToBackTack"); 
      } 

      transaction.commit(); 
     } else { 
      manager.popBackStack(); 
     } 
    } catch (IllegalStateException exception) { 
     Log.w(TAG, "Unable to commit fragment, could be activity as been killed in 
    } 
} 
+0

Dies ist nicht die Antwort, die ich suchte und löste meinen Fall nicht. –

0

fügen Sie einfach android:background="@android:color/white" auf der Hauptansicht der map_layout xml Datei

0

Hier ist die Lösung;

Ausgangsfragment;

public class HomeFragment extends RootFragment { 
    private LocationManager locationManager; 

    public HomeFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     // Obtain the shared Tracker instance. 
     locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.home_tab, container, false); 
     Button go = (Button) v.findViewById(R.id.btn_go); 
     go.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getActivity(), MapFragment.class); 
       if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        //Toast.makeText(getContext(), "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show(); 
        startActivity(intent); 
       } 
      } 
     }); 
     return v; 
    } 

    @Override 
    public void onResume() { 

     super.onResume(); 
    } 
} 

MapFragment (es ist jetzt fragmentActivity nicht-Fragment);

public class MapFragment extends FragmentActivity implements OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    MapView mMapView; 
    private GoogleMap googleMap; 
    // private GoogleMap map; 
    private LocationRequest mLocationRequest; 
    private GoogleApiClient mGoogleApiClient; 
    private Location mLastLocation; 
    private Marker marker; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     mMapView = (MapView) findViewById(R.id.mapView); 
     mMapView.onCreate(savedInstanceState); 

     mMapView.onResume(); // needed to get the map to display immediately 

     try { 
      MapsInitializer.initialize(this.getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mMapView.getMapAsync(this); 



    } 

    @Override 
    public void onMapReady(GoogleMap mMap) { 
     googleMap = mMap; 
     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     Location location = null; 
     try { 
      googleMap.setMyLocationEnabled(true); 
      location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 

     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } 


     if (location != null) { 
      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13)); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude()))  // Sets the center of the map to location user 
        .zoom(17)     // Sets the zoom 
        .bearing(90)    // Sets the orientation of the camera to east 
        .tilt(40)     // Sets the tilt of the camera to 30 degrees 
        .build();     // Creates a CameraPosition from the builder 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     } 
    } 
} 
Verwandte Themen