10

Je voudrais ajouter un fragment à une boîte de dialogue (il peut s'agir d'un dialogue DialogFragment ou d'un dialogue normal). Comment je fais ça?Ajout d'un fragment à une boîte de dialogue

Voici mon DialogFragment:

public class MyDialogFragment extends DialogFragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     MyDialogFragment2 dialog = new MyDialogFragment2(); 
     View v = inflater.inflate(R.layout.news_articles, container, false); 
     getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit(); 
     return v; 
    } 

} 

est ici news_article.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

Voici mon activité principale:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      MyDialogFragment dialog = new MyDialogFragment(); 
      dialog.show(getSupportFragmentManager(), "asdf"); 
     } 
    }); 
} 

Mais quand je l'essayer je reçois:

No view found for id 0x7f070002 for fragment MyDialogFragment2 

Je pense que c'est parce que le FragmentManager de l'activité n'est pas celui que je devrais ajouter, mais je ne trouve pas celui du DialogFragment, où est-ce? .

+2

'getChildFragmentManager(). BeginTransaction() ....' – Luksprog

+0

Merci, mais cela fonctionne uniquement avec l'API 17, n'est-ce pas? – Kalisky

+1

Avec les fragments natifs oui comme ils ont été introduits à partir de 4.2. Mais vous avez toujours l'option des fragments du package de compatibilité de support qui fonctionne avec la même méthode 'getChildFragmentManager()'. – Luksprog

Répondre

7

Mise en page pour Dialog - R.layout.view_with_plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.util.me.TestActivity" 
    > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Details"/> 
    <fragment 
     android:layout_toRightOf="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/email" 
     class="com.util.me.test.PlusOneFragment" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

Comment afficher la boîte de dialogue

public void showDialog(View vIew){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null); 
    builder.setView(view) 
      .setPositiveButton("OK", null) 
      .setNegativeButton("Cancel", null); 

    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 

Le fragment dans class = "com.util.me.test.PlusOneFragment" est simplement un PlusOneFragment généré par Android Studio.

Questions connexes