为什么ListView单击嵌套布局不起作用?

时间:2014-05-24 17:35:35

标签: android android-layout listview onitemclicklistener

说明:我正在使用DrawerLayout来实施SlidingMenu

问题:LinearLayout id = bottom_layout )添加到 activity_main.xml 时,setOnItemClickListener不会不行。

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



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="2"       
        android:orientation="vertical"      
        >
         <LinearLayout
        android:id="@+id/main_activity_linear_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"       

        >
        <FrameLayout
            android:id="@+id/frame_container_slider"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"       

        />
        </LinearLayout>
    <!-- Framelayout to display Fragments -->
    <FrameLayout
               android:id="@+id/frame_container"        
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="1"
               android:paddingBottom="50dp"
        />

    </LinearLayout>
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:paddingBottom="10dp"            
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

    <!-- bunch of components here -->

        <LinearLayout
            android:id="@+id/bottom_layout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/spinner"
            android:layout_marginTop="5px"
            android:gravity="center_horizontal|bottom"
            android:paddingTop="2px" >

        <include 
            android:id="@+id/bottom_bar"
            layout="@layout/bottom_bar"
            android:visibility="visible" 
            />
        <include 
            android:id="@+id/bottom_bar_rechercher"
            layout="@layout/bottom_bar_rechercher"
            android:visibility="gone" 
            />

        </LinearLayout>
</android.support.v4.widget.DrawerLayout>

MainActivity.java

private ListView mDrawerList;
..
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
..
/*
         * Slide menu item click listener
         */
        private class SlideMenuClickListener implements
                ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // display view for selected nav drawer item
                displayView(position);
                if(position==0){                    
                    visibleSlider(); 
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

尝试在layout_weight和&#39; LinearLayout`中使用属性ListView

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1dp"
    android:paddingBottom="10dp"            
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

<!-- bunch of components here -->

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_below="@+id/spinner"
        android:layout_marginTop="5px"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2px" >

答案 1 :(得分:0)

LinearLayout中移除DrawerLayout (id = bottom_layout),如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:weightSum="2"       
        android:orientation="vertical"      
        >
         <LinearLayout
        android:id="@+id/main_activity_linear_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"       

        >
        <FrameLayout
            android:id="@+id/frame_container_slider"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"       

        />
        </LinearLayout>
    <!-- Framelayout to display Fragments -->
    <FrameLayout
               android:id="@+id/frame_container"        
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="1"
               android:paddingBottom="50dp"
        />

    </LinearLayout>
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:paddingBottom="10dp"            
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

    <!-- bunch of components here -->


</android.support.v4.widget.DrawerLayout>
<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/spinner"
            android:layout_marginTop="5px"
            android:gravity="center_horizontal|bottom"
            android:paddingTop="2px" >

        <include 
            android:id="@+id/bottom_bar"
            layout="@layout/bottom_bar"
            android:visibility="visible" 
            />
        <include 
            android:id="@+id/bottom_bar_rechercher"
            layout="@layout/bottom_bar_rechercher"
            android:visibility="gone" 
            />

        </LinearLayout>
</RelativeLayout>