2013-01-31 19 views
12

Ho un'attività con pulsante su di esso e quando un clic chiamiamo il frammento con un altro pulsante sul frammento. ma quando clicco sul pulsante del frammento non posso chiamare un secondo frammento. questa è la mia fonte, piuttosto semplice: activity_main.xml:Frammento di chiamata da frammento

<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); 
    } 
} 

ciò che è sbagliato nel mio codice?

risposta

24

penso che ora Frammento di nidificazione è disponibile solo aggiornare il barattolo computabilità posteriore

ora consente di scavare nel problema è di per sé.

public void onClick2(View view) { 
    Fragment2 fragment2 = new Fragment2(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment1, fragment2); 
    fragmentTransaction.commit(); 
} 

penso che il R.id.fragment1 appartiene ad una TextView che non è un buon posto per includere viste figlio in quanto non è un ViewGroup sua, è possibile rimuovere il textView dal xml e sostituirlo con un LinearLayout permette di dire e funzionerà, se non mi dite quale errore.

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" > 

<LinearLayout 
    android:id="@+id/fragment1" 
    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> 

Aggiornamento per l'errore nel commento

public class Fragment1 extends Fragment implements OnClickListener{ 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment1, container, false); 
((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this); 
    return v; 
} 

public void onClick(View view) { 
    Fragment2 fragment2 = new Fragment2(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.container, fragment2); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 

} 
+0

Non funziona. Sostituisco TextView con LinearLayout, il primo frammento si apre bene, ma quando si preme il pulsante su quel frammento per aprire il secondo l'app si ferma con errore Impossibile trovare un metodo onClick2 (View) nella classe di attività com.example.fragments.MainActivity per gestore onClick su classe vista android.widget.Button con id 'btn_frag2' –

+0

mostrami lo stacktrace dell'errore – confucius

+1

vedi il mio aggiornamento .... – confucius

5

Se si desidera sostituire l'intero Fragment1 con Fragment2, è necessario farlo all'interno MainActivity, utilizzando:

Fragment1 fragment2 = new Fragment2(); 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(android.R.id.content, fragment2); 
fragmentTransaction.commit(); 

Basta inserire questo codice all'interno di un metodo in MainActivity, quindi chiamare quel metodo da Fragment1.

+0

con questo, quando ho ruotare il telefono ho perso il secondo frammento, e la prima uno arriva sullo schermo –

0

userei un ascoltatore. Fragment1 chiama l'ascoltatore (attività principale)

1

Questa è la mia risposta che ha risolto lo stesso problema. Fragment2.java è la classe dei frammenti che contiene il layout di fragment2.xml.

public void onClick(View v) { 
        Fragment fragment2 = new Fragement2(); 
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        fragmentTransaction.replace(R.id.frame_container, fragment2); 
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit(); 
       } 
-2

Basta fare che: getTabAt (indice della scheda)

 ActionBar actionBar = getSupportActionBar(); 
     actionBar.selectTab(actionBar.getTabAt(0)); 
0

Questo codice funziona per me di chiamare il metodo parent_fragment da child_fragment.

ParentFragment parent = (ParentFragment) activity.getFragmentManager(). FindViewById (R.id.contaniner);

parent.callMethod();

-1
Fragment fragment = new Fragment(); 
FragmentManager fm = getActivity().getSupportFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 
ft.replace(R.id.content_frame, fragment); 
ft.commit(); 
0

In MainActivity

private static android.support.v4.app.FragmentManager fragmentManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);      

    fragmentManager = getSupportFragmentManager();                 


    } 

public void secondFragment() { 

    fragmentManager 
      .beginTransaction() 
      .setCustomAnimations(R.anim.right_enter, R.anim.left_out) 
      .replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null) 
      .commit(); 
} 

In FirstFragment chiamata SecondFrgment Così:

new MainActivity().secondFragment(); 
Problemi correlati