2012-01-11 68 views
5

Tôi đang cố gắng hiển thị bản đồ trong một tập hợp các tab, sử dụng các đoạn. Sự cố tôi gặp phải là hoạt động trên bản đồ biến mất nếu người dùng điều hướng đến một đoạn khác, sau đó quay lại. Làm cách nào tôi có thể hiển thị MapActivity trong các đoạn trong tabhost?MapActivity trong TabHost Phân đoạn biến mất sau khi chuyển đổi tab

Cơ sở cho điều này là từ những câu hỏi xung quanh rất nhiều cách để tích hợp một MapActivity với mảnh (xem MapView in a Fragment (Honeycomb))

MainTabActivity có tabhost và sử dụng FragmentManager để hiển thị Những mảnh vỡ trong các tab. MapFragment có một tabhost và sử dụng nó để hiển thị MapActivity.
MapFragment sử dụng LocalActivityManager để thúc đẩy các sự kiện vòng đời đến hoạt động được chứa.

Vì vậy MapFragment: Bố cục

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> 

Các mã trong MainTabActivity cho việc thay đổi nội dung tab:

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

bố trí MainTabActivity (xóa một loạt các bố trí không liên quan):

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

Một lần nữa - Sự cố tôi gặp phải là hoạt động trên bản đồ biến mất nếu người dùng điều hướng đến một đoạn khác, sau đó quay lại. Làm cách nào tôi có thể hiển thị MapActivity trong các đoạn trong tabhost?

Cảm ơn nhiều.

+0

Bạn đã giải quyết xong vấn đề này chưa? Tôi đang chạy lên chống lại cùng một vấn đề ... – Rockmaninoff

Trả lời

5

tôi làm việc xung quanh vấn đề này như sau ...

Tôi có biến cấp lớp:

private View mMapViewContainer; 

tôi tạo ra các tab chứa bản đồ như sau (nơi MyMapActivity là MapActivity hiển thị trong tab và mTabHost là TabHost) trong phương pháp onViewCreated của Fragment tôi:

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

Và sau đó trong các mảnh vỡ onStop() phương pháp tôi đã làm như sau:

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

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

Bây giờ bản đồ được hiển thị lại khi người dùng điều hướng đến một đoạn khác rồi quay lại.

+0

Ngẫu nhiên, mã tôi đã sử dụng để tạo mMapViewContainer của mình sẽ được ghi có vào @ChristopherK trong chuỗi nhận xét của câu trả lời này: http://stackoverflow.com/a/8126287/447197 – tafty

3

Nếu bạn sử dụng ViewPager trong dự án của bạn, hãy gọi setOffscreenPageLimit() cho nó thích hiển thị dưới đây:

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

nơi mảnh vỡ là Vector bánh vụn bạn đã tạo

+0

Cảm ơn bạn rất nhiều! Điều này đã làm tôi thất vọng vì HOURS và cuối cùng tôi đã tìm ra giải pháp cho vấn đề mà các mảnh vỡ của tôi biến mất sau vài lần chuyển mạch! Cảm ơn bạn rất nhiều! – Ruuhkis

Các vấn đề liên quan