【发布时间】:2012-06-26 10:04:58
【问题描述】:
我创建了一个自定义选项卡导航器,现在我需要知道如何使用多个接收自定义适配器的 ListView。
当我需要使用自定义适配器时,我用id=android:list 创建了一个ListView,并将类设置为Extends ListActivity。但现在我觉得我做不到……
【问题讨论】:
标签: android listview listactivity tabnavigator
我创建了一个自定义选项卡导航器,现在我需要知道如何使用多个接收自定义适配器的 ListView。
当我需要使用自定义适配器时,我用id=android:list 创建了一个ListView,并将类设置为Extends ListActivity。但现在我觉得我做不到……
【问题讨论】:
标签: android listview listactivity tabnavigator
要在单个 Activity 上有多个 listView,不需要扩展 ListActivity。只需将普通 ListViews 添加到 xml lauyout 文件并在活动中引用它们并设置所需的适配器。
示例:xml 文件
<ListView android:id="@+id/list_view1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<ListView android:id="@+id/list_view2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
关于活动:
setContentView(R.layout.xmlfile)...
ListView lv1 = (ListView) findViewById(R.id.list_view1);
ListView lv2 = (ListView) findViewById(R.id.list_view2);
lv1.setAdaper(new CustomAdapter1());
lv2.setAdaper(new CustomAdapter2());
【讨论】:
@努诺·贡萨尔维斯
您的 XML 文件中的一个小错误/优化:
对于 ListViews,最好将 layout_height 和 layout_width 属性都定义为 fill_parent 并使用 layout_gravity 属性对其进行缩放。将 ListView 的 layout_height 设置为 wrap_content 不是最佳选择,可能会导致错误或性能问题。
但是你的解决方案在这种情况下可以工作:)
例子:
<ListView android:id="@+id/list_view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>
<ListView android:id="@+id/list_view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>
【讨论】: