編輯:關於 Android 程式設計
在 Android 早期開發裡,ListView 幾乎是列表類界面的標配。它能把一批同結構的數據(聯繫人、聊天記錄、菜單項)以可滾動、可復用的方式鋪排在屏幕上。本文以一個「展示用戶名稱與 IP」的小例子,把 ListView 的完整使用鏈路走一遍:數據準備 → 佈局搭建 → 適配器綁定 → 事件響應。
ListView 本身不關心數據長什麼樣,它只負責「滾動 + 回收」。真正決定每一行外觀的是三樣東西:
ArrayList<HashMap<String, String>>,每個 Map 代表一行;user.xml,描述一行裡有幾個 TextView;SimpleAdapter。Activity 直接繼承 ListActivity,這樣可以少寫一行 findViewById,用 setListAdapter() 即可完成綁定。
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 1. 準備數據:list 的每一個元素都是一個 map,代表列表中的一行
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
// 2. 往 map 中放入鍵值對,key 需要和佈局中的 id、適配器的映射保持一致
map1.put("name", "zhangsan");
map1.put("ip", "192.168.1.1");
map2.put("name", "lisi");
map2.put("ip", "192.168.1.2");
map3.put("name", "wangwu");
map3.put("ip", "192.168.1.3");
// 3. 把每一行裝進 list
list.add(map1);
list.add(map2);
list.add(map3);
// 4. 生成適配器:指定行佈局,以及「數據 key」與「控件 id」的對應關係
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.user,
new String[]{"name", "ip"},
new int[]{R.id.name, R.id.ip});
setListAdapter(adapter);
}
// 單擊某一行時回調,position 是位置,id 是行數據的標識
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("position = " + position + " , id = " + id);
}
}
這裡有一個容易踩的坑:SimpleAdapter 構造函數中的 new int[]{R.id.name, R.id.ip} 必須與 new String[]{"name", "ip"} 一一對應,順序錯了不會報錯,但界面上的文字會「串行」。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
</LinearLayout>
</LinearLayout>
使用 ListActivity 時,ListView 的 id 有約定:寫成 @id/android:list,系統會自動找到它。若使用普通 Activity,則可自行命名,再用 findViewById 獲取。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/mapLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="20dip"
android:gravity="center_vertical" />
<TextView
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip" />
</LinearLayout>
行佈局採用水平方向的 LinearLayout,左側固定寬度放名稱,右側 fill_parent 放 IP,這樣無論文字長短都不會互相擠壓。
運行後可以看到三行「名稱 + IP」的列表,點擊任意一行,控制台會打印出對應的 position 與 id。
回顧整個流程,ListView 的三件套缺一不可:數據源決定內容,行佈局決定樣式,適配器負責粘合。掌握了這條主線之後,當 SimpleAdapter 無法滿足複雜交互(例如每行帶圖片、按鈕、異步加載)時,就可以自然過渡到自定義 BaseAdapter,並配合 convertView 復用與 ViewHolder 模式做性能優化。
Android 熱修復原理與 Nuwa Gradle 插件源碼全解析
開源的熱修復方案已經不少,原理也大同小異。本文以 Nuwa 為例做一場徹底剖析。 專案地址:https://github.com/jasonross/Nuwa配套的打包插
Android 資源限定符的匹配規則:系統如何挑選最合適的資源
當我們用限定符修飾資源目錄(例如 drawable-en-ldpi)時,Android 究竟依照什麼規則來篩選?本文以 drawable 為例梳理整個匹配流程。內容參考官
Android 基本控件 ToggleButton 與 Switch 使用指南
ToggleButton(開關按鈕)與 Switch(開關)講解: 一、核心屬性講解: (1)ToggleButton textOn:按鈕被選中時顯示的文字 textOf
Android 自定義 View 實現圓形圖片效果
今天要講的是如何在 xml 文件中通過引用自定義的 View,實現 ImageView 的圓形圖片效果。首先在項目中新建一個類,我命名為 CircleImageView;