Android-ListView中嵌套(ListView)控件时item的点击事件不起作用的问题

来源:互联网 发布:网络思想政治教育好处 编辑:程序博客网 时间:2024/06/07 00:19
解决:1、在主listview布局文件中的listview中添加属性 android:focusable="false"   
           2、在子listview中最顶上的布局文件添加属性 android:descendantFocusability="blocksDescendants"

另外,listview嵌套时,主listview的adapter执行一下这行代码,listview的最后一个item才不会异常
  adapter.getView(adapter.getCount-1, null,listview);
listview.setAdapter(adapter);
例子如下


主Listview的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_company_uniqueexhibition"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:background="#FFFFFF"
    android:gravity="center_horizontal" >
                
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
                <ListView
                    android:id="@+id/head_listview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:cacheColorHint="@android:color/transparent"
                    android:divider="@color/grey"
                    android:dividerHeight="1dp"
                    android:drawSelectorOnTop="false"
                    android:fadingEdge="none"
                    android:focusable="false"                    
                    android:scrollbars="vertical" />         
               
                 
    </LinearLayout>
</RelativeLayout>
》》》》》
listview 嵌套的子listview的布局文件
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
   
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
     
        
        <ListView
            android:id="@+id/item_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="@android:color/transparent"
            android:divider="@color/grey"
            android:dividerHeight="0dp"
            android:drawSelectorOnTop="false"
            android:fadingEdge="none"            
            android:scrollbars="vertical" />
      
    </LinearLayout>
 
</LinearLayout>