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

时间:2015-03-12 18:35:23

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

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

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

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

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

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

<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包含在名为FrameLayout的{​​{1}}中。

fragment_container是否可以阻止对DrawerLayout方法的调用? 而且,如果是这样,我怎么设置是不可聚焦的(它是一个非常大的布局......我应该把onListItemClick()放在哪里)?

2 个答案:

答案 0 :(得分:1)

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

您的布局应该是

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

答案 1 :(得分:0)

我发现了这个问题。我添加了FrameLayout以包含我的Fragment,但我现在看到DrawerLayout已经包含FrameLayout(称为content_frame)。所以我只使用content_frame代替fragment_container