編輯:Android 開發實例
在 Android 裡,ListView 幾乎是所有列表畫面的起點。要讓它顯示資料,中間必須墊一層 Adapter;常見選擇有 ArrayAdapter 與 SimpleAdapter。網路上不少範例會直接繼承 ListActivity,本例刻意繼承普通的 Activity,把 ListView 當成一般元件來操作,這樣做的好處是版面自由度更高,也更容易理解底層流程。
主版面很單純,一個 LinearLayout 包一個 ListView:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
列的樣式則用 RelativeLayout 疊出「標題在上、說明在下、勾選框靠右」的結構:
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip"
xmlns:android="https://schemas.android.com/apk/res/android">
<CheckBox
android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="12dip"
android:focusable="false" />
<TextView
android:id="@+id/topTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip" />
<TextView
android:id="@+id/bottomTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/topTextView" />
</RelativeLayout>
這是新手最常撞到的牆。CheckBox 在 Android 中的焦點優先級高於 ListView 列本身,畫面一載入,焦點就會預設落在第一列的 CheckBox 上,導致點擊列的其他區域沒有反應。解法是在 CheckBox 上加上:
android:focusable="false"
若還有子元件互搶焦點,可以視情況搭配 android:focusableInTouchMode="false",或在列的根版面設 android:descendantFocusability="blocksDescendants",讓整列的事件統一由 ListView 處理。
public class testListViewActivity extends Activity {
private ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, Object>> listitem = new ArrayList<HashMap<String, Object>>();
for (int i = 1; i <= 10; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemTitle", "文章 " + i);
map.put("ItemText", "Android 小專案 " + i);
listitem.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(
this,
listitem,
R.layout.listview_style,
new String[]{"ItemTitle", "ItemText"},
new int[]{R.id.topTextView, R.id.bottomTextView});
myListView.setAdapter(adapter);
// ...事件設定見下文
}
}
這裡用 ArrayList<HashMap<String, Object>> 而非單純字串陣列,理由是後續要靠 key 去對應各子檢視,資料結構的彈性換來版面的彈性。注意這個集合必須 new 出來,不可留空。
接著看 SimpleAdapter 的五個參數,理解它的「從哪裡來、到哪裡去」:
R.layout.listview_style;{"ItemTitle", "ItemText"};單擊時,回呼的 View 參數就是「被點的那一列」的跟節點。只要把它轉型成 RelativeLayout,再用 getChildAt() 依索引取出子檢視即可。
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView mText = (TextView) view.findViewById(R.id.topTextView);
Toast.makeText(testListViewActivity.this,
"你點擊了第 " + position + " 項:" + mText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
這裡示範用 findViewById 取代硬寫的 getChildAt(1)。兩者效果相同,但 id 定位不受版面巢狀層數影響,日後改版面也不易出錯。若堅持用索引,務必確認列結構沒有變動。
myListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
TextView title = (TextView) ((View) info.targetView).findViewById(R.id.topTextView);
menu.setHeaderIcon(R.drawable.icon);
menu.setHeaderTitle(title.getText());
menu.add(0, 0, 0, "高亮");
menu.add(0, 1, 1, "置頂");
}
});
選單被點選後,事件會回到 Activity 的 onContextItemSelected(),用 item.getItemId() 分辨是哪一項,再對 info.position 指到的列做處理:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
TextView title = (TextView) info.targetView.findViewById(R.id.topTextView);
switch (item.getItemId()) {
case 0:
title.setBackgroundColor(Color.BLUE);
return true;
case 1:
Toast.makeText(this, title.getText() + " 已置頂", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
原作者示範的是先抓 getChildAt(0) 拿到 ListView 再往下挖,那樣寫法在「長按後要正確指向被按的列」這件事上會失準,因為 getChildAt(0) 永遠是第一個可見列。AdapterContextMenuInfo 才是框架為此準備的答案。
ViewHolder 搭配 notifyDataSetChanged(),避免整列重繪。BaseAdapter 並實作 convertView 重用,SimpleAdapter 只適合結構單純的清單。ArrayList<HashMap<String, Object>> 時務必 new 出來,否則一取值就是空指針。
Android 進度條教學:用 Loading Spinner 表達「還不知道要等多久」
當 App 要執行一段耗時的工作,例如從網路下載資料,使用者需要一個「它還在動」的訊號。Android 用 ProgressBar 這個元件來表達等待狀態,而它主要有兩種
Android EditView 控件常用屬性與監聽技巧全解析
什麼是 EditView 在 Android 的界面體系裡,EditView(正式名稱為 EditText)扮演著「可輸入文本框」的角色。它並不是一個全新的控件,而是繼承
Android 仿今日頭條下拉導航菜單:PopupWindow 加 GridView 實作
本文完整示範如何在 Android 中做出「下拉導航選擇菜單」的效果,也就是點一下頂部標題,下方展開一排圖文並茂的分類選單。這種互動在新聞客戶端裡很常見,用來容納較多的分
Android 進階:藍牙隱藏 API 探秘
前文講解了 Android 藍牙的基本用法,本文則更深入一步,探討藍牙方面的隱藏 API。用過 Android 系統設置的人都知道,藍牙搜索之後可以建立配對或解除配對,但