2011-11-17 14 views
35

Po prostu zacząłem od projektowania fragmentów dla HoneyComb. Stworzyłem dwa fragmenty. Kiedy klikam przycisk w lewym fragmencie strony, po prawej stronie tworzony jest nowy fragment. Tymczasem po kliknięciu przycisku w odpowiednim fragmencie (w moim kodu poniżej tj. DetialsFragment powinien zostać zastąpiony przez innego fragmentu. main.xmlAndroid zastępuje bieżący fragment innym fragmentem

<?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:orientation="horizontal" > 
    <fragment class="com.fragment.example.Titles" 
     android:id="@+id/titles" android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 
    <FrameLayout android:id="@+id/details" android:layout_weight="1" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

FragmentExample.java

public class FragmentExample extends Activity { 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

Titles.java

public class Titles extends Fragment { 
    public FragmentTransaction ft; 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.main1, null); 
     Button button1 = (Button)v.findViewById(R.id.button1); 
     button1.setText("santhosh"); 
     button1.setOnClickListener(new OnClickListener() { 



      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       DetailsFragment details = (DetailsFragment) 
          getFragmentManager().findFragmentById(R.id.details); 
        if (details == null || details.getShownIndex() != 1) { 
         // Make new fragment to show this selection. 
         details = DetailsFragment.newInstance(1); 

         // Execute a transaction, replacing any existing 
         // fragment with this one inside the frame. 
         ft 
           = getFragmentManager().beginTransaction(); 
         ft.add(R.id.details, details, "detail"); 
         ft.setTransition(
           FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
         ft.commit(); 
        } 
      } 

     }); 
     return v; 
    } 
} 

DetailsFragment.java

public class DetailsFragment extends Fragment { 
    /** 
    * Create a new instance of DetailsFragment, initialized to 
    * show the text at 'index'. 
    */ 
    Titles title = new Titles(); 
    String[] titles = {"Title1", "Title2", "Title3", "Title4"}; 
    public static DetailsFragment newInstance(int index) { 
     DetailsFragment f = new DetailsFragment(); 

     // Supply index input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("index", index); 
     f.setArguments(args); 

     return f; 
    } 

    public int getShownIndex() { 
     return getArguments().getInt("index", 0); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      // Currently in a layout without a container, so no 
      // reason to create our view. 
      return null; 
     } 
     Button button = new Button(getActivity()); 
     button.setText("Next"); 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      } 
     }); 
     return button; 
    } 
} 

Odpowiedz

83

Następnie umieszczono Twój przycisk jest pokazywanie i zdarzenie kliknięcia wyrzuceniu można wywołać następujące w Twojej click imprezy:

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
ft.commit(); 

i jeśli chcesz wrócić do DetailsFragment na kliknięcie powrotem zapewnić dodasz powyższa transakcja do tylnego stosu, tj.

ft.addToBackStack(null); 

A może czegoś brakuje? Ewentualnie niektórzy sugerują, że twoja aktywność dostaje kliknięcie na przycisk i odpowiada za zastąpienie fragmentów w okienku szczegółów.

+3

Podanie znacznika fragmentu jest opcjonalne. Możesz zrobić 'ft.replace (R.id.details, new NewFragmentToReplace());' –

+0

Nie zapomnij również o 'getFragmentManager(). ExecutePendingTransactions();' –

+2

Przyciski nie znikają z poprzedniego fragmentu. Przyciski starego fragmentu są nadal widoczne na górze nowego fragmentu. – Santanu

0

Możesz spróbować poniżej kodu. to bardzo łatwa metoda wypychania nowego fragmentu ze starego fragmentu.

private int mContainerId; 
private FragmentTransaction fragmentTransaction; 
private FragmentManager fragmentManager; 
private final static String TAG = "DashBoardActivity"; 

public void replaceFragment(Fragment fragment, String TAG) { 

    try { 
     fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(mContainerId, fragment, tag); 
     fragmentTransaction.addToBackStack(tag); 
     fragmentTransaction.commitAllowingStateLoss(); 

    } catch (Exception e) { 
     // TODO: handle exception 
    } 

} 
+0

co to jest mcontainerid – Ayoub

+0

@Ayoub to widok u .. przykład example linearLayout.getId() w java – marlonpya

0

Zastosowanie android.support.v4.app dla FragmentManager & FragmentTransaction w kodzie, to pracował dla mnie.

DetailsFragment detailsFragment = new DetailsFragment(); 
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(R.id.details,detailsFragment); 
fragmentTransaction.commit(); 
0

To bardzo proste, jak zastąpić fragmentem.

DataFromDb changeActivity = new DataFromDb(); 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.changeFrg, changeActivity); 
    transaction.commit(); 
Powiązane problemy