2015-02-11 97 views
0

我有Android佈局(如下所示)。 我設置了一個像這樣的偵聽器:list.setOnItemClickListener。 一切似乎都很好。Android列表視圖setOnItemClickListener僅適用於

在下面的一行:

<ScrollView android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

如果我改變安卓:layout_height從「WRAP_CONTENT」到「FILL_PARENT」它不工作了(從我的列表中的項目不能被選中)。
它只有在設置爲wrap_content時纔有效。 有人可以解釋爲什麼?!??

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <TabWidget android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
      /> 
    <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
      > 
     <ListView android:id="@+id/restaurants" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
       /> 
     <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

      <TableLayout android:id="@+id/details" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:stretchColumns="1" 
         android:shrinkColumns="1" 
         android:paddingTop="4dip" 
        > 
       <TableRow> 
        <TextView android:text="@string/name" /> 
        <EditText android:id="@+id/name" /> 
       </TableRow> 
       <TableRow> 
        <TextView android:text="@string/address" /> 
        <EditText android:id="@+id/addr" /> 
       </TableRow> 
       <TableRow> 
        <TextView android:text="Type:" /> 
        <RadioGroup android:id="@+id/types"> 
         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/take_out" 
            android:text="@string/takeout" 
            android:checked="true" 
           /> 

         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/sit_down" 
            android:text="@string/sitdown" 
           /> 
         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/delivery" 
            android:text="@string/delivery" 
           /> 
        </RadioGroup> 
       </TableRow> 

       <TableRow> 
        <TextView android:text="@string/notes" /> 
        <EditText android:id="@+id/notes" /> 
       </TableRow> 

       <Button android:id="@+id/save" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/save" 
         /> 
      </TableLayout> 
     </ScrollView> 
    </FrameLayout> 
</LinearLayout> 

+0

它不會工作,因爲使用'fill_parent'將嘗試填充'FrameLayout'內的所有區域,但也存在'ListView android:id =「@ + id/restaurants」'。也許一個解決方案將除了'ScrollView'在另一個'RelativeView',並將其放置在ListView android:id =「@ + id/restaurants」' – hrskrs 2015-02-11 11:50:55

+0

做一個測試:將listView背景顏色更改爲黑色,將ScrollView背景顏色更改爲紅。看看你的工作看法。它可以填充listView上方的所有空間。 – 2015-02-11 12:21:35

回答

0

fill_parent(棄用,在API級別8改名爲MATCH_PARENT及更高版本)

設置一個小部件的佈局FILL_PARENT將迫使它擴大到佔據儘可能多的它們的佈局元素中可用的空間大致相當於將Windows窗體控件的dockstyle設置爲Fill

將頂層佈局或控件設置爲fill_parent將強制它佔據整個屏幕。

wrap_content

設置視圖的尺寸爲wrap_content將迫使它擴大隻是遠遠不夠,以遏制它所包含的值(或子控件)。對於控件 - 如文本框(TextView)或圖像(ImageView) - 這將包裝顯示的文本或圖像。對於佈局元素,它將調整佈局大小以適合添加爲其子元素的控件/佈局。

在Android代碼文檔here中有一些細節。

0

您的ScrollView佔用了ListView中的所有可用空間,這些空間可以填充到屏幕上,基本上覆蓋了它。

您絕對不應該將ScrollView與ListView一起使用,因爲ListView會負責自己的垂直滾動。最重要的是,這樣做會挫敗ListView中用於處理大型列表的所有重要優化,因爲它有效地強制ListView顯示其整個項目列表以填充由ScrollView提供的無限容器。 - 從http://developer.android.com/reference/android/widget/ScrollView.html

相關問題