限制ListView的行数

3
我正在开发一个需要通过蓝牙与其他设备配对的应用程序。但是我遇到了一个问题,就是无法正确显示已配对设备的列表。我也查看了Android API中的示例(蓝牙聊天),但是我仍然遇到同样的问题。
配对设备的列表过于庞大,因此隐藏了在列表底部的搜索按钮。
我的XML代码与示例非常相似:
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView android:id="@+id/listpaired_devices"
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/title_paired_devices"
    android:visibility="gone"
    android:background="#666"
    android:textColor="#fff"
    android:paddingLeft="5dp" 
  /> 
  <ListView android:id="@+id/paired_devices"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:layout_weight="1"
  />
  <TextView android:id="@+id/list_new_devices"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/title_other_devices"
    android:visibility="gone"
  /> 
  <ListView android:id="@+id/new_devices"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stackFromBottom="true"
    android:layout_weight="2" 
  />  
  <Button android:id="@+id/btscan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btscan"
  />
</LinearLayout>

但是我无法在底部显示搜索按钮。

这是我的屏幕:

My Screen

您可以看到对话框窗口底部的按钮的一部分。

是否有可能限制listview显示的行数? 有人能告诉我如何解决这个问题吗?

2个回答

5
首先,关于你的代码有几点需要注意:
  • layout_weight 只在对象在某个方向上没有尺寸时才有意义,也就是说你将 layout_height 或者 layout_width 设为 0 时,它才会起作用。所以它对你的代码没有影响。
  • 将 ListView 的高度设置为 wrap_content 意义不大,即使它能工作,这也是不好的做法。应该使用 'fill_parent' 或一个明确的高度。
  • 按钮被隐藏了,因为根据上面的原理,你创建的 ListView 没有预定义的大小,所以它们占据尽可能多的空间,将按钮推出屏幕。

那么让我们思考一下你实际上拥有的东西——只是一个带有底部按钮的单个列表(是的,你可能有标题或多个数据源,但我们将在后面讨论这些内容)。

以下布局将给你一个顶部的 ListView 和一个底部的 Button。ListView 将占用未被 Button 占用的任何空间。请注意,在布局中先定义 Button,然后再定义 ListView——这会导致 Android 在考虑 ListView 之前计算出 Button 占用的高度。然后它会给 ListView 剩下的可用高度。

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <Button android:id="@+id/btscan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/btscan"
  />
  <ListView android:id="@+id/all_devices"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/btscan"
    android:stackFromBottom="true"
  />  
</RelativeLayout>

那么这就是布局。现在让我们考虑您列表的实际内容:您有一个标题,后面是一对设备的列表,再后面是另一个标题,然后是新设备的列表。
您可以使用单个Adapter来创建此内容-Commonsware提供了一个非常好的实现称为MergeAdapter,但您也可以编写自己的代码。MergeAdapter不直接让您查看(例如用于标题),但幸运的是,Commonsware还提供了SackOfViewsAdapter,它允许您将视图添加到其中,然后您可以将SackOfViewsAdapter添加到MergeAdapter中。
这是一些伪代码,应该可以完成上述内容的说明。
// This is the adapter we will use for our list with ListView.setAdapter
MergeAdapter listAdapter = new MergeAdapter();

// The first header - you'll need to inflate the actual View (myHeaderView1) from some resource
// using LayoutInflater
List<View> firstHeaderViews = new List<View();
firstHeaderViews.add(myHeaderView1);
SackOfViewsAdapter firstHeaderAdapter = new SackOfViewsAdapter(firstHeaderViews)

// Second header
List<View> secondHeaderViews = new List<View();
secondHeaderViews.add(myHeaderView2);
SackOfViewsAdapter secondHeaderAdapter = new SackOfViewsAdapter(secondHeaderViews);

// Now to add everything to the MergeAdapter

// First goes the first header
listAdapter.addAdapter(firstHeaderAdapter);

// Now your first list
listAdapter.addAdapter(firstListAdapter);

// Now your second header
listAdapter.addAdapter(secondHeaderAdapter);

// Now your second list
listAdapter.addAdapter(secondListAdapter);

// Now set the adapter to the list
myList.setAdapter(listAdapter)

生成的布局应该看起来像这样。请注意,我扩展了列表以显示它在超过屏幕长度的列表下的行为;按钮仍然可见。红色框标记了屏幕的边界。


那么我们无法指定“可见的最大行数”吗? - M. Usman Khan

2
您可以限制列表视图中显示的行数,但不确定这是否真正有助于您实现的目标,因为您将隐藏对用户可能很重要的信息。您可以通过限制传递给listview适配器的项目数量来限制行数,或者在getView方法中将可见性设置为“gone”,当列表视图达到某个位置时(您可以检查getView()的“position”参数)。
然而,我建议您仅使用一个列表视图(为列表项的视图添加“新/其他设备”标题的分隔符,但默认情况下隐藏它,然后,如suri已经建议的那样,为listview使用头部和尾部(以放置扫描按钮)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接