MapFragment在操作栏标签操作、标签、MapFragment

2023-09-04 04:04:13 作者:你声音真好听

我想建立一个应用程序,将执行操作栏的选项卡。一翼片应该包含一个MapFragment

我如何能实现其中一个根据与标签的操作栏,是地图片段?

您可以帮我如何着手呢?

下面是我到目前为止有:

主类

 包com.nfc.demo;

进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.os.Bundle;

公共类NFCDemoActivity延伸活动{

  标签selectedTab = NULL;

  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    动作条酒吧= getActionBar();
    bar.setDisplayShowHomeEnabled(假);
    bar.setDisplayShowTitleEnabled(假);

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    bar.setCustomView(R.layout.main);

    ActionBar.Tab mapTab = bar.newTab()的setText(地图)。
    ActionBar.Tab settingsTab = bar.newTab()的setText(设置)。
    ActionBar.Tab aboutTab = bar.newTab()的setText(关于)。

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

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

    标签selectedTab =(TAB)getLastNonConfigurationInstance();

    如果(selectedTab == NULL){
      bar.addTab(mapTab,假);
      bar.addTab(settingsTab,假);
      bar.addTab(aboutTab,真正的);
    }

    的setContentView(R.layout.main);

  }

  公共对象onRetainNonConfigurationInstance(){
    返回selectedTab;
  }

  保护的布尔isRouteDisplayed(){
      返回false;
  }

  保护类TabListener实现ActionBar.TabListener {
    私人片段片段;

    公共TabListener(片段片段){
        this.fragment =片段;
    }

    公共无效onTabSelected(TAB键,FragmentTransaction fragmentTransaction){
        fragmentTransaction.replace(R.id.mainFragment,this.fragment,NULL);
        selectedTab =标签;
    }

    公共无效onTabUnselected(TAB键,FragmentTransaction fragmentTransaction){
        fragmentTransaction.remove(this.fragment);
    }

    公共无效onTabReselected(TAB键,FragmentTransaction fragmentTransaction){
        //没做什么
    }
  }
}
 
有什么办法可以知道qq好友是否隐身啊

该片段类都只是返回一个充气使用.xml布局。

XML布局:

main.xml中(图应该是在这个XML文件)

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / mainFragment
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>

< / LinearLayout中>
 

的settings.xml和about.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>

    <的TextView
        机器人:ID =@ + ID / textView123
        机器人:文本=asdfg
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

我一直在试图找出如何进行几天,但我真的很困惑。 任何帮助/提示将不胜AP preciated。

此外,何谈 getLastNonConfigurationInstance()?据德precated。

解决方案

在以下解决方案,它可以将一个GoogleMap的添加到动作栏选项卡/下拉。关键要这样做在于正确地设置您的片段切换到操作栏中的另一个片段时破坏MapFragment。

创建一个实现了操作栏功能的活动:

创建一个使用标签的主要活动在Eclipse中的一个项目。如果你不这样做,继续执行步骤2-5。 创建一个扩展活动和农具类 ActionBar.OnNavigationListener 。 创建一个布局的XML文件,该文件是一个容器,您的标签片段,当你在它们之间进行切换。 实施/重写你的Activity类下面的方法:公共布尔onNavigationItemSelected(INT位置,长ID) 。 在这种方法中,位置之间切换对象,以确定所选择的选项卡并使用FragmentManager这样设置片段到一个新的实例: getFragmentManager()的BeginTransaction()代替(R.id.container,片段).commit()

创建,保存地图片段:

创建一个扩展片段作为标签的片段用一个类。阅读[ 1 ]以更好地了解MapFragment。 创建一个包含碎片元件的布局XML文件(如看到[ 1 ])。使用XML在页面创建布局的XML文件,并用它在你的片段类。 将覆盖该充气布局XML文件中的片段类 onCreateView 。 您的应用程序,现在应该在使用你的片段类的标签却显示地图,切换到另一个选项卡,然后回到地图选项卡将导致重复的视图ID。为了克服这个,继续到下一个步骤。 在片段类,重写下面的方法来专门破坏底层的GoogleMap对象,以便它可以被重新创建时,在地图选项卡再次加载你的片段类:

 
    @覆盖
    公共无效onDestroyView(){
        super.onDestroyView();
        MapFragment F =(MapFragment)getFragmentManager()findFragmentById(R.id.map)。
        如果(F!= NULL)
            。getFragmentManager()的BeginTransaction()删除(F).commit();
    }
 

I am trying to build an app that will implement Action Bar tabs. One of the tabs should contain a MapFragment.

How can I implement an action bar with tabs, under one of which is a map Fragment?

Can you help me with how to proceed with this?

Here is what I have so far :

main class

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
    }
  }
}

The Fragment classes are all just returning an inflater with an .xml layout.

XML Layouts :

main.xml ( map should be on this XML file )

<?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 AND 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>

I've been trying to figure out how to proceed for a couple of days but I am really confused. Any help/tips would be greatly appreciated.

Also, what about getLastNonConfigurationInstance()? It is deprecated.

解决方案

In the following solution, it is possible to add a GoogleMap to an Action Bar tab/dropdown. The key to doing this lies in correctly setting up your fragment to destroy the MapFragment when switching to another fragment in the Action Bar.

Create an Activity that implements the Action Bar functionality:

Create a project in Eclipse that uses Tabs for the main activity. If you don't do this, proceed to steps 2-5. Create a class that extends Activity and implements ActionBar.OnNavigationListener. Create a layout XML file that is a container for your tab fragments when you switch between them. Implement/override the following method in your Activity class: public boolean onNavigationItemSelected(int position, long id). In this method, switch between the position object to determine the selected tab and set the fragment to a new instance using the FragmentManager like this: getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit().

Create a fragment that holds the map:

Create a class that extends Fragment to use as your tab's fragment. Read [1] to better understand the MapFragment. Create a layout XML file that contains a fragment element (as seen in [1]).Use the XML in that page to create a layout XML file and use it in your fragment class. Inflate that layout XML file in your fragment class by overriding onCreateView. Your app should now display a map in the tab that uses your fragment class, however, switching to another tab and back to the map tab will result in a duplicate view ID. To overcome this, go on to the next step. In your fragment class, override the following method to specifically destroy the underlying GoogleMap object so that it can be recreated when the map tab loads your fragment class again:


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        MapFragment f = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        if (f != null) 
            getFragmentManager().beginTransaction().remove(f).commit();
    }