編輯:關於 Android 程式設計
本文示範一個兩級分類的彈出式選單,效果類似美團、淘寶、百度糯米的分類頁。當分類數量很多時,用「左側一級、右側二級」的雙欄結構會比多層展開清爽得多。
先看效果:

1、PopupWindow 初始化
彈窗高度設為螢幕的三分之二、寬度滿版,並監聽 dismiss 事件順便把背景遮罩淡出、把左右清單捲回頂部:
popupWindow = new PopupWindow(this); View view = LayoutInflater.from(this).inflate(R.layout.popup_layout, null); leftLV = (ListView) view.findViewById(R.id.pop_listview_left); rightLV = (ListView) view.findViewById(R.id.pop_listview_right); popupWindow.setContentView(view); popupWindow.setBackgroundDrawable(new PaintDrawable()); popupWindow.setFocusable(true); popupWindow.setHeight(ScreenUtils.getScreenH(this) * 2 / 3); popupWindow.setWidth(ScreenUtils.getScreenW(this)); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { darkView.startAnimation(animOut); darkView.setVisibility(View.GONE); leftLV.setSelection(0); rightLV.setSelection(0); } });
2、左側一級分類的點擊
點左側項目時要先判斷它有沒有二級資料:沒有就直接回傳結果並關閉彈窗;有則更新選中狀態並刷新右側清單。這裡刻意判斷「點的是否已是當前選中項」,若是就不做事,避免無意義的刷新:
//左側ListView點擊事件 leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //二級數據 List<SecondClassItem> list2 = firstList.get(position).getSecondList(); //如果沒有二級類,則直接跳轉 if (list2 == null || list2.size() == 0) { popupWindow.dismiss(); int firstId = firstList.get(position).getId(); String selectedName = firstList.get(position).getName(); handleResult(firstId, -1, selectedName); return; } FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter()); //如果上次點擊的就是這一個item,則不進行任何操作 if (adapter.getSelectedPosition() == position){ return; } //根據左側一級分類選中情況,更新背景色 adapter.setSelectedPosition(position); adapter.notifyDataSetChanged(); //顯示右側二級分類 updateSecondListView(list2, secondAdapter); } });
3、右側二級分類的點擊
右側點完就代表使用者已完成選擇,直接關閉彈窗並回傳一級與二級的 id:
//右側ListView點擊事件 rightLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //關閉popupWindow,顯示用戶選擇的分類 popupWindow.dismiss(); int firstPosition = firstAdapter.getSelectedPosition(); int firstId = firstList.get(firstPosition).getId(); int secondId = firstList.get(firstPosition).getSecondList().get(position).getId(); String selectedName = firstList.get(firstPosition).getSecondList().get(position) .getName(); handleResult(firstId, secondId, selectedName); } });
4、頂部標籤:顯示或隱藏選單
再點一次就收起,同時用 animIn/animOut 搭配遮罩做出背景變暗的效果:
if (popupWindow.isShowing()) { popupWindow.dismiss(); } else { popupWindow.showAsDropDown(findViewById(R.id.main_div_line)); popupWindow.setAnimationStyle(-1); //背景變暗 darkView.startAnimation(animIn); darkView.setVisibility(View.VISIBLE); }
5、刷新右側清單
只更新資料再 notifyDataSetChanged,不必重複 new Adapter:
//刷新右側ListView private void updateSecondListView(List<SecondClassItem> list2, SecondClassAdapter secondAdapter) { secondList.clear(); secondList.addAll(list2); secondAdapter.notifyDataSetChanged(); }
幾個實作上的提醒:PopupWindow 的背景必須設定(這裡用 PaintDrawable),否ğu按返回鍵無法關閉彈窗;showAsDropDown 搭配 setAnimationStyle(-1) 是為了關掉系統預設的進入動畫;若分類資料量很大,建議把一級資料與其二級清單做成關聯結構一次載入,避免每次點擊都去查資料造成延遲。
完整專案可於本站下載。更多 Android 相關內容也可參考本站的其他專題文章。
PhotoView 源碼深度解讀:圖片縮放是如何實現的
開源地址:https://github.com/chrisbanes/PhotoView PhotoView 是一個幫助開發者輕鬆實現 ImageView 縮放的庫,縮放
Universal-Image-Loader 完全解析:從使用到源碼的圖片加載框架全攻略
做 Android 的過程中,幾乎每個人都要面對異步加載圖片這件事,尤其是列表裡一次性載入幾十上百張圖的時候。圖片加載從來不是「new 一個 Bitmap 塞進 Imag
Android 狀態欄透明化與著色:三種實作方案與場景對照
專案上遇到兩個相關需求:狀態欄要透明,且當使用者拖動、頂部內容滾動時,標題欄顏色要能跟著變化。這類效果常被稱為「沉浸式狀態欄」(這個叫法網路上有不少討論,這裡統一稱之為狀
Android 布局技巧之創建高效布局
Android 的 UI 工具包提供了多種布局,但只使用最直覺的方式,未必能獲得最佳效能。尤其當布局層級過深或元件數量過多時,初始化、測量與繪製成本都會增加。本文以常見列