繁体   English   中英

Google Maps标签内的片段

[英]Google maps fragment inside of tabs

我有一个主要活动,其中创建了2个标签(即以android studio viewpager标签为例)。 我还有一个示例地图活动,我想放在第二个选项卡中。 我不太确定如何执行此操作,因此正在寻找一些指导。

所以这是主活动内部的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;
    }
}

这是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"));
    }
}

最后是地图片段的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" />

我认为这就是您所需要的,但是如果需要,我可以添加其他部分。 谢谢

ViewPager仅将Fragments作为其子级来处理,因此您需要将MapView更改为Fragment,以便ViewPager可以承载它。 代替OnCreate,您将拥有:

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

然后在适配器中,应将getItem方法更改为:

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

对于您的情况,您应该为ViewPager的每个子项使用一个片段。 在默认页面上,您将看到PlaceHolderFragment,滑动到下一页时,您将看到MapViewFragment。

最后要做的是更新您的xml文件。 仅当您希望活动处理片段时,才应使用提供的代码。 但是在这种情况下,Fragment由ViewPager托管,因此它应该具有自己的布局。 你应该有这样的东西:

<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" />

希望能有所帮助!

暂无
暂无

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

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