添加工具栏后,棒棒糖上的导航抽屉是空的

时间:2015-05-01 15:31:21

标签: android navigation-drawer android-toolbar

我有一个导航抽屉工作了一段时间,但后来我决定更新它以使用Toolbar而不是默认的支持操作栏。现在,我有一种情况,当我点击汉堡包菜单时,导航抽屉不会打开。但是,我可以从左向右滑动并打开它。问题是,它现在是空的。

值-V21 / styles.xml

    <style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/app_green</item>
    <item name="colorPrimaryDark">@color/app_green_dark</item>
    <item name="android:textColorPrimary">@color/action_bar_text</item>
    <item name="android:textColor">@color/secondary_text_color</item>
    <item name="android:color">@color/secondary_text_color</item>
    <item name="android:colorAccent">@color/app_green</item>
    <item name="android:editTextColor">@color/secondary_text_color</item>

    <item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
    <item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
    <item name="activatableItemBackground">@drawable/activatable_item_background</item>

    <!-- ActionBar Styles -->
    <item name="android:windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="android:windowActionBar">false</item>

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">?android:attr/colorPrimaryDark</item>

    <!-- Global UI Assignments -->
    <item name="android:spinnerStyle">@style/Material.Widget.Spinner</item>

    <item name="android:buttonStyle">@style/Material.Widget.Button</item>
    <item name="android:checkboxStyle">@style/Material.Widget.Checkbox</item>
    <item name="android:textAppearance">@android:style/TextAppearance</item>

    <item name="android:popupWindowStyle">@style/Material.Window.Popup</item>

    <!-- ViewPager -->
    <item name="vpiCirclePageIndicatorStyle">@style/Material.Activity.Login.ViewPagerIndicator.CustomCircle</item>
    <item name="buttonBarStyle">?android:buttonBarStyle</item>
    <item name="buttonBarButtonStyle">?android:buttonBarButtonStyle</item>
    <item name="indeterminateProgressStyle">?android:indeterminateProgressStyle</item>
</style>

navigation_drawer.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<!-- The main content view. Must be first child because XML ordering implies stacking
 context -->
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <!-- Our App Bar. Placed here so we can draw over it with the navigation drawer. -->
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:background="?android:attr/colorPrimary" />

    <!-- Our actual content view. -->
    <FrameLayout
        android:id="@+id/drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

</LinearLayout>

<!-- The navigation drawer itself. -->
<ListView
    android:id="@+id/drawer_list"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:fitsSystemWindows="true"
/>

NavigationDrawerActivity.java

    public class NavigationDrawerActivity extends ActionBarActivity
  implements AdapterView.OnItemClickListener {

  private DrawerLayout mDrawerLayout;
  private ListView mDrawerList;
  private LayoutInflater mInflater;
  private NavDrawerItemAdapter mAdapter;
  private ActionBarDrawerToggle mDrawerToggle;

  // Our "App bar". This will only be populated if the current SDK is >= 21. Otherwise, we'll use an
  // action bar that will be populated when the app first starts.
  private Toolbar mAppBar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation_drawer);
    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    setupNavigationDrawer();
  }

  @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);
    mDrawerToggle.onConfigurationChanged(newConfig);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }

  /**
   * Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
   * it's closed).
   */
  public void toggleNavigationDrawer() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
      closeNavigationDrawer();
    } else {
      openNavigationDrawer();
    }
  }

  /**
   * Opens the navigation drawer.
   */
  public void openNavigationDrawer() {
    mDrawerLayout.openDrawer(GravityCompat.START);
  }

  /**
   * Closes the navigation drawer.
   */
  public void closeNavigationDrawer() {
    mDrawerLayout.closeDrawer(GravityCompat.START);
  }

  /**
   * Initializes items specific to the navigation drawer.
   */
  private void setupNavigationDrawer() {
    mDrawerLayout = (DrawerLayout) mInflater.inflate(R.layout.navigation_drawer, null); // "null" is important.
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.app_green));

    // We want to use the toolbar if the version is 21 or greater, because is has more flexibility
    // than the standard ActionBar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      mAppBar = (Toolbar) findViewById(R.id.app_bar);
      setSupportActionBar(mAppBar);
    }

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    mDrawerToggle = new ActionBarDrawerToggle(
      this,                  /* Our context (Activity that hosts this drawer) */
      mDrawerLayout,         /* The DrawerLayout where the nav drawer will be drawn */
      mAppBar,               /* The Toolbar that is being used as an app bar. */
      R.string.drawer_open,  /* Description of "open drawer", for accessibility */
      R.string.drawer_close  /* Description of "close drawer", for accessibility */
    ) {

      /**
       * Called when a drawer has settled in a completely closed state.
       */
      public void onDrawerClosed(View view) {
        super.onDrawerClosed(view);
        supportInvalidateOptionsMenu();
      }

      /**
       * Called when a drawer has settled in a completely open state.
       */
      public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        supportInvalidateOptionsMenu();
      }
    };

    mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);

    NavDrawerGroup userToolsGroup = new NavDrawerGroup(R.string.drawer_group_usertools, false, 1);
    NavDrawerGroup toolBoxGroup = new NavDrawerGroup(R.string.drawer_group_toolbox, true, 2);
    NavDrawerGroup supportGroup = new NavDrawerGroup(R.string.drawer_group_support, true, 3);

    NavDrawerItem[] navDrawerItems = {
      new NavDrawerItem(R.string.drawer_item_main, R.drawable.ic_main, NavDrawerGroup.DEFAULT_GROUP),
      new NavDrawerItem(R.string.drawer_item_tool_one, R.drawable.ic_tool_one, NavDrawerGroup.DEFAULT_GROUP),
      new NavDrawerItem(R.string.drawer_item_tool_two, R.drawable.ic_tool_two, userToolsGroup),
    };

    mAdapter = new NavDrawerItemAdapter(this, navDrawerItems);
    mDrawerList.setAdapter(mAdapter);

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(this);
  }
}

我知道NavDrawerItem的东西看起来有点不稳定,但我已经证实它适用于旧的导航抽屉(工具栏前),而它所做的只是填充{{1}的一些逻辑带有图标的项目并将它们放在适当的分组中。所以,我相信这不是问题所在。

即使它是,我应该得到除空白背景之外的东西,因为导航抽屉的布局实际上设置了具有绿色背景的ListView的标题,我没有看到。< / p>

修改

根据@ blacksh33p的评论,我删除了处理不同API级别的代码,并将ListView构造函数更改为:

嗯,没有快乐。我删除了条件检测API级别,刚刚初始化了mDrawerToggle,然后将Toolbar更改为以下内容:

mDrawerToggle

但是,我最终得到了相同的结果。 :(

2 个答案:

答案 0 :(得分:2)

onCreate移除代码并添加到setContentView

参考这个DrawerActivity它可能有帮助.. !!

 public class DrawerActivity extends ActionBarActivity {

protected ActionBarDrawerToggle mDrawerToggle;
private String[] mModulesTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private Intent intent;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

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

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

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.options, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {

        return true;
    }
    switch (item.getItemId()) {
    case R.id.action_search:
        intent = new Intent(this, JoinLeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        return true;

    case R.id.action_add:
        intent = new Intent(this, CreateLeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
        return true;

    case R.id.contactUs:
        FragmentManager fm = this.getSupportFragmentManager();
        DialogFragment dialog = new ContactUsDialogFragment(); // creating new object
        dialog.show(fm, "dialog");
        return new ContactUsDialogFragment() != null;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void setContentView(final int layoutResID) {

    View fullLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.layout_drawer, null);

    FrameLayout actContent = (FrameLayout) fullLayout
            .findViewById(R.id.content_frame);

    mModulesTitles = getResources().getStringArray(R.array.modules_array);
    mDrawerLayout = (DrawerLayout) fullLayout
            .findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) fullLayout.findViewById(R.id.left_drawer);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);


    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[3], navMenuIcons.getResourceId(3, -1)));
    // Pages
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[4],-1));
    // What's hot, We  will add a counter here
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[5], -1));
    navDrawerItems.add(new NavDrawerItem(mModulesTitles[6], -1));

    // Recycle the typed array
    navMenuIcons.recycle();
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);
//      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mModulesTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(R.string.app_name);
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(R.string.app_name);
        }
    };
    Log.e("DRAWERLAYOUT@@@", "" + mDrawerLayout);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    getLayoutInflater().inflate(layoutResID, actContent, true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    super.setContentView(fullLayout);
}

public class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    public DrawerItemClickListener() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}
private void selectItem(int position) {
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mModulesTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
    if (position == 0) {
        Intent intent = new Intent(this, DashboardActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 1) {
        Intent intent = new Intent(this, LeagueActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 2) {
        Intent intent = new Intent(this, MatchupActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 3) {
        Intent intent = new Intent(this, HelpActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 4) {
        Intent intent = new Intent(this, PrivacyActivity.class);
        intent.putExtra("from_register", false);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 5) {
        Intent intent = new Intent(this, TermsActivity.class);
        intent.putExtra("from_register", false);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
    if (position == 6) {
        SharedPreferenceUtil.putValue("loginStatus", false);
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        this.finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }
}
}

答案 1 :(得分:0)

我正在使用drawerLayout使用我自己的工具栏开发一个应用程序,它在我的设备上运行良好(android 5.0.1)。为此,我有一个像这样的xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F78B1E"
        android:minHeight="?attr/actionBarSize" />


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical">
        <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/left_navigation"
            android:layout_width="400dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#fff"
        />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>