【问题标题】:Bottom button bar overlaps the last element of Listview!底部按钮栏与 Listview 的最后一个元素重叠!
【发布时间】:2011-01-05 08:07:51
【问题描述】:

我有一个列表视图,它是活动的一部分。我希望用户可以选择批量删除列表视图中的项目,因此当他从菜单中选择相应的选项时,每个列表项旁边都会有一个复选框。当用户单击任何复选框时,按钮栏将从底部向上滑动(如在 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>

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

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

【问题讨论】:

  • 如果将 RelativeLayout 替换为与持有 ListView 的 LinearLayout 具有相同权重的 LinearLayout 会发生什么?

标签: android listview checkbox buttonbar


【解决方案1】:

没有对此进行测试,但我很确定是 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>

【讨论】:

  • 嘿帕特里克!!感谢您的帮助。它部分工作。部分原因是现在我不必手动设置底边距,所以这很有帮助。但是我仍然有以前与调整视图边距有关的问题:即,单击一个复选标记会尝试选中另一个复选框。当 framelayout 在列表视图上方添加按钮栏时,不会出现此问题。有什么想法可能导致这种情况吗?
  • 好问题.. 很难猜出为什么。如果你愿意分享一个小例子(整个项目的来源),也许我可以试着弄清楚。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 2019-08-18
    • 2016-10-13
    • 1970-01-01
    • 2018-11-04
    • 2021-01-01
    • 1970-01-01
    • 2021-08-31
    • 2014-04-26
    相关资源
    最近更新 更多