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

用 ListView 實作騰訊微博風格的列表介面

編輯:關於 Android 程式設計

效果如下圖:

這個介面的特徵是「頭像 + 名稱 + 時間 + 內容」的左側圖片式排列,實作起來最直覺的工具就是 SimpleAdapter——它把「資料 → 視圖」的映射關係用字串陣列宣告式地描述出來,省下了自訂 BaseAdapter 的麻煩。

SimpleAdapter 的建構子

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

各參數含義:

  • context:關聯 SimpleAdapter 運行的視圖上下文。
  • data:一個 Map 的清單。清單中每一項對應列表中的一行,每個 Map 都應包含 from 裡指定的所有 key。
  • resource:定義單行外觀的版面資源 ID,該版面至少要包含 to 陣列中所有 id 對應的 View。
  • from:Map 中的 key 名稱陣列,代表要取用的資料欄位。
  • to:目標 View 的 id 陣列,與 from 一一對應、順序必須一致。

最後一點特別重要:from 與 to 是靠索引配對的,順序錯了不會報錯,但資料會填到錯誤的位置上。

MainActivity

package cn.edu.bzu.micro_blog.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    private ListView listView;
    List<Map<String, ?>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.lv);

        SimpleAdapter simpleAdapter = new SimpleAdapter(
            MainActivity.this,
            getData(),
            R.layout.blog,
            new String[] { "name", "address", "photo" },
            new int[] { R.id.name, R.id.wenzi, R.id.photo });

        listView.setAdapter(simpleAdapter);
    }

    public List<Map<String, ?>> getData() {
        data = new ArrayList<>();

        Map<String, Object> data01 = new HashMap<>();
        data01.put("name", "張三");
        data01.put("address", "最近學習了 ListView 組件,於是就模仿了一下騰訊微博的樣式,看起來效果不錯");
        data01.put("photo", R.drawable.aa);
        data.add(data01);

        data01 = new HashMap<>();
        data01.put("name", "李四");
        data01.put("address", "只是模仿,全都是硬編碼的靜態資料,謝謝欣賞");
        data01.put("photo", R.drawable.th);
        data.add(data01);

        return data;
    }
}

主版面:activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_height="match_parent"
        android:layout_width="match_parent" >
    </ListView>
</RelativeLayout>

單行版面:blog.xml

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

    <ImageButton
        android:id="@+id/photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/th" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/photo"
        android:text="" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/name"
        android:layout_marginLeft="130dp"
        android:layout_marginTop="20dp"
        android:text="1分鐘前" />

    <TextView
        android:id="@+id/wenzi"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_toRightOf="@id/photo"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:text="" />
</RelativeLayout>

幾個可以改進的地方

  • 用 RelativeLayout 定位有更好的寫法。範例中「時間」用 layout_marginLeft="130dp" 硬推位置,換成 layout_alignParentRight="true" 靠右對齊會更穩健,不受螢幕寬度影響。
  • 時間欄位沒綁資料。若要動態帶入,需在 from/to 中加入 "time" 與 R.id.time。
  • 圖片不宜直接塞 R.drawable 的 id。SimpleAdapter 支援把 drawable 資源 id 放進 Map,但若要顯示網路圖片,就必須改用自訂 BaseAdapter 搭配圖片載入庫(Glide、Picasso 等)。
  • 硬編碼資料僅供示範。實務上資料來源應是資料庫、API 或 ViewModel,並透過 notifyDataSetChanged() 或 LiveData 驅動更新。
  • 現在新專案建議直接用 RecyclerView 取代 ListView,它強制 ViewHolder、版面管理器可插拔、動畫支援更完整。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved