地图V2与viewPager [英] Maps V2 with viewPager
问题描述
我目前正在开发针对Android 4.3。
背景:
我使用pageViewer三个不同的片段之间滚动。
我的第二个片段(片2)具有XML与SupportMapFragment等内容。如下图所示:
我的尝试:
虽然在网上搜索,我注意到,对于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 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?
更多关于我的code: 在为getItem里面FragmentPagerAdapter我回到单身片段(每个片段都有一个类),因此我无法找到在互联网上的任何解决方案,因为我的课不能扩展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-果冻的支持库),可以使用的嵌套片段的。
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)));
}
}
}
以上code从这里复制一>
Impoatant注意:您的自定义片段
的布局不能包含<片断>
标记
Impoatant note: your custom Fragments
's layout cannot contain <fragment>
tag.
这篇关于地图V2与viewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!