简体   繁体   English

Android GoogleMap(API V2)和标签

[英]Android GoogleMap (API V2) and Tabs

I want a program that should contain a view with 4 tabs and each one has a different things (Example: 1° tab = map, 2° tab = text etc etc). 我想要一个程序,其中应包含一个带有4个选项卡的视图,并且每个视图都有不同的内容(例如:1°选项卡=地图,2°选项卡=文本等)。 I'm a bit newb about those projects, and I'm on a position where I've successfully imported and started the app with a map. 对于这些项目,我有些陌生,并且我已经成功导入并使用地图启动了该应用程序。 If you think it's better using something else than Tabs, I'm agreeing with you since I can't find anything about. 如果您认为使用Tabs比使用Tabs更好,那么我同意您的意见,因为我找不到任何东西。

If you have anything that has more than three or so tabs, navigation tabs are probably not an ideal choice. 如果您有超过三个左右的选项卡,则导航选项卡可能不是理想的选择。

You can try sideNavigationMenu or some other sliding menu library. 您可以尝试sideNavigationMenu或其他一些滑动菜单库。 I find that sideNavigationMenu is the easiest library to work with if you're just starting out. 如果刚开始,我发现sideNavigationMenu是最容易使用的库。

Regardless, if you would like to continue with navigation tabs, below is some sample code to help you get started. 无论如何,如果您想继续浏览标签,下面是一些示例代码来帮助您入门。

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

}

You would then need a write your TabListener to handle switching between fragments. 然后,您需要编写TabListener来处理片段之间的切换。 Since you're working with Google Maps Android, it's better to hide your fragments and show them when you need to as oppose to detaching and attaching fragments. 由于您使用的是Google Maps Android,因此最好隐藏片段,并在需要反对分离和附加片段时显示它们。

This is pretty much the code taken from the API Demos with the exception of using .show() and .hide() 除了使用.show()和.hide()之外,这几乎是从API演示中获取的代码。

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

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

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