【问题标题】:Android: ListView wont scroll when drawer is addedAndroid:添加抽屉时ListView不会滚动
【发布时间】:2016-04-27 07:17:41
【问题描述】:

我尝试使用ListView 将自定义Navigation Drawer 添加到Activity。 在我添加Navigation Drawer 之前,ListView 可以滚动并且您可以选择项目,但是当我添加抽屉时它不再这样做了。 当我在Navigation Drawer 上方添加ListView 时,Navigation Drawer 有效。 但是当我把ListView放在它下面时,Navigation Drawer就不起作用了。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.theoryx.androidapp.Search">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/snackbarPosition">
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

//<include layout="@layout/content_my_library" />  //<----here   

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

        <ListView
            android:id="@+id/navList"
            android:layout_width="200dp"
            android:layout_marginTop="55dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#ffffff"/>
    </android.support.v4.widget.DrawerLayout>

    <include layout="@layout/content_my_library" /> //when I put this above it the drawer works but then the listview doesn't scroll and cant be selected.

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

Content_my_library:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:padding="0dp"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.theoryx.androidapp.MyLibrary"
    tools:showIn="@layout/activity_my_library">

    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView>

</RelativeLayout>

【问题讨论】:

    标签: android listview drawerlayout navigation-drawer


    【解决方案1】:

    如果您想使用抽屉式导航,请将 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">
    
        <android.support.design.widget.CoordinatorLayout>
            <!-- All the things. -->
        </android.support.design.widget.CoordinatorLayout>
    
        <ListView
            android:id="@+id/navList"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 该代码与问题中的代码完全相同,单独没有任何意义。添加更多以展示您的想法。
    • 基本上抽屉布局必须是根元素,我不知道他想如何构造布局
    • 明白你的意思。但我不是在问这个问题。下次请详细一点。
    • 谢谢,下次会更具体的
    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多