gpt4 book ai didi

java - 标签内的谷歌地图 fragment

转载 作者:行者123 更新时间:2023-12-01 12:01:07 25 4
gpt4 key购买 nike

我有一个主要 Activity ,其中创建了 2 个选项卡(我使用 android studio viewpager 选项卡示例作为基础)。我还有一个示例 map Activity ,我想将其放入第二个选项卡中。我不太确定如何做到这一点,并正在寻找一些指导。

所以这里是主 Activity 内部的SectionsPagerAdapter,我相信这就是我应该这样做的地方,但是我是否调用一个 Intent 来启动创建 map fragment 的 Activity ,或者有没有办法直接执行此操作?

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

最后是 map fragment 的 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 仅将 Fragment 作为其子级处理,因此您需要将 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 的每个子级使用一个 fragment 。在默认页面上,您将看到 PlaceHolderFragment,当滑动到下一页时,您将看到 MapViewFragment。

最后要做的事情是更新您的 xml 文件。仅当您希望 Activity 处理 Fragment 时,才应使用您提供的代码。但在本例中,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" />

希望对你有帮助!

关于java - 标签内的谷歌地图 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27974813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com