7

我目前正在尝试按照 Google IO 2013 指南实施新的 NavigationDrawer。我正在使用 ActionbarSherlock。代码运行良好。唯一的问题是,当拉出导航抽屉时,我无法切换应用程序图标旁边的“向上”插入符号。我在下面粘贴我的整个代码。请帮忙。

public class MainActivity extends SherlockFragmentActivity {
   private DrawerLayout mDrawerLayout;
   private ListView mDrawerList;
   private ActionBarDrawerToggle mDrawerToggle;

   private CharSequence mDrawerTitle;
   private CharSequence mTitle;
   private String[] mPlanetTitles;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      mTitle = mDrawerTitle = getTitle();
      mPlanetTitles = getResources().getStringArray(R.array.planets_array);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.drawer);

      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles));
      mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

      mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
      mDrawerLayout, /* DrawerLayout object */
      R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
      R.string.drawer_open, /* "open drawer" description for accessibility */
      R.string.drawer_close /* "close drawer" description for accessibility */
      ) {
         public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu(); 
         }

         public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu(); 
         }
      };

      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setHomeButtonEnabled(true);


      mDrawerLayout.setDrawerListener(mDrawerToggle);

      if (savedInstanceState == null) {
         selectItem(0);
      }

      new Thread(new Runnable() {

        @Override
        public void run() {
             prefs = getPreferences(MODE_PRIVATE);
             opened = prefs.getBoolean(OPENED_KEY, false);
             if(opened == false)
             {
                     mDrawerLayout.openDrawer(mDrawerList);
             }
        }
    }).start();
   }

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

   @Override
   public boolean onPrepareOptionsMenu(Menu menu) {
      boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
      menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
      return super.onPrepareOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(final MenuItem item) {
      if (mDrawerToggle.onOptionsItemSelected(getMenuItem(item))) { 
         return true; 
      }

      switch (item.getItemId()) {
         case R.id.action_websearch:
            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
            intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
            if (intent.resolveActivity(getPackageManager()) != null) {
               startActivity(intent);
            } else {
               Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
            }
            return true;
         default:
            return super.onOptionsItemSelected(item);
      }
   }

   private android.view.MenuItem getMenuItem(final MenuItem item) {
      return new android.view.MenuItem() {
         @Override
         public int getItemId() {
            return item.getItemId();
         }

         public boolean isEnabled() {
            return true;
         }

         @Override
         public boolean collapseActionView() {
            return false;
         }

         @Override
         public boolean expandActionView() {
            return false;
         }

         @Override
         public ActionProvider getActionProvider() {
            return null;
         }

         @Override
         public View getActionView() {

            return null;
         }

         @Override
         public char getAlphabeticShortcut() {

            return 0;
         }

         @Override
         public int getGroupId() {

            return 0;
         }

         @Override
         public Drawable getIcon() {

            return null;
         }

         @Override
         public Intent getIntent() {

            return null;
         }

         @Override
         public ContextMenuInfo getMenuInfo() {

            return null;
         }

         @Override
         public char getNumericShortcut() {

            return 0;
         }

         @Override
         public int getOrder() {

            return 0;
         }

         @Override
         public SubMenu getSubMenu() {

            return null;
         }

         @Override
         public CharSequence getTitle() {

            return null;
         }

         @Override
         public CharSequence getTitleCondensed() {

            return null;
         }

         @Override
         public boolean hasSubMenu() {

            return false;
         }

         @Override
         public boolean isActionViewExpanded() {

            return false;
         }

         @Override
         public boolean isCheckable() {

            return false;
         }

         @Override
         public boolean isChecked() {

            return false;
         }

         @Override
         public boolean isVisible() {

            return false;
         }

         @Override
         public android.view.MenuItem setActionProvider(ActionProvider actionProvider) {

            return null;
         }

         @Override
         public android.view.MenuItem setActionView(View view) {

            return null;
         }

         @Override
         public android.view.MenuItem setActionView(int resId) {

            return null;
         }

         @Override
         public android.view.MenuItem setAlphabeticShortcut(char alphaChar) {

            return null;
         }

         @Override
         public android.view.MenuItem setCheckable(boolean checkable) {

            return null;
         }

         @Override
         public android.view.MenuItem setChecked(boolean checked) {

            return null;
         }

         @Override
         public android.view.MenuItem setEnabled(boolean enabled) {

            return null;
         }

         @Override
         public android.view.MenuItem setIcon(Drawable icon) {

            return null;
         }

         @Override
         public android.view.MenuItem setIcon(int iconRes) {

            return null;
         }

         @Override
         public android.view.MenuItem setIntent(Intent intent) {

            return null;
         }

         @Override
         public android.view.MenuItem setNumericShortcut(char numericChar) {

            return null;
         }

         @Override
         public android.view.MenuItem setOnActionExpandListener(OnActionExpandListener listener) {

            return null;
         }

         @Override
         public android.view.MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {

            return null;
         }

         @Override
         public android.view.MenuItem setShortcut(char numericChar, char alphaChar) {

            return null;
         }

         @Override
         public void setShowAsAction(int actionEnum) {


         }

         @Override
         public android.view.MenuItem setShowAsActionFlags(int actionEnum) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitle(CharSequence title) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitle(int title) {

            return null;
         }

         @Override
         public android.view.MenuItem setTitleCondensed(CharSequence title) {

            return null;
         }

         @Override
         public android.view.MenuItem setVisible(boolean visible) {

            return null;
         }
      };
   }

   /* The click listner for ListView in the navigation drawer */
   private class DrawerItemClickListener implements ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         selectItem(position);
      }
   }

   private void selectItem(int position) {
      // update the main content by replacing fragments
      Fragment fragment = new PlanetFragment();
      Bundle args = new Bundle();
      args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
      fragment.setArguments(args);

      FragmentManager fragmentManager = getSupportFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

      // update selected item and title, then close the drawer
      mDrawerList.setItemChecked(position, true);
      setTitle(mPlanetTitles[position]);
      mDrawerLayout.closeDrawer(mDrawerList);
   }

   @Override
   public void setTitle(CharSequence title) {
      mTitle = title;
      getSupportActionBar().setTitle(mTitle);
   }

   /**
    * When using the ActionBarDrawerToggle, you must call it during
    * onPostCreate() and onConfigurationChanged()...
    */

   @Override
   protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      // Sync the toggle state after onRestoreInstanceState has occurred.
      mDrawerToggle.syncState();
   }

   @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      // Pass any configuration change to the drawer toggls
      mDrawerToggle.onConfigurationChanged(newConfig);
   }

   /**
    * Fragment that appears in the "content_frame", shows a planet
    */
   public static class PlanetFragment extends SherlockFragment {
      public static final String ARG_PLANET_NUMBER = "planet_number";

      public PlanetFragment() {
         // Empty constructor required for fragment subclasses
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
         int i = getArguments().getInt(ARG_PLANET_NUMBER);
         String planet = getResources().getStringArray(R.array.planets_array)[i];

      // change int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
         int imageId = getResources().getIdentifier("tempmap", "drawable", getActivity().getPackageName());
         // change ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
         ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
         // change  getActivity().setTitle(planet);
         getActivity().setTitle("MapView");
         return rootView;
      }
   }
}
4

4 回答 4

11

如果您不需要移动 3 行(例如在 gmail 应用程序中),您可以添加:

<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>

在您的活动主题中。我更喜欢在我的应用程序中使用另一个库。

ic_drawer 可以从这里下载:http: //developer.android.com/training/implementing-navigation/nav-drawer.html

于 2013-06-27T11:03:12.793 回答
11

尝试像这样在切换抽屉上调用syncState:

    @Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

//your ActionBarDrawerToggle is below
    mDrawerToggle.syncState();
}

或者就在你实例化它之后。

于 2013-10-03T15:20:51.167 回答
3

我有同样的问题,并对该主题进行了一些研究。让插入符号在较旧的 SDK 上正常工作似乎并不容易。长话短说,有一个人有一个基于反射的运行示例实现:

https://github.com/nicolasjafelle/SherlockNavigationDrawer

我对其进行了测试,它对我来说非常有效,即使在 Android 2.x 上也是如此!

于 2013-06-03T23:46:53.813 回答
0

我在 4.x 设备中删除了插入符号,但在 2.x 设备中没有。如果您需要,存储库在这里: https ://github.com/bcrider/NavigationDrawerSherlocked

这只是 Google 提供的示例,我使用 ActionBarSherlock 进行了修改,就像您所做的那样。我修复了示例中的方法 (onOptionsItemSelected) 的问题,该问题最初阻止了在 ActionBarSherlock 集成到应用程序后点击的图标执行任何操作。

希望这可以帮助!

于 2013-06-09T00:25:08.857 回答