2012-05-02 65 views
7

Jestem całkiem nowy na Androida, więc może to być oczywista odpowiedź, ale nie mogę tego znaleźć.Android: Dodawanie fragmentu do działania

Piszę wersję aplikacji kalkulatora. Chcę, aby tak, że gdy użytkownik kliknie jeden z przycisków, rozpoczyna się fragment (z większą liczbą przycisków, które mogą następnie użyć, aby uzyskać więcej danych wejściowych).

Kod fragment brzmi następująco:

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class VariableMenu extends Fragment { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);} 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.fragment, container, false); 
} 

} 

z układu XML w następujący sposób:

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

<Button 
     android:id="@+id/Bx" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="X" 
     android:onClick="onButtonClicked"/> 
    <Button 
     android:id="@+id/By" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/Bx" 
     android:layout_alignTop="@id/Bx" 
     android:text="Y" 
     android:onClick="onButtonClicked"/> 
    <Button 
     android:id="@+id/Bz" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/By" 
     android:layout_alignTop="@id/By" 
     android:text="Z" 
     android:onClick="onButtonClicked"/> 

</RelativeLayout> 

(wiem, że nie należy używać twardej zakodowane ciągi, ale będę naprawię to, gdy tylko coś działa)

Próbowałem dodać go za pomocą fragmenttransaction z metodą onTouch przycisku aktywującego, tak jak:

public void onFragmentActivated(View v) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     VariableMenu fragment = new VariableMenu(); 
     fragmentTransaction.add(R.layout.main, fragment); 
     fragmentTransaction.commit(); 
    } 

ale spowodowało to błąd mówiąc, że "Nie znaleziono widoku dla id: ... (main) dla fragmentu VariableMenu."

Więc zrobiłem pogląd na to w głównym pliku XML:

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

(Irrelevant things) 

    <fragment 
     android:id="@+id/Fragment" 
     android:name="calculator.app.VariableMenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/B1"/> 

(Irrelevant things) 

</RelativeLayout> 

Ale spowodowało to fragment jest tam tak szybko, jak aktywność powstał (i setContentView (R.layout.main) ; biegł).

Czy istnieje prosty sposób programowego dodawania i usuwania fragmentów za pomocą interfejsu użytkownika do działania?

Odpowiedz

11

Problem polega na tym, że fragment potrzebuje kontenera z identyfikatorem widoku. Podanie identyfikatora układu spowoduje błąd, który zobaczysz.

Możesz dodać fragment do układu względnego, ale aby to zrobić poprawnie, musisz mu przypisać odpowiednie parametry układu, aby można go było umieścić. Łatwiej byłoby po prostu stworzyć FrameLayout w ramach względnego układu, który otacza jego zawartość, a następnie dodać tam fragment.

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

    <FrameLayout 
     android:id="@+id/FragmentContainer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/B1"/> 

</RelativeLayout> 

Następnie

fragmentTransaction.add(R.id.FragmentContainer, fragment); 
+1

+1. @Spencer również, jeśli twój fragment może działać jako osobne działanie, możesz je zmienić, aby rozszerzyć działanie FragmentActivity i traktować je jak zwykłą aktywność. – Joe

+0

Bardzo dziękuję, to zadziałało idealnie. Nie sądzę, by FragmentActivity działał w tym przypadku, ale będę miał na myśli tę opcję na przyszłość. – Spencer

Powiązane problemy