2011-01-05 47 views
2

我有一個列表視圖,它是一個活動的一部分。我希望用戶可以選擇批量刪除列表視圖中的項目,因此當他從菜單中選擇相應的選項時,每個列表項目都會在其旁邊有一個複選框。當用戶單擊任何複選框時,按鈕欄將從底部向上滑動(如在gmail應用程序中)並單擊刪除按鈕刪除所選項目,但單擊欄上的取消按鈕將取消選中所有選中的項目。底部按鈕欄與Listview的最後一個元素重疊!

這是我的網頁layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  
    android:background="@android:color/transparent" 
    > 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 

    <LinearLayout 
     android:id="@+id/list_area" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     > 
     <ListView 
      android:id="@+id/mylist" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" 
      android:drawSelectorOnTop="false" 
      android:layout_weight="1" 
     /> 

     <TextView 
     android:id="@+id/empty_list_message" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFF" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:text="@string/msg_for_emptyschd" 
     android:layout_margin="14dip" 
     android:layout_weight="1" 
     /> 
    </LinearLayout> 

    <RelativeLayout 
    android:id="@+id/bottom_action_bar" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/schedule_bottom_actionbar_border" 
    android:layout_marginBottom="2dip" 
    android:layout_gravity="bottom" 
    android:visibility="gone" 
    >  
    <Button 
     android:id="@+id/delete_selecteditems_button" 
     android:text="Deleted Selected" 
     android:layout_width="140dip" 
     android:layout_height="40dip" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="3dip" 
     android:layout_marginTop="3dip" 
    /> 

    <Button 
    android:id="@+id/cancel_button" 
     android:text="Cancel" 
     android:layout_width="140dip" 
     android:layout_height="40dip" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="3dip" 
     android:layout_marginTop="3dip" 
    />  
    </RelativeLayout>  
    </FrameLayout>   
</LinearLayout> 

到目前爲止,我已經得到了一切,除了工作時的底部欄變成在選中複選框可見,它重疊列表的最後一個元素。所有其他列表項目可以向上滾動,但不能向上滾動列表的最後一項,因此用戶無法選擇該項目,如果他打算。這裏是screenshot of the overlap

我已經嘗試使用listview頁腳選項,但將該欄添加到列表的末尾,而不是將其固定在屏幕的底部。有沒有辦法我可以「提升」listview足夠的重疊不會發生? 順便說一句,我已經嘗試向listview本身添加底部邊距,或者在使按鈕條可見之前將LinearLayout包裝到列表視圖中,但它引入了其他錯誤,例如單擊一個複選框來檢查ListView中的某個複選框。

+0

如果將RelativeLayout替換爲與LinearLayout具有相同權重的LinearLayout,並保存ListView,會發生什麼情況? – James 2011-01-05 08:46:43

回答

0

沒有測試這個,但我很確定它是造成這個:)的FrameLayout。因爲FrameLayout不關心你給它的「重量」。 FrameLayout只是把你插入的視圖放在彼此的前面。第二個是你給的,你說android:layout_gravity =「bottom」,它使它在底部對齊。但只是在列表視圖的前面。刪除FrameLayout,它應該工作,我認爲。

試試這樣說:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="@android:color/transparent"> 

    <LinearLayout android:id="@+id/list_area" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
     <ListView android:id="@+id/mylist" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:background="@android:color/transparent" 
      android:drawSelectorOnTop="false" android:layout_weight="1" /> 

     <TextView android:id="@+id/empty_list_message" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" android:layout_gravity="center_vertical|center_horizontal" 
      android:text="@string/msg_for_emptyschd" android:layout_margin="14dip" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <RelativeLayout android:id="@+id/bottom_action_bar" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="@drawable/schedule_bottom_actionbar_border" 
     android:layout_marginBottom="2dip" 
     android:visibility="gone"> 
     <Button android:id="@+id/delete_selecteditems_button" 
      android:text="Deleted Selected" android:layout_width="140dip" 
      android:layout_height="40dip" android:layout_alignParentLeft="true" 
      android:layout_marginLeft="3dip" android:layout_marginTop="3dip" /> 
     <Button android:id="@+id/cancel_button" android:text="Cancel" 
      android:layout_width="140dip" android:layout_height="40dip" 
      android:layout_alignParentRight="true" android:layout_marginRight="3dip" 
      android:layout_marginTop="3dip" /> 
    </RelativeLayout> 

</LinearLayout> 
+0

嘿帕特里克!感謝您的幫助。它部分工作。部分原因是,現在我不必手動設置底部邊距,這樣就有了幫助。但我仍然遇到了以前與調整視圖邊距有關的問題:即單擊一個複選標記試圖檢查另一個複選框。 framelayout添加列表視圖上方的按鈕欄時,不會出現此問題。任何想法可能造成這種情況? – elto 2011-01-05 09:45:14

+0

好問題..這是很難猜測爲什麼。如果你會分享一個小例子(整個項目的來源),那麼也許我可以試着弄明白。 – 2011-01-06 06:47:35

0

好像我已經解決了的奇怪行爲複選框的問題。首先,問題發生了,因爲一旦listview被重新調整大小,它就會重新繪製自己,因此getView()會再次調用所有可見列表項。由於我使用的是ViewHolder,並且視圖被重用,所以它似乎會導致相同的複選框被重新用於其他列表項目。因此,當我點擊一個複選框時,其他複選框被點擊。因爲此刷新列表視圖的調用僅在列表被調整大小時發生,並且只有在未標記其他複選框時在列表中標記複選框時纔會調整列表大小,因此在第一個複選框被標記後,此問題不再出現。

我通過創建自定義集合類來存儲選中的項目,並且在將項目放入此集合之前,我將單擊的複選框的標記和clicklistener克隆到新的複選框並將該新複選框立即存儲到採集。這解決了複選框的奇怪行爲。 這可能不是解決問題的最佳解決方案,所以如果你知道比這更好的東西,請分享。