2

我修改了 ABS Fragment - Menu 示例,以便当按下操作栏上的菜单项(Home 或 Filter)之一时,它会打开一个带有上下文菜单的片段(menu1)。

但是,按下菜单项时不会出现 menu1。也不会发生错误。

你能帮我找到我缺少的东西吗?

public static class MenuFragment extends SherlockFragment {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
     }

     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         menu.add("Home").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

     }
}

第二个片段:

     /**
      * Second fragment with a menu.
      */
public static class Menu2Fragment extends SherlockFragment {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
     }

     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         menu.add("Filter").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) 
     {
         int id = item.getItemId();
         Toast.makeText(getActivity(), "Option " + id+ " selected", Toast.LENGTH_SHORT).show();
         if (id == android.R.id.home) {
            // rbmView.toggleMenu();

         return true;
         } else {

             FragmentManager fm = getActivity().getSupportFragmentManager();
             FragmentTransaction ft = fm.beginTransaction();
             Fragment mFrag1 = fm.findFragmentByTag("menu1");
             if (mFrag1 == null) {
                 mFrag1 = new ContxtMenuFragment();
                 ft.add(mFrag1, "menu1");
                 ft.show(mFrag1);
             }
             ft.commit();  
             return super.onOptionsItemSelected(item);
         }
     }
}

上下文菜单片段

public static class ContxtMenuFragment extends android.support.v4.app.Fragment {
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

         }
         @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                // TODO Auto-generated method stub
                super.onCreateContextMenu(menu, v, menuInfo);
                CreateMenu(menu);  // http://mobiforge.com/designing/story/understanding-user-interface-android-part-4-even-more-views
            }

         @Override
            public boolean onContextItemSelected(android.view.MenuItem item) {
                // TODO Auto-generated method stub
                return super.onContextItemSelected(item);
    //              return MenuChoice(item);
            }

     public static boolean MenuChoice(android.view.MenuItem item) {
    //       switch (item.getItemId()) {
    //       case 0:
    //           Toast.makeText(ViewPagerActivity.this, "You clicked on Item 1", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 1:
    //           Toast.makeText(getActivity(), "You clicked on Item 2", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 2:
    //           Toast.makeText(getActivity(), "You clicked on Item 3", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 3:
    //           Toast.makeText(getActivity(), "You clicked on Item 4", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 4:
    //           Toast.makeText(getActivity(), "You clicked on Item 5", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 5:
    //           Toast.makeText(getActivity(), "You clicked on Item 6", 
    //               Toast.LENGTH_LONG).show();
    //           return true;
    //       case 6:
    //           Toast.makeText(getActivity(), "You clicked on Item 7", 
    //               Toast.LENGTH_LONG).show();
    //           return true;            
    //       }
       return false;
      }    


    public static void CreateMenu(ContextMenu menu) {
        // TODO Auto-generated method stub
         menu.setQwertyMode(true);
         android.view.MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
         {
             mnu1.setAlphabeticShortcut('a');
        //     mnu1.setIcon(R.drawable.alert_dialog_icon);            
         }
         android.view.MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
         {
             mnu2.setAlphabeticShortcut('b');
         //    mnu2.setIcon(R.drawable.ic_popup_reminder);            
         }
         android.view.MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
         {
             mnu3.setAlphabeticShortcut('c');
             mnu3.setIcon(R.drawable.icon);
         }
         android.view.MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
         {
             mnu4.setAlphabeticShortcut('d');                    
         }

}
4

0 回答 0