简体   繁体   English

如何在一个屏幕中使用两个ListView?

[英]How to use Two ListView in one screen?

I want to use two list view in one ListActivity. 我想在一个ListActivity中使用两个列表视图。 How can I do this? 我怎样才能做到这一点? Please help me to create two different list view in one ListActivity. 请帮助我在一个ListActivity中创建两个不同的列表视图。

Thanks Deepak 谢谢Deepak

Just create an XML named main.xml file like: 只需创建一个名为main.xml的XML文件即可,例如:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  >
    <ListView android:id="@+id/ListView01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />
    <ListView android:id="@+id/ListView02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/ListView01"
        android:gravity="center_horizontal"
        />
</RelativeLayout>

Load this xml file via: setContentView(R.layout.main); 通过以下方式加载此xml文件: setContentView(R.layout.main); in your onCreate function. 在您的onCreate函数中。

ListView list1 = ((ListView) findViewById(R.id.ListView01));

allows you to access the first list and then you can apply your adapter to list1. 允许您访问第一个列表,然后将适配器应用于list1。 By exchanging 1 to 2 you can access the second ListView as well. 通过将1交换为2,您还可以访问第二个ListView。

You could also use a LinearLayout instead of a RelativeLayout depending upon how you want the listviews to display. 您还可以使用LinearLayout而不是RelativeLayout,具体取决于您希望列表视图显示的方式。 The following XML has two listviews, one on top of the other. 以下XML具有两个列表视图,一个位于另一个列表视图之上。 Each occupying half of the screen. 每个占据屏幕的一半。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#f00" >
    </ListView>

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#0f0" >
    </ListView>

</LinearLayout>

Accessing the listviews is the same regardless of the layout. 无论布局如何,访问列表视图都是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM