Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 列表容器 ListView 入門(一)

編輯:關於 Android 程式設計

ListView 是一個用於列表展示的容器,內部可容納多個資料項。資料項既可以是只含一個 TextView 的簡單布局,也可以是多控件組合的複雜布局。

繼承關係

java.lang.Object
  └ android.view.View
      └ android.view.ViewGroup
          └ android.widget.AdapterView<ListAdapter>
              └ android.widget.AbsListView
                  └ android.widget.ListView

官方文件中這樣描述:一個以垂直捲動方式顯示項目的檢視,項目來自與該檢視綁定的 ListAdapter。

小提醒:有一種情況下 ListView 可能無法正常發揮——當它被放在 ScrollView 之下時。由於兩者都具備捲動能力,嵌套後往往只會顯示第一筆資料,實務上應避免這種寫法。

一、最簡單的 ListView

用法類似 Spinner:準備一個資料源(最簡單是陣列)、一個 Adapter,再透過 setAdapter() 將兩者綁定即可。

ListView lv_01 = (ListView) findViewById(R.id.lv_listview01);
String[] items = new String[]{"逍遙", "靈兒", "月如"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv_01.setAdapter(adapter);

二、自訂的 ListView

setAdapter(ListAdapter adapter) 負責為 ListView 設定資料。傳入的 adapter 可能會依據 ListView 目前使用的功能(例如加入頁首或頁尾)而被 WrapperListAdapter 包裝。

由此可見,ListView 的樣式是透過 Adapter 來決定的。ListAdapter 是 ListView 與其資料之間的橋樑,資料通常來自 Cursor,但並非必要——只要資料被 ListAdapter 封裝,ListView 便能顯示。

歸納起來,打造一個自訂 ListView 需要三個要素:

・資料源:陣列或 List<Map>

・Adapter:SimpleAdapter,擴充性佳,可組合多種控件

・布局:layout.xml

搞懂 SimpleAdapter 的參數

SimpleAdapter 的參數眾多,讓不少初學者頭疼,先看構造函數:

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

參數說明:

context:目前上下文。

data:一個裝有 Map 的 List 集合,List 中每一項對應列表的一行,該 Map 應包含 from 中指定的所有鍵。

resource:定義每一項樣式的布局檔 id,該布局至少要包含 to 中所定義的檢視。

from:欲加入的欄位名稱清單(對應 Map 的 key)。

to:用來顯示 from 各欄位的檢視,且必須都是 TextView。

用例子會更清楚:

SimpleAdapter sa = new SimpleAdapter(this, getData(), R.layout.item_listview,
    new String[]{"icon", "content", "content1"},
    new int[]{R.id.iv_icon, R.id.tv_content, R.id.tv_content1});
private List<Map<String, Object>> getData() {
    List<Map<String, Object>> myList = new ArrayList<Map<String, Object>>();

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("icon", R.drawable.ic_launcher);
    map.put("content", "xiaoyao");
    map.put("content1", "男主角");
    myList.add(map);

    map = new HashMap<String, Object>();
    map.put("icon", R.drawable.ic_launcher);
    map.put("content", "linger");
    map.put("content1", "女主角");
    myList.add(map);

    map = new HashMap<String, Object>();
    map.put("icon", R.drawable.ic_launcher);
    map.put("content", "月如");
    map.put("content1", "女主角");
    myList.add(map);

    map = new HashMap<String, Object>();
    map.put("icon", R.drawable.ic_launcher);
    map.put("content", "tangyu");
    map.put("content1", "男主角");
    myList.add(map);

    return myList;
}

對照可知,from 中的參數其實就是 Map 的 key。而 resource 描述的布局,其內含的 id 至少要涵蓋 to 所列出的那些,且實際呈現順序即依照該清單排列。

完整範例

以下 MainActivity 繼承了 ListActivity,因此不必在 XML 中另外定義 ListView:

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter sa = new SimpleAdapter(this, getData(), R.layout.item_listview,
            new String[]{"icon", "content", "content1"},
            new int[]{R.id.iv_icon, R.id.tv_content, R.id.tv_content1});
        this.setListAdapter(sa);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("abc", getData().get(position).toString());
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> myList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("icon", R.drawable.ic_launcher);
        map.put("content", "xiaoyao");
        map.put("content1", "男主角");
        myList.add(map);
        // 其餘資料項依此類推
        return myList;
    }
}

item_listview.xml:

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

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tv_content1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>

由於直接繼承了 ListActivity,因此只要重寫 onListItemClick() 回呼方法,即可取得點擊項目的相關資訊。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved