ListFragment.onListItemClick() 未被调用;可能是由于导航抽屉

ListFragment.onListItemClick() not being called; possibly due to Navigation Drawer

我很困惑为什么我的 onListItemClick() 方法没有被调用。

我正在阅读有关该主题的 this question,它提到布局中的任何其他组件都需要设置 android:focusable="false"

但是,这是我的布局 class。如您所见,它没有其他组件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

我检查了父级 activity 的布局,它确实有一些其他组件,即抽屉布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

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

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>    

您可以看到我的 ListFragment 包含在名为 fragment_containerFrameLayout 中。

DrawerLayout 是否会阻止调用我的 onListItemClick() 方法? 而且,如果是这样,我怎样才能设置为不可聚焦(这是一个相当大的布局......我应该把 android:focusable="false" 放在哪里)?

我发现了这个问题。我添加了一个 FrameLayout 来包含我的 Fragment,但我现在看到 DrawerLayout 中已经包含了一个 FrameLayout(称为 content_frame)。所以我只是使用 content_frame 而不是 fragment_container.

解决方案 - 将 DrawerLayout 设置为基本布局。您不需要在抽屉布局之外使用 RelativeLayout,因为它没用。这可能是一个提示

你的布局应该是

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- Recommended to use only one as both are match_parent -->
        <FrameLayout 
            android:layout_height="match_parent" 
            android:layout_width="match_parent" 
            android:id="@+id/fragment_container"/>
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>