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

Android 開發系列(十九):為 SimpleAdapter 自訂樣式

編輯:關於 Android 程式設計

Adapter 的核心作用,是把資料處理好之後再呈現到視圖上。對一般的 ArrayAdapter 來說,只要把一個陣列與一個樣式傳給它,就能用列表把這個字串陣列顯示出來。例如:

listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ls));

然而,像手機聯絡人那樣的介面,每一列除了文字之外,還可能帶有頭像與電話號碼,這種複雜的視圖是單靠 ArrayAdapter 無法完成的。此時我們可以改用 SimpleAdapter,只不過需要先自行設計版面樣式。

一、定義列表容器

先建立一個 Android 專案,在 layout 目錄下定義一個 ListView 用來承載列表。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <!-- 定義一個 List -->
    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

二、撰寫 Java 程式碼

SimpleAdapterTest.java:

package org.crazyit.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SimpleAdapterTest extends Activity {
    private String[] names = new String[] { "虎頭", "弄玉", "李清照", "李白" };
    private String[] descs = new String[] { "可愛的小孩", "一個擅長音樂的女孩", "一個擅長文學的女性", "浪漫主義詩人" };
    // 這是三張圖片的 id 集合
    private int[] imageIds = new int[] { R.drawable.tiger, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 創建一個 List 集合,元素是 Map
        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> listItem = new HashMap<String, Object>();
            listItem.put("header", imageIds[i]);
            listItem.put("personName", names[i]);
            listItem.put("desc", descs[i]);
            listItems.add(listItem);
        }
        // 創建一個 SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.simple_item,
                new String[] { "personName", "header", "desc" },
                new int[] { R.id.name, R.id.header, R.id.desc });
        ListView list = (ListView) findViewById(R.id.mylist);
        list.setAdapter(simpleAdapter);
        // 列表項單擊事件
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), names[position] + "被單擊了", 1).show();
            }
        });
        list.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), names[position] + "被選中了", 1).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });
    }
}

三、自訂列表項樣式

在建立 SimpleAdapter 時,我們指定了自訂樣式 R.layout.simple_item,正是靠它才能呈現出複雜的列表項。

simple_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- 用於顯示頭像 -->
    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 姓名 -->
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f0f"
            android:paddingLeft="10dp" />
        <!-- 描述 -->
        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:paddingLeft="10dp" />
    </LinearLayout>
</LinearLayout>

把上述內容組合起來,就能得到帶有頭像、姓名與描述的複雜列表效果:

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