Action Bar标签中的MapFragment

问题描述 投票:9回答:2

我正在尝试构建一个将实现Action Bar选项卡的应用程序。其中一个选项卡应包含MapFragment。

如何实现带有选项卡的操作栏,其中一个是地图片段?

你能帮我解决这个问题吗?

这是我到目前为止:

主要班级

package com.nfc.demo;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class NFCDemoActivity extends Activity {

  Tab selectedTab = null;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getActionBar();
    bar.setDisplayShowHomeEnabled(false);
    bar.setDisplayShowTitleEnabled(false);

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    bar.setCustomView(R.layout.main);

    ActionBar.Tab mapTab = bar.newTab().setText("Map");
    ActionBar.Tab settingsTab = bar.newTab().setText("Settings");
    ActionBar.Tab aboutTab = bar.newTab().setText("About");

    MapFragment mapFragment = new MapFragment();
    SettingsFragment settingsFragment = new SettingsFragment();
    AboutFragment aboutFragment = new AboutFragment();

    mapTab.setTabListener(new TabListener(mapFragment));
    settingsTab.setTabListener(new TabListener(settingsFragment));
    aboutTab.setTabListener(new TabListener(aboutFragment));

    Tab selectedTab = (Tab) getLastNonConfigurationInstance();

    if (selectedTab == null) {
      bar.addTab(mapTab, false);
      bar.addTab(settingsTab, false);
      bar.addTab(aboutTab, true);
    }

    setContentView(R.layout.main);

  }

  public Object onRetainNonConfigurationInstance() {
    return selectedTab;
  }

  protected boolean isRouteDisplayed() {
      return false;
  }

  protected class TabListener implements ActionBar.TabListener {
    private Fragment fragment;

    public TabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        fragmentTransaction.replace(R.id.mainFragment, this.fragment, null);
        selectedTab = tab;
    }

    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
        fragmentTransaction.remove(this.fragment);
    }

    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
        //do nothing
    }
  }
}

Fragment类只是返回一个带有.xml布局的inflater。

XML布局:

main.xml(映射应该在此XML文件中)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

</LinearLayout>

settings.xml和about.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView123"
        android:text="asdfg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

但添加qazxsw poi会引发错误:

MapFragment

我一直试图弄清楚如何进行几天,但我真的很困惑。任何帮助/提示将不胜感激。

还有,Error inflating class fragment error caused by java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040005, tag null, or parent id 0x1020002 with another fragment for com.google.android.gms.maps.MapFragment 12-28 21:14:07.991: E/AndroidRuntime(26189): at android.app.Activity.onCreateView(Activity.java:4722) 怎么样?它已被弃用。

android android-layout android-fragments android-maps google-maps-android-api-2
2个回答
17
投票

在以下解决方案中,可以将GoogleMap添加到操作栏选项卡/下拉列表中。这样做的关键在于在切换到Action Bar中的另一个片段时正确设置片段以销毁MapFragment。

创建一个实现Action Bar功能的Activity:

  1. 在Eclipse中创建一个项目,该项目使用Tabs作为主要活动。如果不这样做,请继续执行步骤2-5。
  2. 创建一个扩展getLastNonConfigurationInstance()并实现Activity的类。
  3. 创建一个布局XML文件,当您在它们之间切换时,该文件是制表符片段的容器。
  4. 在Activity类中实现/覆盖以下方法:ActionBar.OnNavigationListener
  5. 在此方法中,在public boolean onNavigationItemSelected(int position, long id)对象之间切换以确定所选选项卡,并使用FragmentManager将片段设置为新实例,如下所示:position

创建一个包含地图的片段:

  1. 创建一个扩展getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit()的类,以用作选项卡的片段。阅读[Fragment]以更好地理解MapFragment。
  2. 创建一个包含片段元素的布局XML文件(如[1]中所示)。使用该页面中的XML创建布局XML文件并在片段类中使用它。
  3. 通过重写1来扩展片段类中的布局XML文件。
  4. 您的应用现在应该在使用您的片段类的选项卡中显示一个地图,但是,切换到另一个选项卡并返回到地图选项卡将导致重复的视图ID。为了克服这个问题,请继续下一步。
  5. 在您的片段类中,重写以下方法以专门销毁基础GoogleMap对象,以便在map选项卡再次加载片段类时可以重新创建它:
onCreateView

-1
投票

不确定你是否已经解决了。您必须将Google Play服务添加为库项目才能使其正常运行。首先,我尝试添加jar文件,但这不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.