1. 程式人生 > >Android-ListView中巢狀(ListView)控制元件時item的點選事件不起作用的問題

Android-ListView中巢狀(ListView)控制元件時item的點選事件不起作用的問題

解決: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>