单个布局中具有自定义适配器的多个ListView

时间:2012-06-26 10:04:59

标签: android listview listactivity tabnavigator

我创建了一个自定义选项卡导航器,现在我需要知道如何使用多个将接收自定义适配器的ListView。

当我需要使用自定义适配器时,我使用ListView创建了id=android:list并将类设置为Extends ListActivity。但现在我想我不能那样做......

2 个答案:

答案 0 :(得分:3)

要在单个活动上拥有多个listView,不需要扩展ListActivity。只需将正常的ListView添加到xml lauyout文件中,然后在活动上引用它们并设置所需的适配器。

示例:xmlfile

<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());

答案 1 :(得分:1)

@NunoGonçalves

XML文件中的一个小错误/优化:

对于ListViews,最好将layout_heightlayout_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>
相关问题