带有 viewPager 的地图 V2

Maps V2 with viewPager(带有 viewPager 的地图 V2)

本文介绍了带有 viewPager 的地图 V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 Android 4.3 开发.
背景:
我正在使用 pageViewer 在三个不同的片段之间滚动.我的第二个片段(选项卡 2)包含带有 SupportMapFragment 和其他内容的 XML.如下图:
我的问题http://imageupload.co.uk/files/vrhg1e06x977rkm1vhmd.jpg
我的尝试:
在搜索网络时,我注意到 SupportMapFragment 我需要 FragmentActivity,它不适用于 FragmentPagerAdapter.我需要用户能够控制地图以及控制片段中的其余内容.
我的问题:
如何在片段中有更多内容的同时将 SupportMapFragment 与 viewPager 一起使用?

I'm currently developing for Android 4.3.
Background:
I'm using pageViewer to scroll between three different fragments. My second fragment (tab 2) has XML with SupportMapFragment and other content. as shown below :
My problem http://imageupload.co.uk/files/vrhg1e06x977rkm1vhmd.jpg
My Attempt:
While searching the web I noticed that for SupportMapFragment I need FragmentActivity which doesn't apply for FragmentPagerAdapter. I need the user to have the ability to control the map as well as control the rest of the content in the fragment.
My Question:
How can I use the SupportMapFragment with the viewPager while having more content in the Fragment?

更多关于我的代码:在 FragmentPagerAdapter 内的 getItem 中,我返回单例片段(每个片段都有一个类),因此我无法通过 Internet 找到任何解决方案,因为我的类无法扩展 SupportMapFragment,因为它有更多数据.

More about my code: in the getItem inside FragmentPagerAdapter I'm returning Singleton Fragments (each Fragment has a class) therefore I couldn't find any solution over the Internet since my class can't extend SupportMapFragment because it has more data.

推荐答案

从 Android 4.2 开始(也在 pre-Jelly 的支持库中)你可以使用 嵌套片段.

Since Android 4.2 (also in the support library for pre-Jelly) you can use nested fragments.

你可以看到如何创建这样的片段是:

You can see how to create such fragment is:

public class MyFragment extends Fragment {

    private SupportMapFragment fragment;
    private GoogleMap map;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, fragment).commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (map == null) {
            map = fragment.getMap();
            map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
        }
    }
}

以上代码从此处复制而来.

重要提示:您的自定义 Fragments 的布局不能包含 <fragment> 标记.

Impoatant note: your custom Fragments's layout cannot contain <fragment> tag.

这篇关于带有 viewPager 的地图 V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!















本文标题为:带有 viewPager 的地图 V2