FragmentMap + ActionBar Tab

后端 未结 4 1381
忘了有多久
忘了有多久 2021-02-10 07:47

I\'ve been trying insert a MapView into an ActionBar Tab, but I wasn\'t able to solve the problem even googling.

Here is the Main

相关标签:
4条回答
  • 2021-02-10 08:24

    Google's Maps API Library for Android doesn't support Fragments right now.

    0 讨论(0)
  • 2021-02-10 08:29

    Solution is to start mapview in the main activity which should extend FragmentMapActivity. Then your fragment can implement the mapview by getting it from the main activity.

    Main activity:

        mMapView = new MapView(YourActivity.this, MAPS_KEY);
        mMapView.setClickable(true);
        Exchanger.mMapView = mMapView;
    

    Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMapView = Exchanger.mMapView;
        return mMapView;
    }
    
    @Override
    public void onDestroy() {
        if(mMapView != null) {
            NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) mMapView.getParent();
            parentView.removeView(mMapView);
        }
        super.onDestroy();
    }
    

    Exchanger is a class with a static field with mapview in it.

    0 讨论(0)
  • 2021-02-10 08:30

    I've solved this issue changing the onCreateView method:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_geo, container, false);
        }
        return view;
    }
    

    where view is a View object.

    0 讨论(0)
  • 2021-02-10 08:31

    I use show/hide methods to avoid creating multiple MapView in a MapActivity :

    public class TabFragment extends SherlockFragment {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ActionBar actionBar = getSherlockActivity().getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            actionBar.addTab(actionBar
                .newTab()
                .setText("List")
                .setTabListener(
                    new TabListener(((FragmentActivity) getActivity()), "listFragment", ListFragment.class)));
    
            actionBar.addTab(actionBar
                .newTab()
                .setText("Map")
                .setTabListener(
                    new TabListener(((FragmentActivity) getActivity()), "MapFragment", MapFragment.class)));
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.tab_view, container, false);
        }
    
        public static class TabListener implements ActionBar.TabListener {
            private final FragmentActivity activity;
            private final String tag;
            private final Class clazz;
            private final Bundle args;
            private Fragment fragment;
    
            public TabListener(FragmentActivity activity, String tag, Class clz) {
                this(activity, tag, clz, null);
            }
    
            public TabListener(FragmentActivity activity, String tag, Class clz, Bundle args) {
                this.activity = activity;
                this.tag = tag;
                this.clazz = clz;
                this.args = args;
    
                // Check to see if we already have a fragment for this tab, probably from a previously saved state. If so,
                // hide it, because our initial state is that a tab isn't shown.
                fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
                if (fragment != null && !fragment.isHidden()) {
                    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                    ft.hide(fragment);
                    ft.commit();
                }
            }
    
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                if (fragment == null) {
                    fragment = Fragment.instantiate(activity, clazz.getName(), args);
                    ft.add(R.id.container_fragment, fragment, tag);
                } else {
                    ft.show(fragment);
                }
            }
    
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                if (fragment != null) {
                    ft.hide(fragment);
                }
            }
    
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题