編輯:關於 Android 程式設計
效果如下圖:

這個介面的特徵是「頭像 + 名稱 + 時間 + 內容」的左側圖片式排列,實作起來最直覺的工具就是 SimpleAdapter——它把「資料 → 視圖」的映射關係用字串陣列宣告式地描述出來,省下了自訂 BaseAdapter 的麻煩。
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 是靠索引配對的,順序錯了不會報錯,但資料會填到錯誤的位置上。
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;
}
}
<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>
<?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>
layout_marginLeft="130dp" 硬推位置,換成 layout_alignParentRight="true" 靠右對齊會更穩健,不受螢幕寬度影響。from/to 中加入 "time" 與 R.id.time。notifyDataSetChanged() 或 LiveData 驅動更新。
應用耗電分析與優化:Battery Historian 實戰指南
在 Android 專案裡,電量消耗一直是最難量化、卻又最被用戶敏感的指標。使用者未必說得出你的產品有多少功能,但只要手機待機掉得比別人快,卸載的決定往往下得毫不留情。指
Android 版本控制環境建置:SVN 與 Git 入門
一、建立虛擬機測試環境 在 Android 開發與版本控制學習中,虛擬機是很實用的測試環境。常見的虛擬機軟體包括 VMware Workstation 與 Oracle
Android 仿外送點餐頁面的二級聯動列表
互動場景說明外送或點餐應用常見「左側分類、右側內容」的布局。左側是分類索引,右側是餐品列表;當右側捲動時,左側會自動對應到目前分類;點擊左側分類時,右側也會捲動到相應位置
用 AOP 打造 Android 遠程調用 SDK:動態代理實戰解析
「什麼是 Spring AOP?」——這道面試題一出,我腦海裡瞬間湧現出事務管理、SDK、程式碼監控、Spring Remoting 一大堆概念,反而一時語塞。後來想想,