2

我有一个列表视图,它是活动的一部分。我希望用户可以选择批量删除列表视图中的项目,因此当他从菜单中选择相应的选项时,每个列表项旁边都会有一个复选框。当用户单击任何复选框时,按钮栏将从底部向上滑动(如在 gmail 应用程序中)并单击删除按钮删除所选项目,但是单击栏上的取消按钮将取消选中所有选中的项目。

这是我的页面 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>

到目前为止,我已经完成了所有工作,除了当底部栏在复选框选择时变得可见时,它与列表的最后一个元素重叠。所有其他列表项都可以向上滚动,但您不能向上滚动列表的最后一项,因此如果用户愿意,则不能选择该项目。这是重叠的屏幕截图

我曾尝试使用 listview 页脚选项,但这会将栏附加到列表的末尾,而不是将其固定在屏幕底部。有没有办法我可以“提高”列表视图足以使重叠不会发生?顺便说一句,我已经尝试将底部边距添加到列表视图本身,或者在使按钮栏可见之前包装列表视图的线性布局,但它引入了其他错误,例如单击一个复选框会检查列表视图中的另一个复选框。

4

2 回答 2

0

好像我已经解决了奇怪的复选框行为的问题。首先,问题发生了,因为一旦列表视图调整大小,它就会重新绘制自己,因此 getView() 会再次为所有可见列表项调用。由于我使用的是 ViewHolder,并且视图正在被重用,因此似乎它会导致相同的复选框再次被其他列表项重用。因此,当我单击一个复选框时,会单击另一个复选框。因为,仅在调整列表大小时才发生刷新列表视图的调用,并且仅在未标记其他复选框时标记列表中的复选框时才调整列表大小,因此在第一个复选框被标记后,此问题不会再次出现.

我通过创建一个自定义集合类来存储选中的项目来解决该问题,并且在将项目放入此集合之前,我将单击的复选框的标签和 clicklistener 克隆到一个新的复选框中,并将该新复选框现在存储到集合中。这解决了复选框的奇怪行为。这可能不是解决问题的最佳方法,所以如果您知道比这更好的方法,请分享。

于 2011-01-07T09:06:10.887 回答
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>
于 2011-01-05T08:49:31.423 回答