【问题标题】:Multiple ListViews with Custom Adapters in a Single Layout单个布局中带有自定义适配器的多个 ListView
【发布时间】:2012-06-26 10:04:58
【问题描述】:

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

当我需要使用自定义适配器时,我用id=android:list 创建了一个ListView,并将类设置为Extends ListActivity。但现在我觉得我做不到……

【问题讨论】:

    标签: android listview listactivity tabnavigator


    【解决方案1】:

    要在单个 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());
    

    【讨论】:

    • 我可以在没有 ListActivity 的情况下设置 CustomAdapters 吗?
    • 是的,当然,只需创建两个不同的适配器类并在您的列表视图中随意使用它们。检查我更新的答案。
    【解决方案2】:

    @努诺·贡萨尔维斯

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

    【讨论】:

    • 我没有注意这一点,因为这不是重点。 :) 无论如何感谢您的更正。
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多