【问题标题】:ListView not scrolling in nav_headerListView 不在 nav_header 中滚动
【发布时间】:2017-06-12 13:51:50
【问题描述】:

我试图在我的导航抽屉标题中添加一个listView。我有列表,它填充得很好,唯一的问题是我无法滚动列表。似乎nav drawer 本身正在拦截滚动动作并且不允许我滚动ListView

nav_header_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical">

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

和抽屉

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fitsSystemWindows="true"
   tools:openDrawer="start">

<include
    layout="@layout/app_bar_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_drawer_drawer" />

如果您需要更多文件,我可以发布

【问题讨论】:

  • 只有当子视图大于容器时,ListView 中有多少项目才会滚动?
  • @Yupi 列表是动态生成的
  • 你需要添加固定大小的数组..
  • @Javacoder 这违背了我想要做的事情的目的。我想要一个动态列表,它只需要滚动
  • 任何有此问题的人都可以在这里找到答案stackoverflow.com/a/14577399/5202215

标签: android listview navigation-drawer


【解决方案1】:

您可以根据需要自定义抽屉式导航。设计一个单独的布局,比如 nav_layout.xml 并在你的 activity_home.xml 中用下面的代码替换你的导航视图。

 <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <include
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            layout="@layout/nav_layout" />


    </android.support.design.widget.NavigationView>

现在打开您的 nav_layout.xml 并输入以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical">

    <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

通过这种方式,您可以根据需要自定义抽屉式导航。

【讨论】:

  • 我使用了您的答案并且效果很好,直到我意识到菜单项(共享、发送)消失了。我问了一个新问题here。任何进一步的帮助将不胜感激!
  • 嗨,它仍然没有滚动任何想法吗?
【解决方案2】:

对于 listview xml,尝试添加一个或两个:

android:focusable="true" android:focusableInTouchMode="true"

我必须为在列表视图中创建的未响应触摸事件的按钮执行此操作。

【讨论】:

  • 好主意,但无济于事
猜你喜欢
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多