Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實作兩層下拉選單與即時搜尋

編輯:關於 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 開發有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved