編輯:關於 Android 程式設計
本文示範如何實作一個「上方搜尋框 + 下方兩層下拉選單」的互動效果,供大家參考。

上方是搜尋框,下方是可展開的兩層下拉選單。

輸入關鍵字後,下方列表會即時顯示符合的結果;清空輸入則回到原本的下拉選單。
(1)主框架。從上到下依序是文字輸入框、搜尋結果列表、兩層列表。這裡用同一個 LinearLayout 包住三者,方便切換顯示與隱藏。
<LinearLayout> <LinearLayout android:id="@+id/city_middle"> <EditText android:id="@+id/txtfind" android:hint="請輸入" > </EditText> <ListView android:id="@+id/listfind" > </ListView> <ExpandableListView android:id="@+id/exList" /> </LinearLayout> </LinearLayout>
(2)第一層選單樣式。用 ImageView 的圖示來區分該項目是否已展開:
<LinearLayout> <TextView android:id="@+id/group" > </TextView> <ImageView android:id="@+id/tubiao"> </ImageView> </LinearLayout>
(3)第二層選單樣式:
<LinearLayout> <TextView android:id="@+id/child"> </TextView> </LinearLayout>
(1)定義選單資料結構。第一層用 fatherList,第二層用 childList(每個元素是一個 List,對應第一層的每個項目)。
public static List<BasicNameValuePair> fatherList = new ArrayList<BasicNameValuePair>(); public static List<List<BasicNameValuePair>> childList = new ArrayList<List<BasicNameValuePair>>();
產生測試資料:
for (int i = 0; i < 20; i++) { fatherList.add(new BasicNameValuePair("father" + i, "father" + i)); List<BasicNameValuePair> cList = new ArrayList<BasicNameValuePair>(); for (int j = 0; j < 5; j++) { cList.add(new BasicNameValuePair("child" + i + ":" + j, "child" + i + ":" + j)); } childList.add(cList); }
(2)搜尋結果列表的 Adapter。它在建構子中直接完成「比對 → 篩選」的工作,把命中的項目收集到自己的 list 裡:
protected class ListAdapter extends BaseAdapter { private LayoutInflater mInflater; //查詢結果列表 private List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); public ListAdapter(Context context, String strin) { mInflater = LayoutInflater.from(context); //查詢匹配 for (int i = 0; i < childList.size(); i++) { for (int j = 0; j < childList.get(i).size(); j++) { String tmp = childList.get(i).get(j).getValue(); if (tmp.indexOf(strin) >= 0) { list.add(new BasicNameValuePair(childList.get(i).get(j).getName(), tmp)); } } } } public int getCount() { return list.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { convertView = mInflater.inflate(R.layout.child, null); TextView title = (TextView) convertView.findViewById(R.id.child); title.setText(list.get(position).getValue()); return convertView; } }
初始化搜尋列表,預設隱藏:
list = (ListView) findViewById(R.id.listfind); list.setVisibility(View.GONE);
(3)兩層選單的 Adapter,繼承 BaseExpandableListAdapter:
protected class ExAdapter extends BaseExpandableListAdapter { @Override public int getGroupCount() { return fatherList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return fatherList.get(groupPosition).getValue(); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition).getValue(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.group, null); } TextView t = (TextView) view.findViewById(R.id.group); t.setText(fatherList.get(groupPosition).getValue()); //展開,改變圖片 ImageView gImg = (ImageView) view.findViewById(R.id.tubiao); if (isExpanded) gImg.setBackgroundResource(R.drawable.mm_submenu_down_normal); else gImg.setBackgroundResource(R.drawable.mm_submenu_normal); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.child, null); } TextView t = (TextView) view.findViewById(R.id.child); t.setText(childList.get(groupPosition).get(childPosition).getValue()); return view; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }
初始化兩層選單。這裡把群組指示箭頭與分隔線都關掉,讓外觀完全交由自訂版面控制:
exList = (ExpandableListView) findViewById(R.id.exList); exList.setAdapter(new ExAdapter()); exList.setGroupIndicator(null); exList.setDivider(null);
(4)搜尋事件。用 TextWatcher 監聽輸入變化,做到「邊打字邊篩選」:
txtFind = (EditText) findViewById(R.id.txtfind); txtFind.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s != null && !s.toString().equals("")) { list.setAdapter(new ListAdapter(DWinterDemoActivity.this, s.toString())); list.setVisibility(View.VISIBLE); exList.setVisibility(View.GONE); } else { list.setVisibility(View.GONE); exList.setVisibility(View.VISIBLE); } } });
邏輯很單純:有輸入就顯示搜尋結果、隱藏下拉選單;輸入清空就還原成下拉選單。兩個控制項共用同一個父容器,因此切換顯示即可,不必另開畫面。
(5)避免進入頁面時自動彈出軟鍵盤:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
這一行在只有搜尋功能的頁面上很好用;若頁面本身以輸入為主要目的,則可以拿掉。
上面的 ListAdapter 在 getView() 中每次都重新 inflate 版面,這在資料量大或輸入速度快時會造成明顯卡頓。實務上建議改成 convertView 複用機制:先判斷 convertView 是否為 null,不為 null 就重用它並只更新文字。另外,afterTextChanged() 每次按鍵都會重建整個 Adapter 並做雙層迴圈比對,若資料規模達到數千筆,可考慮加上 debounce(延遲 200~300ms 才觸發篩選),輸入體驗會順很多。
以上就是實作兩層下拉選單搭配即時搜尋的完整做法,希望對你的 Android 開發有所幫助。
Android 動畫入門:補間動畫、幀動畫與 LayoutAnimation 重點整理
動畫在 App 中經常用於提升操作回饋與視覺流暢度,例如 PopupWindow 彈出、Tab 切換、載入提示與列表進場效果。Android 早期主要支援補間動畫與幀動畫
使用 SurfaceView 實作自訂浮水印相機
浮水印相機可以在照片上加入時間、地點、操作資訊或自訂文字。本文以 SurfaceView 為基礎,說明如何完成相機預覽、拍照、浮水印顯示與結果頁流程。 功能設計 自訂相機
Android 側滑菜單手勢優化與平滑開闔實作
從生硬拖拽到自然側滑 早期的側滑菜單實作,常常只是讓主面板跟著手指移動,缺少慣性與吸附效果,操作起來會比較生硬。要貼近 QQ6.0 的體驗,就必須加入手勢判斷、平滑動畫與
打造順手好用的 Android 模擬器:完整設定指南
預設的模擬器有多難用 預設狀態下的 Android 模擬器,最顯眼的問題是界面醜:視窗佔掉整個螢幕的龐大空間,還莫名其妙地擺著一個實體螢幕鍵盤。 但如果你真的用過,還有