0

我使用ActionBarDrawerToggle來打開和關閉DrawerLayout。 但我的drawerlayout裏面的listView沒有顯示。NavigationDrawer ListView不顯示

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.left_drawer); 

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    mDrawerList.setBackgroundColor(getResources().getColor(R.color.abs__background_holo_light)); 
    mDrawerList.setAdapter(menuAdapter); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, 
      R.string.open, R.string.close) { 

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

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

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

它只顯示一個沒有條目的黑色抽屜。 drawerList應該是「setBackgroundColor」中設置的白色,並且drawerList應顯示適配器的條目。

當我用openDrawer(mDrawerList)打開抽屜時,它可以工作,但不能滑動。

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; 
    //} 

    switch (item.getItemId()) { 
    case android.R.id.home: 
     if (mDrawerLayout.isDrawerOpen(mDrawerList)) { 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      mDrawerLayout.openDrawer(mDrawerList); 
     } 
     break; 

下面是main.xml中的佈局與DrawerLayout:

<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 main content --> 

<fragment 
    android:id="@+id/activeRemindersList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    class="com.colapps.reminder.fragments.ActiveRemindersFragment" /> 

<!-- The navigation drawer --> 

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

任何人可以幫助?

UPDATE:

這個問題似乎是片段。 如果我添加的片段,而不是一個簡單的FrameLayout所有工作正常:

<!-- The main content --> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Test" /> 
</FrameLayout> 

<!-- The navigation drawer --> 

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

回答

0

問題解決了!

問題是,在片段佈局也是一個「DrawerLayout」。

我改變了很多,忘記從片段佈局中刪除它。

感謝所有試圖幫助我的人。

0

我敢肯定這有任何事做:

  • ListView沒有重量本身TY
  • ListView控件適配器是
  • ListView控件被隱藏在片段

嘗試改變layot文件,以便ListView控件具有重量特性以及和改變的高度財產片段。此外,把他們兩個在LinearLayout

還要確保您有thesemethods在活動覆蓋:

/** 
* 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); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // The action bar home/up action should open or close the drawer. 
    // ActionBarDrawerToggle will take care of this. 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } else 
     return false; 
} 

UPDATE:

使用onDrawerItemSelected(int pos)方法添加/替換片段。

@Override 
public void onDrawerItemSelected(int pos) {  

    // update the main content by replacing fragments 
    Fragment fragment = null; 

    switch (pos) { 
    case 0:   
     fragment = new FragOne(); 
     break; 
    case 1: 
     fragment = new FragTwo(); 
     break; 
    case 2: 
     fragment = new FragThree(); 
     break; 
    } 
    // R.id.content_frame is the id of the FrameLayout 
    getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit() 

    // update selected item then close the drawer 
    mDrawerList.setItemChecked(pos, true); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

這裏有一個詳細教程/例如如何實現NavigationDrawer。當我第一次使用NavigationDrawer時,我通過本教程學習並能夠成功實現它。

http://developer.android.com/training/implementing-navigation/nav-drawer.html

+0

在DrawerLayout上不需要重量,因爲它是重疊的。 它正在工作,如果我打電話「openDrawer」,並不使用ActionBarDrawerToggle。 至少它應該有設置backgroundcolor。 – chrisonline

+0

你能看看我更新的答案,看看它現在是否有效? –

+0

我已經在我的主要活動中使用過這些方法,但它不起作用。 – chrisonline

0

我認爲你缺少你的XML一個<FrameLayout>(注意,我有包裝的片段內容)。

它應該是這個樣子:

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 

    <FrameLayout 
     android:id="@+id/main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <fragment 
      android:id="@+id/app_info_fragment_phone" 
      ... /> 
    </FrameLayout> 

    <ListView 
     android:id="@+id/drawer" 
     ... /> 

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

沒有成功!如果我添加一個FrameLayout,則不顯示片段。然後我得到一個白色的空白頁面,並且抽屜只是像以前一樣黑。 – chrisonline

+0

您可能有多個問題。我非常肯定你需要FrameLayout(我從文檔中拿到了我的工作示例)。祝你好運。 – Booger

+0

@Booger:你能提供你的整個工作示例代碼嗎? – astuter