2015-03-31 20 views
1

我想使用工具欄的導航抽屜,但按導航按鈕不會觸發onOptionsItemSelected處理程序和列表不會打開。導航MenuItem在工具欄不會觸發onOptionsItemSelected

然而,搜索按鈕會觸發onOptionsItemSelected。

這是我的代碼和xml:

編輯: 也onDrawerClosed()和onDrawerOpened()不叫

MainActivity.java:

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

mToolbar = (Toolbar) findViewById(R.id.tool_bar); 
setSupportActionBar(mToolbar); 

mAdapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles, numOfTabs); 

// Assigning ViewPager View and setting the adapter 
mPager = (ViewPager) findViewById(R.id.pager); 
mPager.setAdapter(mAdapter); 

// Assigning the Sliding Tab Layout View 
mTabs = (SlidingTabLayout) findViewById(R.id.tabs); 
mTabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width 

mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
    @Override 
    public int getIndicatorColor(int position) { 
     return getResources().getColor(R.color.augury_white); 
    } 
}); 

// Setting the ViewPager For the SlidingTabsLayout 
mTabs.setViewPager(mPager); 

// Set navigation drawer 
mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
mDrawerList = (ListView) findViewById(R.id.left_drawer); 

// Set the adapter for the list view 
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); 
// Set the list's click listener 
mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

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

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

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
      getActionBar().setTitle(mTitle); 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      syncState(); 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      getActionBar().setTitle(mDrawerTitle); 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      syncState(); 
     } 
    }; 

// Set the drawer toggle as the DrawerListener 
mDrawerLayout.setDrawerListener(mDrawerToggle); 

}

activity_main.xml:

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

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     /> 

    <com.augury.mobile.auguryandroid.SlidingTabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="2dp" 
     android:background="@color/augury_blue"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:layout_weight="1"> 

    <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"> 


    <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 

    </android.support.v4.widget.DrawerLayout> 
    </android.support.v4.view.ViewPager> 

</LinearLayout> 

謝謝!

+0

你覆蓋'onPostCreate()'? – Xcihnegn 2015-03-31 08:00:13

+0

是的,我調用它:mDrawerToggle.syncState(); – 2015-03-31 08:03:13

+0

'mDrawerToggle.setDrawerIndicatorEnabled(true);'你有?以及'ActionBarDrawerToggle',V7的導入? – Xcihnegn 2015-03-31 08:09:50

回答

0

寫這

getSupportActionBar().setDisplayShowHomeEnabled(true); 

剛過您設置工具欄

,並刪除

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

這將帶你到前面的任務,如果您有任何可能給你意想不到的行爲

+0

謝謝,試過了......沒有變化:( – 2015-03-31 08:03:55

2

試試這個佈局:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" 
    > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <include 
    android:id="@+id/tool_bar" 
    layout="@layout/tool_bar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    /> 

    <com.augury.mobile.auguryandroid.SlidingTabLayout 
    android:id="@+id/tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:elevation="2dp" 
    android:background="@color/augury_blue"/> 

    <android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:layout_weight="1"> 
    </android.support.v4.view.ViewPager> 

</LinearLayout> 

<!-- The navigation drawer --> 
<ListView android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:background="#111"/> 

</android.support.v4.widget.DrawerLayout> 
+0

完美!!謝謝!!!現在唯一的問題是,列表視圖太高,當它打開你可以看不到導航箭頭圖標.. – 2015-03-31 08:48:24

+0

哇,我完全忽略了佈局 – 2015-03-31 08:49:58

1

這是對我工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/relative" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" tools:context=".MainActivity"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     /> 

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

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <com.augury.mobile.auguryandroid.SlidingTabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:elevation="2dp" 
       android:background="@color/augury_blue"/> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/pager" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:layout_weight="1"> 
      </android.support.v4.view.ViewPager> 

     </LinearLayout> 

     <!-- The navigation drawer --> 
     <ListView android:id="@+id/left_drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="#111"/> 

    </android.support.v4.widget.DrawerLayout> 

</RelativeLayout>