导航抽屉ListView不显示

时间:2013-08-18 12:48:18

标签: android android-layout android-listview navigation-drawer android-layout-weight

我使用ActionBarDrawerToggle来打开和关闭DrawerLayout。 但是我的drawerlayout中的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;

这是带有DrawerLayout的main.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" >

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

有人可以帮忙吗?

更新

问题似乎是片段。 如果我添加而不是Fragment一个简单的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" />

3 个答案:

答案 0 :(得分:1)

问题解决了!

问题在于Fragment Layout也是一个“DrawerLayout”。

我已经改变了很多,忘记将它从片段布局中删除。

感谢所有试图帮助我的人。

答案 1 :(得分:0)

我很确定这与之有关:

  • ListView没有权重属性
  • ListView适配器为空
  • ListView被隐藏

尝试更改layot文件,以便ListView也具有权重属性并更改Fragment的height属性。此外,将它们都放在 LinearLayout

同时确保您在“活动”中覆盖了这些方法:

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

<强>更新

使用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

答案 2 :(得分: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>