当前位置 : 主页 > 手机开发 > android >

android – 限制listview的行数

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在开发一个需要通过蓝牙与其他设备配对的应用程序.我遇到了麻烦,以正确的方式显示配对设备的列表.我也在 android api(蓝牙聊天)中查看了这个例子,但我遇到了同样的问题. 配对设
我正在开发一个需要通过蓝牙与其他设备配对的应用程序.我遇到了麻烦,以正确的方式显示配对设备的列表.我也在 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

您可以在对话框窗口的底部看到一些按钮.

可以限制列表视图中显示的行数吗?任何人都可以告诉我如何解决这个问题

首先是关于你的代码的几点:

> layout_weight仅在对象在特定维度中没有大小时才有意义,即将layout_height或layout_width设置为0,因此这对您的代码没有影响.
>将ListView的高度设置为wrap_content是没有意义的,即使它起作用也是不好的做法.使用’fill_parent’或确定的高度.
>按钮被隐藏,因为根据上面的点,您创建的ListView没有预定义的大小,因此占用尽可能多的空间,将按钮从屏幕上移开.

所以让我们考虑一下你真正拥有的东西 – 它只是一个单独的列表,底部有一个按钮(是的,你可能有标题或多个数据源,但我们会进入那个).

以下布局将在顶部提供ListView,在底部提供Button. ListView将占用Button未使用的任何空间.注意如何在布局中的ListView之前定义Button – 这会导致Android在考虑ListView之前计算按钮占用的高度.然后它为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>

这就是布局.现在让我们考虑列表的实际内容:您有一个标题,后面跟着一个配对设备列表,然后是另一个标题,然后是新设备列表.

您可以使用单个适配器创建它 – 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)

生成的布局应该看起来像like this.注意我扩展了列表以显示它的行为与列表长于屏幕的行为;按钮仍然可见.红色框标记屏幕的边界.

网友评论