简体   繁体   中英

How to use Two ListView in one screen?

I want to use two list view in one ListActivity. How can I do this? Please help me to create two different list view in one ListActivity.

Thanks Deepak

Just create an XML named main.xml file like:

<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); in your onCreate function.

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

allows you to access the first list and then you can apply your adapter to list1. By exchanging 1 to 2 you can access the second ListView as well.

You could also use a LinearLayout instead of a RelativeLayout depending upon how you want the listviews to display. The following XML has two listviews, one on top of the other. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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