Fragment d'appel de fragment
j'ai une activité avec bouton et lors d'un clic j'appelle le fragment avec un autre bouton sur le fragment. mais quand on clique sur fragment du bouton je ne peux pas appeler un deuxième fragment. c'est ma source, assez simple:
activity_main.xml:
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_click"
android:text="Call Fragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:onClick="onClick"
/>
</LinearLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment1"
android:text="Fragment 1"
android:textSize="25sp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="@+id/btn_frag2"
android:text="Call Fragment 2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment2"
android:text="Fragment 2"
android:textSize="25sp"
android:gravity="center_vertical|center_horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.commit();
}
}
Fragment1.java
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
public void onClick2(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
}
Fragment2.java
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
ce qui est faux dans mon code?
source d'informationauteur Jack Daniel
Vous devez vous connecter pour publier un commentaire.
Je pense que maintenant Fragment de nidification est disponible juste mettre à jour l'arrière computability pot
maintenant permet de creuser dans le problème lui-même .
Je pense que le
R.id.fragment1
appartient à uneTextView
qui n'est pas un bon endroit pour inclure les vues enfant parce que ce n'est pas unViewGroup
vous pouvez supprimer letextView
à partir du xml et de le remplacer par unLinearLayout
permet de dire et il va fonctionner , si ce n'est me dire quel est le message d'erreur .fragment1.xml
Mise à jour pour l'erreur dans le commentaire
Si vous souhaitez remplacer l'ensemble de la Fragment1 avec Fragment2, vous devez le faire à l'intérieur de MainActivity, en utilisant:
Viens de mettre ce code à l'intérieur d'une méthode dans MainActivity, puis appelez la méthode de Fragment1.
C'est ma réponse qui a résolu le même problème. Fragment2.java est le fragment de la classe qui détient la mise en page de fragment2.xml.
Je voudrais utiliser un écouteur. Fragment1 l'appel de l'auditeur (activité principale)
Ce code fonctionne pour moi d'appeler le parent_fragment méthode de child_fragment.
ParentFragment parent = (ParentFragment) de l'activité.getFragmentManager().findViewById(R. id.contaniner);
parent.callMethod();
Dans MainActivity
Dans FirstFragment appel SecondFrgment Comme ceci:
Viens de le faire: getTabAt(index de votre onglet)