简体   繁体   English

Android菜单项打开新片段

[英]android menu item open new fragment

i'm using swiping tabs as navigation to display 5 pages ( fragments) , in the app menu ( 3 dot ) button i want to create 2 pages, 1 for settings, and other for about page ( just static text) . 我正在使用滑动选项卡作为导航来显示5页(片段),在应用程序菜单中(3点)按钮,我想创建2页,其中1个用于设置,另一些用于关于页面(仅静态文本)。 please advice how can i to this. 请建议我该怎么办。

in specific, how can i go to a new fragment ( not in my tab) to be displayed when clicked from menu items. 具体来说,当我从菜单项中单击时,如何去显示一个新的片段(不在我的标签中)。

please assist me, thank you in advance. 请帮助我,谢谢你。

this is my code so far. 到目前为止,这是我的代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){

        case R.id.action_reset:

            Unsafe.uhs1a.setSelection(0);
            Unsafe.uhs1b.setSelection(0);
            Unsafe.uhs1c.setSelection(0);
            Precondition.phs1a.setSelection(0);
            Precondition.phs1b.setSelection(0);
            Precondition.phs1c.setSelection(0);

        case R.id.action_about:

            // need to open a static page ( fragment) here 

        return true;
        default:
            return super.onOptionsItemSelected(item);
    }


}

}

Not sure if this is what you want, but I have a fragment which opens a new Actvity (with a fragment inside): 不知道这是否是您想要的,但是我有一个片段可以打开一个新的Actvity(里面有一个片段):

 public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.action_about:
         Intent intent_to = new Intent(getActivity(),MyNewActivity.class);
            startActivity(intent_to);
            break;

    }
    return true;
}

Inside MyNewActivity class you should have a container and open a fragment using getFragmentManager() MyNewActivity类中,您应该有一个容器并使用getFragmentManager()打开一个片段

You can even use startActivityForResult if you want to do something depending in how the new fragment closes 如果要根据新片段的关闭方式进行操作,甚至可以使用startActivityForResult

Hope it helps 希望能帮助到你

Old question but WTH.You do this using a FragmentManager and her friend FragmentTransaction : 问题是WTH,您可以使用FragmentManager和她的朋友FragmentTransaction

@Override
public boolean onOptionsItemSelected(MenuItem item){
   switch (item.getItemId()){

    case R.id.action_reset:

        Unsafe.uhs1a.setSelection(0);
        Unsafe.uhs1b.setSelection(0);
        Unsafe.uhs1c.setSelection(0);
        Precondition.phs1a.setSelection(0);
        Precondition.phs1b.setSelection(0);
        Precondition.phs1c.setSelection(0);

    case R.id.action_about:
      Fragment newFragment = new TheFragmentYouWantToOpen();
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, newFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
    //frame_container is the id of the container for the fragment
    return true;
    default:
        return super.onOptionsItemSelected(item);
}

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

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