2012-01-11 18 views
5

के बाद टैबहोस्ट फ्रैगमेंट में मैपएक्टिविटी गायब हो रही है, मैं टुकड़ों का उपयोग करके टैब के एक सेट में एक नक्शा प्रदर्शित करने की कोशिश कर रहा हूं। मेरी समस्या यह है कि यदि उपयोगकर्ता किसी अन्य खंड में वापस जाता है तो नक्शा गतिविधि गायब हो जाती है। मैं टैबहोस्ट में टुकड़ों में मैपएक्टिविटी कैसे प्रदर्शित कर सकता हूं?टैब स्विच

इस के लिए आधार कैसे टुकड़े के साथ एक MapActivity एकीकृत करने के लिए आसपास कई सवालों से है (MapView in a Fragment (Honeycomb) देखें)

MainTabActivity एक tabhost है और टैब में टुकड़े प्रदर्शित करने के लिए FragmentManager उपयोग करता है। मैपफ्रैगमेंट में एक टैबहोस्ट है और मैपएक्टिविटी प्रदर्शित करने के लिए इसका उपयोग करता है।
मैपफ्रैगमेंट स्थानीय गतिविधि प्रबंधक का उपयोग जीवन चक्र घटनाओं को निहित गतिविधि में प्रचारित करने के लिए करता है।

तो MapFragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.map_fragment, container, false); 
     mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); 
     mTabHost.setup(getLocalActivityManager()); 
     Intent intent = new Intent(getActivity(), MapActivityWrapper.class); 
//  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//does nothing 
     TabHost.TabSpec tab = mTabHost.newTabSpec("map") 
       .setIndicator("map") 
       .setContent(intent); 
     mTabHost.addTab(tab); 
     return view; 
    } 

MapFragment लेआउट:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 
    <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:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <TabWidget android:id="@android:id/tabs" 
        android:visibility="gone" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 
      <FrameLayout android:id="@android:id/tabcontent" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

टैब सामग्रियों को परिवर्तित करने के लिए MainTabActivity में कोड:

private void updateTab(String oldId, String tabId) { 
    FragmentManager fm = getSupportFragmentManager(); 
    Fragment old = fm.findFragmentByTag(oldId); 
    Fragment selected = fm.findFragmentByTag(tabId); 
    FragmentTransaction ft = fm.beginTransaction(); 
    boolean added = false; 
    if (selected == null) { 
     selected = createFragment(tabId); 
    } else { 
     try { 
      ft.remove(selected); 
     } catch (Exception e) { 
      ft.add(android.R.id.tabcontent, selected, tabId); 
      added = true; 
      //exception for removing an unadded fragment... why throw an exception? 
     } 
    } 
    if (old != null) ft.remove(old); 
    if (!added) ft.replace(android.R.id.tabcontent, selected, tabId); 
    if (!creating && !backStackProcessing) ft.addToBackStack(null);//member vars 
    ft.commit(); 
} 

MainTabActivity लेआउट (असंबंधित लेआउट का एक समूह को नष्ट कर दिया):

<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" 
     android:background="#EFEFEF"> 
    <RelativeLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_gravity="top" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/app_default_bg" 
       android:layout_above="@+id/ad_region" 
       android:layout_below="@+id/quick_action_region"> 
      <FrameLayout android:id="@+id/tab1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
      <FrameLayout android:id="@+id/tab2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
      <FrameLayout android:id="@+id/map_tab" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
      <FrameLayout android:id="@+id/tab3" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 
     </FrameLayout> 
     <TabWidget android:layout_width="fill_parent" 
        android:id="@android:id/tabs" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true"/> 
    </RelativeLayout> 
</TabHost> 

फिर से - मेरी समस्या यह है कि यदि उपयोगकर्ता किसी अन्य खंड में वापस जाता है तो नक्शा गतिविधि गायब हो जाती है। मैं टैबहोस्ट में टुकड़ों में मैपएक्टिविटी कैसे प्रदर्शित कर सकता हूं?

बहुत धन्यवाद।

+0

क्या आपने इस समस्या को हल करने का अंत किया? मैं एक ही समस्या के खिलाफ दौड़ रहा हूँ ... – Rockmaninoff

उत्तर

5

बना लिया है मैं के रूप में इस समस्या के काम की वेक्टर है निम्नानुसार है ...

मेरे पास कक्षा स्तर परिवर्तक था:

private View mMapViewContainer; 

मैं नक्शा युक्त इस प्रकार टैब बनाया गया (जहां MyMapActivity MapActivity टैब में प्रदर्शित है और mTabHost TabHost है) मेरी टुकड़ा की onViewCreated विधि में:

// Map tab 
Intent intent = new Intent(getActivity(), MyMapActivity.class); 
Window window = mLocalActivityManager.startActivity(MAP_ACTIVITY_TAG, intent); 
mMapViewContainer = window.getDecorView(); 
mMapViewContainer.setVisibility(View.VISIBLE); 
mMapViewContainer.setFocusableInTouchMode(true); 
((ViewGroup) mMapViewContainer).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 

View tab = getActivity().getLayoutInflater().inflate(R.layout.tab_background, null); 
((TextView) tab.findViewById(R.id.tv_tab_title)).setText(getResources().getString(R.string.map)); 
TabSpec tabSpec = mTabHost.newTabSpec(MAP_TAB_TAG).setIndicator(tab).setContent(new TabContentFactory() { 

    @Override 
    public View createTabContent(String tag) { 
     return mMapViewContainer; 
    } 
}); 
mTabHost.addTab(tabSpec); 

और फिर टुकड़े में onStop

@Override 
public void onStop() { 
    super.onStop(); 

    ((ViewGroup) mMapViewContainer.getParent()).removeView(mMapViewContainer); 
} 

अब नक्शा फिर से प्रदर्शित किया जाता है उपयोगकर्ता वापस एक और टुकड़ा पर जाने के बाद जब:() विधि मैं निम्नलिखित किया था।

+0

संयोग से, मेरे मैप व्यूकंटनर को बनाने के लिए इस्तेमाल किया जाने वाला कोड इस उत्तर के टिप्पणी थ्रेड में @ क्रिस्टोफरके को जमा किया जाना चाहिए: http://stackoverflow.com/a/8126287/447197 – tafty

3

यदि आप अपनी परियोजना में ViewPager इस्तेमाल किया, फोन setOffscreenPageLimit() यह नीचे दिखाया गया है पसंद के लिए:

this.mViewPager.setOffscreenPageLimit(fragments.size());

टुकड़े जहां टुकड़े आप

+0

बहुत बहुत धन्यवाद! यह मुझे HOURS के लिए परेशान कर रहा था और अंततः मुझे समस्या का समाधान मिल गया जहां मेरे टुकड़े दो स्विच के बाद गायब हो गए थे! आपको बहुत - बहुत धन्यवाद! – Ruuhkis

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