【问题标题】:NavigationDrawer ListView does not show up导航抽屉列表视图不显示
【发布时间】:2013-08-18 12:48:17
【问题描述】:

我使用 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);        

它只显示一个没有条目的黑色抽屉。 抽屉列表应该是白色的,如“setBackgroundColor”中设置的那样,抽屉列表应该显示来自适配器的条目。

当我用 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" />

有人可以帮忙吗?

更新:

问题似乎是片段。 如果我添加一个简单的 FrameLayout 而不是 Fragment,则一切正常:

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

【问题讨论】:

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


    【解决方案1】:

    问题解决了!

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

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

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

    【讨论】:

      【解决方案2】:

      我认为您的 XML 中缺少 &lt;FrameLayout&gt;(请注意我包装了片段内容的那个)。

      它应该看起来像这样:

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

      【讨论】:

      • 没有成功!如果我添加 FrameLayout,则不显示 Fragment。然后我得到一个白色的空白页面,抽屉像以前一样只有黑色。
      • 您可能有不止一个问题。我很确定您需要 FrameLayout(我的工作示例完全来自文档)。祝你好运。
      【解决方案3】:

      我很确定这与以下任何一种情况有关:

      • ListView 没有 weight 属性
      • ListView 适配器为空
      • ListView 被片段隐藏

      尝试更改layot 文件,使ListView 也具有权重属性,并更改Fragment 的高度属性。此外,将它们都放在 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)方法添加/替换Fragments。

      @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

      【讨论】:

      • 在 DrawerLayout 上不需要权重,因为它是重叠的。如果我调用“openDrawer”并且不使用 ActionBarDrawerToggle,它就可以工作。至少它应该有设置的背景色。
      • 你能看看我更新的答案,看看它现在是否有效吗?
      • 我的主要活动中已经有这个方法,但它不起作用。
      • 那么我完全没有想法了。我将不得不看到更多你的代码。请看这里:developer.android.com/training/implementing-navigation/…
      • 我想我找到了问题所在。看来我的片段是问题所在。如果我删除片段并在其中添加一个带有 TextView 的 FrameLayout,它就可以工作。如果我再次添加片段,我有一个黑色的空抽屉。但我不明白为什么。
      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      相关资源
      最近更新 更多