简体   繁体   English

Google Maps标签内的片段

[英]Google maps fragment inside of tabs

I have a main activity that has 2 tabs that are created in it (im using the android studio viewpager tabs example as a base). 我有一个主要活动,其中创建了2个标签(即以android studio viewpager标签为例)。 I also have a sample maps activity that I would like to put inside of the 2nd tab. 我还有一个示例地图活动,我想放在第二个选项卡中。 I am not too sure how to do this, and was looking for some guidance. 我不太确定如何执行此操作,因此正在寻找一些指导。

So here is the SectionsPagerAdapter that is inside of the main activity, and I believe this is where I am supposed to do so, but do I call an intent to launch the activity that creates the map fragment, or is there a way to do this directly? 所以这是主活动内部的SectionsPagerAdapter,我相信这是应该这样做的地方,但是我是否调用了启动创建地图片段的活动的意图,或者有没有办法做到这一点?直?

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        //so here?
        return PlaceholderFragment.newInstance(position + 1);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_upcoming).toUpperCase(l);
            case 1:
                return getString(R.string.title_map).toUpperCase(l);
        }
        return null;
    }
}

and here is the MapView.java: 这是MapView.java:

public class MapView extends ActionBarActivity {

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map_view);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (map == null) {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (map != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        //map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

and lastly here is the xml for the fragment for the map 最后是地图片段的xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsView" />

I think this is all you will need, but I can add other parts if need be. 我认为这就是您所需要的,但是如果需要,我可以添加其他部分。 Thanks 谢谢

The ViewPager only handles Fragments as its child, so you need to change your MapView into a Fragment so that the ViewPager can host it. ViewPager仅将Fragments作为其子级来处理,因此您需要将MapView更改为Fragment,以便ViewPager可以承载它。 Instead of OnCreate, you will have : 代替OnCreate,您将拥有:

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

Then in the adapter, you should change the getItem method to : 然后在适配器中,应将getItem方法更改为:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return PlaceHolderFragment.newInstance();
        case 1:
            return MapViewFragment.newInstance();
    }
}

In your case, you should use one fragment per child of your ViewPager. 对于您的情况,您应该为ViewPager的每个子项使用一个片段。 On the default page, you will see the PlaceHolderFragment, and when swipping to the next page, you will see the MapViewFragment. 在默认页面上,您将看到PlaceHolderFragment,滑动到下一页时,您将看到MapViewFragment。

The last thing to do is updating your xml file. 最后要做的是更新您的xml文件。 You should use the code you provided only if you want the Activity to handle the Fragment. 仅当您希望活动处理片段时,才应使用提供的代码。 But in this case, the Fragment is hosted by the ViewPager, so it should have it's own layout. 但是在这种情况下,Fragment由ViewPager托管,因此它应该具有自己的布局。 You should have something like that : 你应该有这样的东西:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsView" />

Hope it helped ! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM