2013-04-10 11 views
18

मैं 3 टैब के साथ एक एक्शन बार है ओवरलैपिंग, प्रत्येक टैब एक टुकड़ा खोलता है। तीसरे टैब, "सूची", एक सूची है: enter image description hereटुकड़े एक दूसरे को

जब मैं एक आइटम यह एक और टुकड़ा खोलता है, जो कार्रवाई बार का हिस्सा नहीं है पर क्लिक करें:

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; 
     ... 

यह यह क्या दिखता है जैसे कि के बाद:

0: enter image description here

इस बिंदु से, अगर मैं अन्य टैब पर जाएँ और फिर कैटलॉग टैब पर लौटने, मैं 2 पिछले टुकड़े एक दूसरे को ओवरलैप करने को देखने के

मैं इसे कैसे होने से रोकने करते हैं?

उत्तर

4

आप उन्हें टैग द्वारा खोज करने के द्वारा टुकड़े प्रबंधन कर सकते हैं। जब टुकड़ा जोड़ने

transaction.addToBackStack("myCustomFragmentTag"); 

जोड़ने टैग नाम backstack करने के लिए आप आवेदन में कहीं भी टुकड़ा नष्ट करने के लिए करना चाहते हैं:

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

आप कुछ व्यवहार को ओवरराइड कोशिश कर सकते हैं ताकि कोड की 'इस लाइन पिछले टुकड़ा

प्रारंभ नष्ट करूँगा
getFragmentManager().popBackStack(); 
+0

मैं इस प्रकार के रूप में उपयोग करने की कोशिश की: 'transaction.addToBackStack (" CategoryFragment ");' और फिर onResume में कोड के बाकी का उपयोग करने की कोशिश की और onTabReselected तरीकों। लेकिन मैं अभी भी इन टुकड़ों अतिव्यापी देखा ... – Igal

+0

मैं अपने डेमो अनुप्रयोग में एक ही मुद्दा है ... – Jayesh

+0

मुझे pastebin.com में अपने कोड दिखाने .... – Jayesh

0

.. आप टैब के लिए इस तरह की कोशिश कर सकते

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

अपने लेआउट एक्सएमएल

<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

में टुकड़ा लेआउट के लिए एक पृष्ठभूमि की स्थापना इसे ठीक होगा।

+0

यह एक टिप्पणी किया जाना चाहिए। –

+0

एंड्रॉयड: पृष्ठभूमि = "एंड्रॉयड: colorBackground" –

+0

भी आप ==> एंड्रॉयड नीचे जोड़ने के लिए इसलिए क्लिक अभ्यस्त वर्ष टुकड़ा के लिए पारित हो: क्लिक करने योग्य = "true" –

1

इस सवाल के अनुसार: Android: fragments overlapping issue

आप सभी की जरूरत सिर्फ एक्सएमएल फ़ाइल

में अपने लिए एक पृष्ठभूमि रंग सेट करने के लिए इस समस्या का समाधान है।

संबंधित मुद्दे