开发者

How to use Two ListView in one screen?

开发者 https://www.devze.com 2023-01-10 04:41 出处:网络
I want to use two list view in one ListActivity. How can I do this? P开发者_开发知识库lease help me to create two different list view in one ListActivity.

I want to use two list view in one ListActivity. How can I do this? P开发者_开发知识库lease 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消