簡體   English   中英

如何在一個屏幕中使用兩個ListView?

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

我想在一個ListActivity中使用兩個列表視圖。 我怎樣才能做到這一點? 請幫助我在一個ListActivity中創建兩個不同的列表視圖。

謝謝Deepak

只需創建一個名為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>

通過以下方式加載此xml文件: setContentView(R.layout.main); 在您的onCreate函數中。

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

允許您訪問第一個列表,然后將適配器應用於list1。 通過將1交換為2,您還可以訪問第二個ListView。

您還可以使用LinearLayout而不是RelativeLayout,具體取決於您希望列表視圖顯示的方式。 以下XML具有兩個列表視圖,一個位於另一個列表視圖之上。 每個占據屏幕的一半。

<?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>

無論布局如何,訪問列表視圖都是相同的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM