0

我想要一個應該包含4個標籤的視圖,每個人都有不同的東西(例如:1°標籤=地圖,2°標籤=文本等)的程序。我對這些項目有點新鮮感,而且我已經成功導入並使用地圖啓動應用程序。如果您認爲使用Tabs以外的其他產品會更好,我同意你的意見,因爲我找不到任何關於它的內容。Android谷歌地圖(API V2)和標籤

回答

1

如果您有任何超過3個左右的選項卡,導航選項卡可能不是理想的選擇。

您可以試試sideNavigationMenu或其他滑動菜單庫。如果你剛剛起步,我發現sideNavigationMenu是最簡單的圖書館。

無論如何,如果你想繼續使用導航標籤,下面是一些示例代碼來幫助你開始。

public class MainActivity extends Activity{ 

public static Context appContext; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    appContext = getApplicationContext(); 


    //ActionBar 
    ActionBar bar = getActionBar(); 
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    bar.addTab(bar.newTab() 
      .setText("Map Fragment") 
      .setTabListener(new TabListener<MapSampleFragment>(
        this, "map_fragment", MapSampleFragment.class))); 

    bar.addTab(bar.newTab() 
      .setText("List Fragment") 
      .setTabListener(new TabListener<LocationListFragment>(
        this, "list_fragment", LocationListFragment.class))); 



    bar.addTab(bar.newTab() 
      .setText("Other Fragment") 
      .setTabListener(new TabListener<MoreStuffFragment>(
        this, "more_stuff_fragment", MoreStuffFragment.class))); 

} 

你會再需要一個寫TabListener處理片段之間的切換。由於您正在使用Google Maps Android,因此最好隱藏片段並在需要時顯示它們,以反對分離和附加片段。

這是非常)

public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 
    private final Activity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 
    private final Bundle mArgs; 
    private Fragment mFragment; 

    public TabListener(Activity activity, String tag, Class<T> clz) { 
     this(activity, tag, clz, null); 
    } 

    public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
     mArgs = args; 

     // Check to see if we already have a fragment for this tab, probably 
     // from a previously saved state. If so, deactivate it, because our 
     // initial state is that a tab isn't shown. 
     mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag); 
     if (mFragment != null && !mFragment.isDetached()) { 
      FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction(); 
      ft.hide(mFragment); 
      ft.commit(); 
     } 
    } 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     if (mFragment == null) { 
      mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); 
      ft.add(android.R.id.content, mFragment, mTag); 
     } else { 
      ft.show(mFragment); 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      ft.hide(mFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show(); 
    } 
} 
從API演示用.show除外()和.hide(採取的代碼