2013-04-10 20 views
18

Ich habe eine Aktionsleiste mit 3 Tabs, jedes Tab öffnet ein Fragment. Die dritte Registerkarte „Katalog“, hat eine Liste: enter image description hereFragmente, die sich überlappen

Wenn ich auf ein Element klicken, um es ein weiteres Fragment öffnet, die nicht Teil der Aktionsleiste ist:

public void onClick(View v) { 
    switch (v.getId()) 
    { 
    case R.id.category1:  
     Fragment cosmeticsFragment = new ActivityCosmetics(); 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

     transaction.replace(android.R.id.content, cosmeticsFragment); 
     transaction.addToBackStack(null); 

     transaction.setTransition(1); 

     transaction.commit(); 
     break; 
     ... 

Dies ist, wie es aussieht wie danach: enter image description here

Von diesem Punkt, wenn ich auf andere Reiter gehen und dann auf die Registerkarte Katalog zurückkehren, sehe ich die zwei überlappende vorherigen Fragmente einander:

enter image description here

Wie verhindere ich, dass es passiert?

Antwort

4

Sie können Ihre Fragmente verwalten, indem Sie sie nach Tags durchsuchen. Wenn Fragment Hinzufügen

TAG Name
transaction.addToBackStack("myCustomFragmentTag"); 

zu Backstack hinzufügen Wenn Sie Fragment überall in Anwendung zerstören wollen:

Fragment previousInstance = getFragmentManager().findFragmentByTag("myCustomFragmentTag"); 
       if (previousInstance != null) 
        transaction.remove(previousInstance); 

Sie versuchen, ein bestimmtes Verhalten außer Kraft setzen kann, um diese Zeile Code ‚letzte Fragment initialisiert destroy ll

getFragmentManager().popBackStack(); 
+0

Ich habe versucht, es wie folgt zu verwenden: 'transaction.addToBackStack (" CategoryFragment ");' und dann versuchte, den Rest des Codes in onResume und onTabReselected Methoden zu verwenden. Aber ich sah immer noch diese Fragmente überlappen ... – Igal

+0

Ich habe das gleiche Problem in meiner Demo-App ... – Jayesh

+0

zeigen Sie mir Ihren Code in pastebin.com .... – Jayesh

0

Sie mögen dies für Tabs wieder ..

public class Tabs extends TabActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tabs); 
    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    // Intent intent; // Reusable Intent for each tab 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    Intent intent1 = new Intent().setClass(this, Social.class); 
    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("app_name").setIndicator("Practice", 
         res.getDrawable(R.drawable.tab_social)) 
        .setContent(intent1); 
    tabHost.addTab(spec); 
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.bbgg); 
    // Do the same for the other tabs 
    Intent intent2 = new Intent().setClass(this, Web.class); 
    spec = tabHost.newTabSpec("application").setIndicator("Application", 
         res.getDrawable(R.drawable.tab_web)) 
        .setContent(intent2); 
    tabHost.addTab(spec); 
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bbgg); 
    Intent intent3 = new Intent().setClass(this, Catalog.class); 
    spec = tabHost.newTabSpec("toplinks").setIndicator("Top Links", 
         res.getDrawable(R.drawable.tab_catalog)) 
        .setContent(intent3); 
    tabHost.addTab(spec); 
    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.bbgg); 
    tabHost.setCurrentTab(0); 
} 
} 

In Ihrem Layout xml

<TabHost 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

    <LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
    <TabWidget 
android:id="@android:id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></TabWidget> 
<FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"></FrameLayout> 
</LinearLayout> 
</TabHost> 
3

einen Hintergrund Fragment-Layout einstellen würde dieses Problem beheben.

+0

Dies sollte ein Kommentar sein. –

+0

android: background = "? Android: colorBackground" –

+0

müssen Sie auch unten hinzufügen, damit Klicks nicht an das alte Fragment übergeben werden ==> android: clickable = "true" –

Verwandte Themen