2

J'ai un fragment parent dans ma vue dans lequel 2 fragments enfants sont ajoutés par programmation pour afficher des cartes. C'est la disposition pour le fragment enfant, la carte est rendue dans FrameLayout.Vue personnalisée Android et fragments imbriqués

location_field_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/settings_action_item_row_style" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="256dp" 
     android:id="@+id/map_fragment_container" 
     android:layout_marginTop="2dp"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="This is where map loads."/> 
    </FrameLayout> 
</LinearLayout> 

Il y a un événement click pour carte à charger dans FrameLayouts correspondant. Mon problème est, même si la vue FrameLayout est gonflée avec succès (comme je peux voir le texte de débogage "La carte charge ici" pour les deux fragments enfant) mais la carte pour le second enfant rend le fragment lui-même (je le vois rafraîchi lorsque le deuxième auditeur de clic de carte est déclenché). Qu'est-ce qui pourrait mal tourner ici?

classe pour gonfler au-dessus de la disposition qui est ensuite ajouté à fragment parent:

LocationField.java

public class LocationField extends LinearLayout 
{ 
    private MyMapFragment mMapFragment; 
    public LocationField() 
    { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.location_field_view, this, true); 
     addMapFragment(); 
     //mMapFragment.loadMap() method gets triggered based on a click event. 
    } 

    private void addMapFragment() 
    { 
     mMapFragment = MyMapFragment.newInstance(lat, long, marker); 
     FragmentTransaction transaction = mParentFragment.getChildFragmentManager().beginTransaction(); 
     transaction.add(R.id.map_fragment_container, mMapFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 
} 

MyMapFragment.java

public class MyMapFragment extends MapFragment implements OnMapReadyCallback 
{ 
    private static final String KEY_MARKER = "marker"; 
    private static final String KEY_LATITUDE = "latitude"; 
    private static final String KEY_LONGITUDE = "longitude"; 
    private LatLng mLatLng; 
    private String mMarker; 

    public static MyMapFragment newInstance(Double latitude, Double longitude, String marker){ 

     MyMapFragment myMapFragment = new MyMapFragment(); 
     Bundle arguments = new Bundle(); 
     arguments.putDouble(KEY_LATITUDE, latitude); 
     arguments.putDouble(KEY_LONGITUDE, longitude); 
     arguments.putString(KEY_MARKER, marker); 
     myMapFragment.setArguments(arguments); 
     return myMapFragment; 
    } 

    public void onCreate(Bundle savedInstance) 
    { 
     super.onCreate(savedInstance); 
     Bundle arguments = getArguments(); 
     mLatLng = new LatLng(arguments.getDouble(KEY_LATITUDE), arguments.getDouble(KEY_LONGITUDE)); 
     mMarker = arguments.getString(KEY_MARKER); 
    } 

    public void loadMap() 
    { 
     getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     if (googleMap == null) { 
      return; 
     } 
     googleMap.addMarker(new MarkerOptions().position(mLatLng).title(mMarker)); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, 10f)); 
    } 
} 

Répondre

1

résolu le problème en identifiant de réglage FrameLayouts pour mappez dynamiquement en utilisant la méthode generateViewId().

Il semble que FragmentManager ait écrasé les ID de trame des trames précédemment ajoutées. (Parce qu'ils étaient tous ayant la même ID ??)

Partage les modifications de code nécessaires pour résoudre le problème (dans le cas où quelqu'un d'autre face à ce problème):

  1. Générer ID pour la mise en page de cadre dynamique. Au départ, il y aura un identifiant pour la mise en page d'image dans votre vue. Récupère l'objet de vue et attribue l'ID de vue nouvellement généré.

    mMapFrameId = View.generateViewId();

  2. Utilisez le même ID pendant la transaction de fragment, tout en ajoutant un nouveau fragment.

    mMapFragment = new MapFragment(); 
    FragmentTransaction transaction = mFragmentManager.beginTransaction(); 
    transaction.add(mMapFrameId, mMapFragment); 
    transaction.addToBackStack(null); 
    transaction.commit();