2013-08-11 4 views
15

मैं एकाधिक टैब के साथ एक FragmentTabHost का उपयोग कर रहा हूं (here दिखाया गया है)। हालांकि, मैं अपने टैब को यादृच्छिक रूप से GetFragmentByTag (जो उस मामले में वापस लौटाता है) के साथ याद नहीं कर सकता है जब तक कि कम से कम एक बार टैब पर क्लिक करके संबोधित टैब सक्रिय नहीं किया जाता है।FragmentTabHost - टैब को पहली बार देखने के लिए एड्रेस करने योग्य नहीं है

FragmentTabHost टैब की रचना में देरी होने में प्रतीत होता है जब तक कि हमें वास्तव में उनकी आवश्यकता नहीं होती है (उर्फ उपयोगकर्ता उस पर क्लिक करता है और इसे देखना चाहता है)।
क्या होस्ट को तत्काल बनाने के लिए मजबूर करने का कोई तरीका है जैसे कि मैं सुरक्षित रूप से getFragmentByTag द्वारा उन्हें एक्सेस कर सकता हूं?
या टैब को "अपने आप पर" बनाना संभव है और बस उन्हें टैबहोस्ट में जोड़ें?

+0

यदि आप शुरू में फोन 'onTabChanged (tabId)' हर टैब के लिए (वर्तमान से एक के लिए स्विच करने से पहले), तो यह है कि 'FragmentTabHost' बाध्य करेगा पूर्व सभी टुकड़ों को दोहराएं। यदि आप खंड कंटेनर की आईडी प्राप्त करते हैं, तो आप इसे स्वयं भी कर सकते हैं। वैकल्पिक रूप से, आप अपने वांछित व्यवहार को लागू करने के लिए इसे बढ़ा सकते हैं या फोर्क कर सकते हैं। – corsair992

उत्तर

5

क्या मेजबान को तुरंत उन्हें बनाने के लिए मजबूर करने का कोई तरीका है कि मैं सुरक्षित रूप से getFragmentByTag द्वारा उन्हें एक्सेस कर सकता हूं?

संख्या क्योंकि लेनदेन onAttachedToWindow() पर निष्पादित किया गया है। जैसा कि आप देख mFragmentManager.executePendingTransactions();onAttachedToWindow पर निष्पादित किया जाता है

@Override 
protected void onAttachedToWindow() { 
    super.onAttachedToWindow(); 
    String currentTab = getCurrentTabTag(); 
    // Go through all tabs and make sure their fragments match. 
    // the correct state. 
    FragmentTransaction ft = null; 
    for (int i=0; i<mTabs.size(); i++) { 
     TabInfo tab = mTabs.get(i); 
     tab.fragment = mFragmentManager.findFragmentByTag(tab.tag); 
     if (tab.fragment != null && !tab.fragment.isDetached()) { 
      if (tab.tag.equals(currentTab)) { 
       // The fragment for this tab is already there and 
       // active, and it is what we really want to have 
       // as the current tab. Nothing to do. 
       mLastTab = tab; 
      } else { 
       // This fragment was restored in the active state, 
       // but is not the current tab. Deactivate it. 
       if (ft == null) { 
        ft = mFragmentManager.beginTransaction(); 
       } 
       ft.detach(tab.fragment); 
      } 

     } 
    } 
    // We are now ready to go. Make sure we are switched to the 
    // correct tab. 
    mAttached = true; 
    ft = doTabChanged(currentTab, ft); 
    if (ft != null) { 
     ft.commit(); 
     mFragmentManager.executePendingTransactions(); 
    } 
} 
@Override 
protected void onDetachedFromWindow() { 
    super.onDetachedFromWindow(); 
    mAttached = false; 
} 

: source code पर एक नजर है की सुविधा देता है।

या क्या टैब "मेरे अपने" पर बनाना संभव है और बस उन्हें टैबहोस्ट में जोड़ें?

हाँ, आप tabhost का उपयोग कर सकते हैं और आप नीचे विधियों के साथ टैब सामग्री बना सकते हैं।

public TabHost.TabSpec setContent (int viewId)

public TabHost.TabSpec setContent (Intent intent)

public TabHost.TabSpec setContent (TabHost.TabContentFactory contentFactory)

+1

मेरे लिए भी उपयोगी है 'FragmentTabHost.java' में' DoTabChanged' विधि में स्रोत कोड की समीक्षा कर रहा था। स्रोत कोड से जोड़ने के लिए धन्यवाद। –

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