排列表视图的限制数量 [英] Limit number of rows of listview

查看:133
本文介绍了排列表视图的限制数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发,需要配对通过蓝牙等设备的应用程序。我是有以显示正确的方式配对的设备列表中的麻烦。我还认为,比如在Android的API(蓝牙聊天),但我有同样的问题。

I am developing an application that needs to pair with other devices through Bluetooth. I was having trouble in order to show the list of paired devices in correct manner. I had also viewed the example in the android api (Bluetooth Chat), but i was having the same problem.

配对设备列表是大的,然后隐藏搜索按钮是在列表的底部。

The list of paired devices are to big and then hides a search button that are at the bottom of the list.

我的XML code是非常相同的例子:

My xml code is very the same of the example:

<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>

不过,我不能显示底部的搜索按钮。 在这里,我的屏幕: 我的屏幕

But and i can't show the search button at the bottom. Here my screen: My Screen

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

You could see a bit of the button at the bottom of the dialog window.

这可能限制在列表视图显示的行数?谁能告诉我怎样才能解决这个问题。

It's possible to limit the number of rows shown at the listview ? Can anyone tell me how i can fix this problem

推荐答案

首先你code几点:

  • layout_weight 是,如果一个对象在一定的尺寸没有大小的唯一有意义的,那就是你设置 layout_height layout_width 0,因此这对你的code没有影响。
  • ListView的高度设置为 WRAP_CONTENT 是pretty的意义,即使它的工作原理是不好的做法。二者必选其一FILL_PARENT还是有一定的高度。
  • 在该按钮被隐藏,因为,按照上面的时候,你所创建的列表视图没有predefined大小,采取尽可能多的空间,因为他们可以,按下按钮关闭屏幕。
  • layout_weight is only meaningful if an object has no size in a certain dimension, that is you set layout_height or layout_width to 0, so this has no effect on your code.
  • Setting the height of a ListView to wrap_content is pretty meaningless, and even if it works it's bad practice. Use either 'fill_parent' or a definite height.
  • The button is hidden because, as per the point above, the ListViews you have created have no predefined size so take up as much space as they can, pushing the button off the screen.

让我们想想,你真的有存在 - 它只是一个单一的列表底部的一个按钮(是的,你可以在那里头或多个数据源,但我们会到这一点)

So let's think about what you really have there - it's just a single list with a button at the bottom (yes you may have headers or multiple sources of data in there but we'll get onto that).

下面的布局会给你一个ListView在顶部和底部按钮。在ListView将占用不正在使用的按钮的任​​何空间。请注意在按钮定义的布局ListView控件 - 这将导致Android的计算有多少高度的按钮占据了才考虑的ListView。然后,它给人的ListView的高度可用的其余部分。

The following layout will give you a ListView at the top and a Button at the bottom. The ListView will take up any space not being used by the Button. Notice how the Button is defined before the ListView in the layout - this causes Android to calculate how much height the button takes up before considering the ListView. It then gives the ListView the rest of the height available.

<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>

这就是布局。现在,让我们考虑您的列表的实际内容:你有一个头,然后配对设备的列表,紧接着又头,然后对新设备的列表。

So that's the layout. Now lets consider the actual content of your list: you have a header, followed by a list of paired devices, followed by another header and then a list of new devices.

您可以使用单个适配器创建此 - Commonsware提供了一个很好的实现称为 MergeAdapter 但你可以code本自己。 MergeAdapter不会直接让你的视图(如标题),但令人欣慰的Commonsware还提供了<一个href="https://github.com/commonsguy/cwac-sacklist/tree/master/src/com/commonsware/cwac/sacklist">SackOfViewsAdapter它可以让你的意见添加到它,然后你可以添加SackOfViewsAdapter到MergeAdapter。

You can create this using a single Adapter - Commonsware provides a very good implementation called MergeAdapter but you could code your own. MergeAdapter doesn't directly let you a view (e.g. for the headers) but thankfully Commonsware also provides the SackOfViewsAdapter which allows you to add views to it, and then you can add the SackOfViewsAdapter to the MergeAdapter.

下面是一些伪code这首先要做到的是什么介绍。

Here is some pseudo-code which should accomplish what is outlined above.

// 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)

制作看起来应该是布局这样。请注意我扩展列表以显示其与列表长于屏幕的行为;按钮仍然可见。红色的盒子,标志着屏幕的界限。

The layout produced should look something like this. Note I extended the list to show how it behaves with a list longer than the screen; the button still remains visible. The red box marks the bounds of the screen.

这篇关于排列表视图的限制数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆