获取谷歌地图片段onCreateView使用动作条的标签 [英] Getting Google Map Fragment in onCreateView using ActionBar tabs

查看:122
本文介绍了获取谷歌地图片段onCreateView使用动作条的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时在动作条使用标签来获取谷歌地图的片段。当我加载标签页地图加载罚款,但我想要得到的地图对象,所以我可以居中,添加标记等。

有没有办法做到这一点,如果有,会有人这么好心给我看怎么样?

这里的code我的标签式网页...

具体方法我正在使用的公共静态类地图扩展片段

 公共类Page3Activity扩展FragmentActivity实现ActionBar.TabListener {

    最终的上下文语境=这一点;
    静态GoogleMap的地图;
    静态图形页面mMapView;
    数据库处理器DB =新的数据库处理器(上下文);

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    @燮pressLint(NewApi)
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.page3);

        //创建将返回一个片段为三个主要部分的适配器
        //应用程序的。
        mAppSectionsPagerAdapter =新AppSectionsPagerAdapter(getSupportFragmentManager());

        ColorDrawable colorDrawable =新ColorDrawable();
        最后的动作条动作条= getActionBar();
        colorDrawable.setColor(0xff9ACC00);
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setTitle(道之旅计算器);
        actionBar.setSubtitle(旅行信息);
        actionBar.setDisplayShowTitleEnabled(真正的);
        actionBar.setDisplayShowHomeEnabled(真正的);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

     //指定不应该启用主页/向上按钮,因为没有分级
        //父。
        actionBar.setHomeButtonEnabled(假);

        //指定,我们将在操作栏中显示的标签。
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //设置的ViewPager,将适配器,并设立一个监听器,当
        //部分之间的用户刷卡。
        mViewPager =(ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
            @覆盖
            公共无效onPageSelected(INT位置){
                //当不同的应用程序部分之间刷卡,选择相应的选项卡。
                //我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有一个参考
                //标签。
                actionBar.setSelectedNavigationItem(位置);
            }
        });

        //对于每个应用程序的章节中,添加一个标签,操作栏。
        的for(int i = 0; I< mAppSectionsPagerAdapter.getCount();我++){
            //创建与对应于由适配器中定义的页面标题文本标签。
            //还可以指定这个活动对象,它实现了TabListener接口,作为
            //监听器当选择该选项卡。
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(I))
                            .setTabListener(本));
        }

    }

    / *
     *动作条顶吧code
     * /


    @覆盖
    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    }

    @覆盖
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
        //当选择给定的标签,切换到在ViewPager相应页面。
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @覆盖
    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    }

    / **
     * A {@link FragmentPagerAdapter}返回对应于主要的一个片段
     *应用程序的部分。
     * /
    公共静态类AppSectionsPagerAdapter扩展FragmentPagerAdapter {

        公共AppSectionsPagerAdapter(FragmentManager FM){
            超(FM);
        }

        //标签之间的切换
        @覆盖
        公共片段的getItem(int i)以{
            开关(ⅰ){
                情况下0:
                    返回新地图();
                情况1:
                    返回新方向();
                案例2:
                    返回新poiList();
                案例3:
                    返回新tripInfo();
                默认:
                    I = I + 1;
            }
            返回null;
        }

        @覆盖
        公众诠释getCount将(){
            返回4;
        }

        @覆盖
        公共字符串getPageTitle(INT位置){
            字符串TABNAME =;
            如果(位置== 0){
                TABNAME =地图;
            }否则,如果(位置== 1){
                TABNAME =路线;
            }否则,如果(位置== 2){
                TABNAME =POI列表;
            }否则,如果(位置== 3){
                TABNAME =旅行信息;
            }
            返回TABNAME;

        }
    }

    / **
     * 地图
     * /
    公共静态类地图扩展片段{

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.maptab,集装箱,假);

            //我希望能够做这样的事情...
            / *
            GoogleMap的地图;
            SupportMapFragment FM =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map1)。
            地图= fm.getMap();
            map.DOSOMETHING
            * /


            返回rootView;
        }
    }

    / **
     *路线
     * /

    公共静态类四通八达片段{

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
                查看rootView = inflater.inflate(R.layout.directionstab,集装箱,假);
                ((TextView中)rootView.findViewById(android.R.id.text1))。的setText(
                方向);
            返回rootView;
        }
    }

    / **
     * POI列表
     * /

    公共静态类poiList扩展片段{

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
                查看rootView = inflater.inflate(R.layout.poitab,集装箱,假);
                ((TextView中)rootView.findViewById(android.R.id.text1))。的setText(
                poiList);
            返回rootView;
        }
    }

    / **
     *跳闸信息
     * /

    公共静态类tripInfo扩展片段{

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.tripinfotab,集装箱,假);
            ((TextView中)rootView.findViewById(android.R.id.text1))。的setText(
            跳闸信息);
            返回rootView;
        }
    }

}
 

下面是我的XML文件(maptab.xml),我有我的地图片段...

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <片段
    机器人:ID =@ + ID / MAP1
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.google.android.gms.maps.SupportMapFragment
    />

< / LinearLayout中>
 

解决

 公共静态类地图扩展片段{
        图形页面图形页面;
        GoogleMap的地图;

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
            视图V = inflater.inflate(R.layout.maptab,集装箱,假);

            //获取从XML布局的图形窗口和创建它
            图形页面=(图形页面)v.findViewById(R.id.mapview);
            mapView.onCreate(savedInstanceState);

            //获取从图形页面来使用GoogleMap并执行初始化的东西
            地图= mapView.getMap();
            //map.getUiSettings().setMyLocationButtonEnabled(false);
            //map.setMyLocationEnabled(true);
            (新MarkerOptions()位置(新的经纬度(50.167003,19.383262)))map.addMarker;


            //需要做任何CameraUpdateFactory调用之前调用MapsInitializer
            尝试 {
                MapsInitializer.initialize(this.getActivity());
            }赶上(GooglePlayServicesNotAvailableException E){
                e.printStackTrace();
            }

            //更新的MapView的位置和变焦
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(新经纬度(43.1,-87.9),10);
            map.animateCamera(cameraUpdate);

            返回伏;
        }

        @覆盖
        公共无效onResume(){
            mapView.onResume();
            super.onResume();
        }

        @覆盖
        公共无效的onDestroy(){
            super.onDestroy();
            mapView.onDestroy();
        }

        @覆盖
        公共无效onLowMemory(){
            super.onLowMemory();
            mapView.onLowMemory();
        }
    }
 

解决方案

您可以参考到包含地图使用片段 mViewPager.getItem(0)再投以地图片段,并用它任何你喜欢的方式。但其实这是不是从外面片段操作起来非常不错的设计。因此,我建议你实现所有相关的内部地图片段,这样你就不需要解决此类问题的地图业务逻辑,那你贴和所有你的业务逻辑封装片段,它是便携式的内部。

I'm trying to retrieve the Google Map fragment while using Tabs in an ActionBar. When I load the tabbed page the map loads fine, but I want to get the map object so I can center it, add markers etc.

Is there a way to do this and if there is, would somebody be so kind to show me how?

Here's the code for my tabbed page...

The specific method I'm working with is public static class Map extends Fragment

public class Page3Activity extends FragmentActivity implements ActionBar.TabListener {  

    final Context context = this;
    static GoogleMap map;
    static MapView mMapView;
    DatabaseHandler db = new DatabaseHandler(context);

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    @SuppressLint("NewApi")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.page3);

        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        ColorDrawable colorDrawable = new ColorDrawable();
        final ActionBar actionBar = getActionBar();
        colorDrawable.setColor(0xff9ACC00);
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setTitle("Road Trip Calculator");
        actionBar.setSubtitle("Trip Information");
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE );

     // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

    }

    /*
     * ACTIONBAR TOP BAR CODE
     */


    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        //Switching between tabs
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new Map();
                case 1:
                    return new Directions();
                case 2:
                    return new poiList();
                case 3:
                    return new tripInfo();
                default:
                    i=i+1;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public String getPageTitle(int position) {
            String tabName = "";
            if(position == 0) {
                tabName = "Map";
            } else if(position == 1) {
                tabName = "Directions";
            } else if(position == 2) {
                tabName = "POI List";
            } else if(position == 3) {
                tabName = "Trip Info";
            }
            return tabName;

        }
    }

    /**
     * Map
     */
    public static class Map extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.maptab, container, false);

            //I want to be able to do something like this...
            /* 
            GoogleMap map;
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
            map = fm.getMap();
            map.DOSOMETHING
            */


            return rootView;
        }
    }

    /**
     * Directions
     */

    public static class Directions extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.directionstab, container, false);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                "directions");
            return rootView;
        }
    }

    /**
     * POI List
     */

    public static class poiList extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.poitab, container, false);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                "poiList");
            return rootView;
        }
    }

    /**
     * Trip Info
     */

    public static class tripInfo extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.tripinfotab, container, false);
            ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            "Trip Info");
            return rootView;
        }
    }

}

Here is my XML file (maptab.xml) that I have my map fragment in...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    />

</LinearLayout>

SOLVED

    public static class Map extends Fragment {
        MapView mapView;
        GoogleMap map;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.maptab, container, false);

            // Gets the MapView from the XML layout and creates it
            mapView = (MapView) v.findViewById(R.id.mapview);
            mapView.onCreate(savedInstanceState);

            // Gets to GoogleMap from the MapView and does initialization stuff
            map = mapView.getMap();
            //map.getUiSettings().setMyLocationButtonEnabled(false);
            //map.setMyLocationEnabled(true);
            map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));


            // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
            try {
                MapsInitializer.initialize(this.getActivity());
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }

            // Updates the location and zoom of the MapView
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
            map.animateCamera(cameraUpdate);

            return v;
        }

        @Override
        public void onResume() {
            mapView.onResume();
            super.onResume();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mapView.onDestroy();
        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mapView.onLowMemory();
        }
    }

解决方案

You can get reference to the fragment containing your Map using mViewPager.getItem(0) and then cast to Map fragment and use it any way you like. But this is actually not very good design to operate on fragment from the outside. So I would recommend you to implement all the business logic related to map inside Map fragment, this way you don't need to solve that kind of issue, that you posted and all you business logic is encapsulated inside a fragment, which is portable.

这篇关于获取谷歌地图片段onCreateView使用动作条的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆