Actionbar启动片段

时间:2014-02-25 11:27:20

标签: android android-fragments android-actionbar fragment

如何从onOptionsItemsSelected启动片段?

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
          case R.id.action_support:

              Fragment f = new SupportFragment();
                          FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(android.R.id.content, f).commit();



              Toast display = Toast.makeText(this, "Settings", 10);
              display.show();
          case R.id.action_guide:
              Toast display1 = Toast.makeText(this, "Guide", 10);
              display1.show();
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }

}

我试图启动支持片段。

由于

2 个答案:

答案 0 :(得分:4)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   Fragment fragment; 
    switch (item.getItemId()) {
            case R.id.action_support:
                fragment = new Home();
            case 1:
                fragment = new NotesList();
            default:
                fragment = new defaultPageOrAnyOtheOption();
    }

  FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment).commit();
  return true;

} // onOptionsItemSelected

答案 1 :(得分:0)

试试吧

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
          case R.id.action_support:

              //launch Support Fragment
             // Fragment f = new SupportFragment();
             // replace(android.R.id.content, f);

              Toast display = Toast.makeText(this, "Settings", 10);
              display.show();
          return true;
          case R.id.action_guide:
              Toast display1 = Toast.makeText(this, "Guide", 10);
              display1.show();
             return true;
                     }
    }