編輯:關於 Android 程式設計
ListView 大概是 Android 最常使用的列表元件。最經典的組合是 ListActivity + SimpleAdapter:資料放進 ArrayList<HashMap>,再指定每一列要用哪個版面檔、哪些欄位對應到哪些控制項,就能快速把清單顯示出來。
如果 Activity 繼承 ListActivity,版面中的 ListView 一定要使用系統保留的 id @id/android:list。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
</ListView>
</LinearLayout>
</LinearLayout>
這一列要顯示兩個 TextView:使用者名稱與 IP。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/user_name"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingTop="2dip"
android:textSize="10pt"/>
<TextView
android:id="@+id/user_ip"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="10pt"/>
</LinearLayout>
package com.example.wenandroid;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class ListViewMain extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>();
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("user_name", "張三");
map1.put("user_ip", "192.168.0.110");
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("user_name", "李四");
map2.put("user_ip", "192.168.0.111");
list.add(map1);
list.add(map2);
SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user,
new String[] { "user_name", "user_ip" },
new int[] { R.id.user_name, R.id.user_ip });
setListAdapter(listAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("id=" + id);
System.out.println("position=" + position);
}
}
SimpleAdapter 的建構子共有五個參數,依序是:
1. Context:目前執行環境。
2. data:資料來源,這裡是 List<Map>,每個 Map 代表一列。
3. resource:單列的版面檔。
4. from:Map 中的鍵名稱陣列。
5. to:要填入值的控制項 id 陣列,順序必須與 from 一一對應。
理解這個對應關係之後,只要調整 Map 的鍵與版面中的 id,就能自由變化清單的呈現方式,不必改動任何程式邏輯。
SimpleAdapter 適合結構單純的清單。一旦每一列需要顯示圖片、需要非同步載入、或是版面有部分是自訂 View,就應該改用繼承 BaseAdapter 的自訂 Adapter,並在 getView() 中實作「版面重用」(透過 convertView 與 ViewHolder 模式),否則長清單的捲動會明顯卡頓。
Android 4.4 Launcher3 改為單屏壁紙的方法
Android 4.4 的 Launcher3 預設壁紙通常會預留滑動視差空間,因此寬度會大於螢幕。若要讓壁紙只顯示單一畫面,需要調整壁紙尺寸與滑動行為。 壁紙資訊來源
Android 熱修復之 AndFix 使用教程
一、AndFix 簡介 AndFix 全稱 Android hot-fix,是阿里巴巴推出的 Android 熱修復框架,支持 Android 2.3 到 6.0 版本,
一個難住三年經驗工程師的 bug:addView 之後 scaleType 失效
晚上九點多,一位合作夥伴發來一段代碼,說折騰了很久。需求很常見:圖片選擇器選完圖後把圖顯示到界面上,類似微信發圖。由於相機照片的長寬各不相同,為了排版統一,一般都會把 I
Android OpenGL ES 繪製 3D 圖形的核心步驟
OpenGL ES 是 OpenGL 三維圖形 API 的子集,專為手機、PDA 和遊戲主機等嵌入式設備設計。早期 Android 設備普遍支持 OpenGL ES 1.