編輯:關於 Android 程式設計
ListView 是 Android 開發中出鏡率極高的列表控件,幾乎每個需要展示資料集合的介面都會用到它。ListView 裡的每一個子項(Item)可以只是一個字串,也可以是一個由多個控件組成的複合版面。本文就從原理到程式碼,把 ListView 的用法完整走一遍。
實現一個 ListView,大致可以拆成三步:
1. 準備好要顯示的資料;
2. 用一維或多維的動態陣列(如 ArrayList)來保存這些資料;
3. 建立一個「適配器」(Adapter)。簡單來說,適配器就是連接資料與 Item 的橋樑——動態陣列裡有多少筆資料,它就會生成多少個 Item;最後把適配器設定給 ListView 顯示出來。
理解「適配器」是掌握 ListView 的關鍵。你可以把它想像成一個翻譯官:一端對接你的資料來源,另一端對接 Item 的版面配置,負責把資料逐一填進對應的視覺元件裡。
首先是主版面 main.xml,內容很簡單,一個 LinearLayout 裡放一個 ListView 即可:
[plain]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout>
接著是 Item 的版面 my_listitem.xml,它決定了每一列的外觀。這裡用一個垂直 LinearLayout 包了兩個 TextView,一個當標題、一個當內容:
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/ItemTitle"
android:textSize="30dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/ItemText">
</TextView>
</LinearLayout>
這裡用到的幾個屬性值得說明:paddingBottom="3dip" 讓 Layout 底部留出 3 個像素的空白;paddingLeft="10dip" 讓左側留出 10 個像素;textSize="30dip" 則把標題字體設為 30 個像素大。
最後來到 Java 部分,這裡示範用 SimpleAdapter 把資料綁定到列表:
[javascript]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 綁定 XML 中的 ListView,作為 Item 的容器
ListView list = (ListView) findViewById(R.id.MyListView);
// 生成動態陣列並載入資料
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for(int i=0; i<30; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "This is Title.....");
map.put("ItemText", "This is text.....");
mylist.add(map);
}
// 建立適配器,把陣列對應到 ListItem
SimpleAdapter mSchedule = new SimpleAdapter(this,
mylist, // 資料來源
R.layout.my_listitem, // ListItem 的 XML 實現
new String[] {"ItemTitle","ItemText"}, // 動態陣列與 Item 對應的子項
new int[] {R.id.ItemTitle, R.id.ItemText}); // Item 裡兩個 TextView 的 ID
// 添加並顯示
list.setAdapter(mSchedule);
}
如果資料並非寫死,而是從 SQLite 資料庫讀取,做法也大同小異。先用 Cursor 遍歷查詢結果,把每一列的欄位塞進 HashMap,再交給 SimpleAdapter:
[java]
DBHelper helper=new DBHelper(this);
Cursor c=helper.query();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
while (c.moveToNext()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", c.getString(c.getColumnIndex("Title")));
map.put("date", c.getString(c.getColumnIndex("Date")));
mylist.add(map);
}
String[] from={"title","date"};
int[] to={R.id.ItemTitle,R.id.ItemDate};
SimpleAdapter ada=new SimpleAdapter(this, mylist, R.layout.listitem, from, to);
ListView list = (ListView) findViewById(R.id.MyListView);
list.setAdapter(ada);
歸根結柢,無論資料來自硬編碼的陣列還是資料庫查詢,ListView 的用法都離不開「準備資料 → 建立適配器 → 設定給 ListView」這條主線。掌握了這個套路,再複雜的列表需求也能迎刃而解。
魅藍 metal 能刷安卓嗎?刷回基於 Android 的 Flyme OS 教學
魅族這次推出的魅藍 metal 採用的是基於 YunOS 版本的 Flyme OS,一些喜歡折騰的用戶自然希望能刷回基於 Android 的 Flyme。那麼筆者就來提供
Android JNI 學習筆記(四):動手做個參數傳遞小範例
在前面第零篇粗略介紹了 JNI 的編程模式之後,接著的幾篇又針對 JNI 裡的一些核心概念做了初步說明。不知道大家讀下來是否清楚,但相信不少朋友和我一樣,剛接觸一樣新技術
安卓微信閃退怎麼辦?兩招搞定閃退與打不開問題
一提到 APP 閃退,許多人會先聯想到 iOS 裝置的問題,但其實閃退並非蘋果的「專利」。近期就有安卓用戶遇到微信閃退的情形,卻不知如何是好。以下就為大家提供解決手機微信
Android ListView 分組與懸浮 Header 實作方法
分組清單與懸浮標題是常見的資訊呈現方式。Android 原生 ListView 沒有直接提供這種效果,但可透過自訂 Adapter 與 Header View 模擬。